Tag: SSMS Tips

  • SSMS

    SSMS

    SSMS, SQL Server Management Studio, is the standard application that we use to run queries and administer instances of SQL Server.

    The Microsoft Documentation and links to download can be found here – Download SSMS

    My blog posts with the SQL Server Management Studio (SSMS) Tag may help guide you through common configuration tasks and/or resolve issues when using SSMS.

    SSMS Maximum Characters Displayed, September 12, 2022
    SSMS Show Line Numbers, September 3, 2022
    How to Connect to SQL Server with Windows Authentication using a Different Domain, August 25, 2022
    USE Command in SQL Server, November 25, 2020
    Change Default Database in SQL Server, November 23, 2020
    Disk Usage by Top Tables Report in SQL Server, October 4, 2020
    Include Execution Trace Messages in SQL Server Error Logs, May 5, 2020
    “Index was out of range” SQL Server Management Studio, April 15, 2019
    The (SSMS) Import Flat File Feature, January 24, 2019
    Using Custom Connection Colours in SSMS, October 22, 2018
    Exporting SQL Server Query Results to CSV, October 2, 2018

    Hope this is useful for you. This page will be updated as I publish more blog posts in this area.

  • SSMS Silent Install

    SSMS Silent Install

    This is a note on how to silently install SQL Server Management Studio (SSMS) via command (PowerShell).

    SSMS is a tool most people use to administer SQL Server and to run SQL queries. If you need to install SSMS many times or on multiple computers, you may want to use the silent installation feature when automating the process.

    This ‘silent‘ installation allows you to install SSMS without any user interaction, making it quick and easy to deploy on multiple computers. In this demo, we are using the Standard Microsoft Installer Command-Line Quiet Option (/q).

    How to Silently Install SSMS

    To perform a silent installation of SSMS, you will need a copy of the SSMS installation files.
    You can download SSMS from this link here: SSMS Download (Microsoft Docs)

    Open Windows Terminal or any command prompt for PowerShell as Administrator and navigate to the directory where you have the SSMS installation files downloaded. Then run the msi exec command as shown:

    # Navigate to directory with ssms download
    cd ssms
    gci
    
    # Install SSMS (silently)
    .\SSMS-Setup-ENU.exe /q
    SSMS Silent Install

    The only parameter being used here is /q which specifies that the installation should be performed silently. So SSMS will install in the background and your terminal prompt will return to a command-ready state when the installation has been completed.

    SSMS will be installed silently on the host and be ready for use. That should be all there is to this one. Feel free to check out the SSMS Tips Tag for more useful information about this application.

  • SSMS Maximum Characters Displayed

    SSMS Maximum Characters Displayed

    By default, the max number of characters displayed in a single column of a query in SSMS when outputting as text is 256 characters.

    We can amend our query outputs in SSMS to Results to Text, rather than the default grid layout. This can be done by clicking the top bar menu button as shown in the screenshot below.

    SSMS Results to Text Option

    If using text output rather than grid, you may encounter issues with truncated query outputs if a column contains more than 256 chars.

    The screenshot below shows an example of a truncated column, only showing 256 out of 550 characters in the column.

    SSMS Maximum Characters Results to Text

    The demo below shows how to fix this and increase this SSMS default value to the maximum chars allowed.

    Increase SSMS Maximum Characters for Results To Text

    To increase your SSMS Maximum Characters for Results to Text, follow these steps.

    1. Click Tools at the top menu bar and select Options.

    SSMS Open Options Menu

    2. Navigate to Query Results > SQL Server > Results to Text.

    SSMS Results to Text Max Chars

    3. Open a new query Window and re-run your query or procedure.

    SSMS Results to Text Full

    That’s it, working for you now… I hope.

    The reason I note this one down as a blog post is that there are rare occasions where you might need to use Results to Text as a DBA. One example, when working with MSSQL Replication, the output of sp_scriptdynamicupdproc will likely contain more than 256 characters for the procedure statement output.

    Feel free to check out my SSMS Tips tag for more random Management Studio tips and stuff.

  • SSMS Show Line Numbers

    SSMS Show Line Numbers

    This post contains a guide on how to show line numbers in the SSMS query window. Useful if you want to see line numbers while you code.

    As well as general preference, enabling line numbers in SQL Server Management Studio is particularly useful when debugging an error message in a SQL script.

    The demo below includes the following:
    # Show Line Numbers in SSMS
    # Debugging Example

    Show Line Numbers in SSMS

    To enable line numbers in Management Studio, click Tools at the top menu bar and select Options.

    SSMS Tools Options

    Navigate to Text Editor > Transact-SQL > General and tick Line Numbers.

    SSMS Show Line Numbers

    Click OK, and you’ll see now see line numbers in the SSMS query window.

    SSMS Line Numbers

    Debugging Example

    When you are debugging a SQL script within SSMS, showing line numbers might help you when encountering an error like this one below.

    I’ve opened a random Stored Procedure, amended and attempted to run it, knowing it will fail.

    SQL Error Line

    The line our code has failed on is included in the output error message:
    Msg 102, Level 15, State 1, Procedure sp_addlogin, Line 33 [Batch Start Line 7]
    Incorrect syntax near ‘gif’.
    Msg 156, Level 15, State 1, Procedure sp_addlogin, Line 35 [Batch Start Line 7]
    Incorrect syntax near the keyword ‘else’.

    Line numbers shown in these error messages aren’t always accurate, but it should give you a pointer to the section of broken code.

    You can Double-Click the error message to bring you to the referenced line number, or use the Go To Line feature (CTRL + G).

  • How to Connect to SQL Server with a Different Domain User Account

    How to Connect to SQL Server with a Different Domain User Account

    This post is a guide on how to connect to SQL Server with Windows Authentication, but using a different Domain User rather than your own.

    The regular Windows SQL Server User connects & runs their queries via SQL Server Management Studio (SSMS). If you’re in a corporate environment, you’ll likely be logged into your computer with an Active Directory (AD) User. In this example, if we open SSMS we will likely connect with our currently logged-in AD account.

    This guide shows how to authenticate to MSSQL using an AD account that is not the same as your own logged-in user, which is particularly useful for testing newly created SQL Server AD User access and permissions.

    Open SSMS with Other Domain User

    A regular SQL Server Management Studio (SSMS) user in Windows has the application pinned to their taskbar. Search with the Windows button and follow the same on the SSMS app icon.

    Right-Click SSMS Icon –> Hold CTRL + Right-Click Icon –> Select Run As Different User

    SSMS Run as a Different User
    Run as a different user prompt

    Simple as that. No need for much longer explanations. All I can refer you to is my SSMS Tips tag for more Management Studio related blog posts.

  • Disk Usage by Top Tables Report in SQL Server

    Disk Usage by Top Tables Report in SQL Server

    The Disk Usage by Top Tables Report in SQL Server is a quick way to get the sizes of all tables within a database. It’ll show all tables largest to smallest tables by total size on disk.

    If you notice that a database is growing larger in size, then you will want to know what data is causing the increase. Knowing your largest table(s) on all SQL systems helps you make better decisions overall. For example, with this knowledge, you might want to consider performing a review of indexes or compressing some indexes to save space.

    There are many ways to check the size of a table in MS SQL Server. This post is to help show you how to open the SQL Server Disk Usage by Top Tables Report, which is what I find to be one of the more efficient ways to check table sizes in SQL.

    I have another post if of interest that shows a variety of ways to check table sizes in MSSQL. Below shows a demo of the Disk Usage by Top Tables Report in MSSQL as described.

    Disk Usage by Top Tables Report

    To open this report:-

    1. Open SQL Server Management Studio (SSMS).
    2. Expand Databases.
    3. Right-click the Database and select Reports > Standard Reports > Disk Usage by Top Tables

    Disk Usage by Top Tables SSMS

    The report shows that this database has 2 tables close to 700MB in size, both containing the same number of rows but there are some differences in the indexes.

    Disk Usage by Top Tables Report

    For more random tips for checking disk space in MSSQL, have a look at my Measuring Databases tag.

  • Using Custom Connection Colours in SSMS

    Using Custom Connection Colours in SSMS

    SQL Server Management Studio (SSMS) is the tool that most SQL Server user queries are run from. It has an option that allows you to change the colour of your query banner, which is useful if you want visually mark a server that you work with every day.

    My choice of colour below stands out quite well:

    SSMS Connection Colors

    Amending this is particularly useful when you are running queries in test, development & production environments from the same host. You want to have a visual warning/display to indicate clearly which MSSQL host you are running your next query on.

    This is a feature that I find myself using from time to time, and I see others using it frequently.


    Changing Connection Colours in SSMS

    To change a connection colour for a particular SQL Server Instance, follow below:

    1. Click to connect to a SQL Server instance within SSMS.

    SSMS Connect to Database

    2. Click Options.

    SSMS Connection Options

    3. Within the Connection Properties tab, check the box to use custom colours and select your preference.

    Change Connection Colours SSMS

    4. Open a new query window to view the change.

    SQL Server Create Database
  • Exporting SQL Server Query Results to CSV

    Exporting SQL Server Query Results to CSV

    This post contains a guide on how to export data from a SQL Server database using SSMS.

    SSMS, SQL Server Management Studio, is the standard application that most people use to run queries for a Microsoft SQL Server Database. The default export option in SSMS is set as Column-Aligned, and we need to change it to Comma-Delimited to get our query output formatted correctly for CSV.

    To export a SQL query to CSV in SSMS we use the Results to File option which is shown in the demo below in this post. We use this instead of the standard query output option, Results to Table.

    The first part of this post runs through how to export a SQL query to a file using the default output options in SSMS, and then we are changing the output format column options as mentioned above.

    # Export SQL Server Query to File (Default SSMS Options)
    # Change SSMS Output Format Options (for CSV Files)

    Export SQL Server Query to File

    To export a query the simplest way, would be for us to click Results to File as marked below.

    SSMS Results to File Option

    When you run the query, it’ll prompt asking where and what to save the file as.

    .rpt File SQL Server

    The default file type is as shown above, a .rpt file. If we open that in NotePad++, it’s looking like this:

    Open .rpt File Notepad++

    We’ll save it as CSV this time.

    Save .rpt file as .csv

    You will now likely want to change the delimiter after looking at this output file. This .rpt file should not be opened as CSV.

    .rpt file in Excel

    Change SSMS Output Format Options

    Now that we’ve looked at the default SSMS export file option, let’s change it to Comma Delimited so we can open SQL query outputs as CSV.

    Within SSMS, go to Tools > Options as shown in the screenshot below.

    SSMS Tools Options

    In the Options window that appears, navigate to Query Results > SQL Server > Results to Text

    SSMS Results to Text Option

    The default option is shown above, Column aligned. Change this to Comma delimited.

    SSMS Delimitor

    We now need to disconnect our current session within the SSMS Object Explorer and connect back to the SQL Server instance.

    SSMS Disconnect

    Now, if we run that query as we did above outputting as text, the output file should be formatted correctly when saved as CSV.

    Export to CSV from SQL Server

    You may have identified an issue with the exported CSV data, and it contains “rows affected” at the end of it. The reason for this is that we need to include SET NOCOUNT within the SQL query when using Results to Text. I talk more about this in another one of my posts – Count Rows in CSV Files > Checking for Data Issues

    I hope this guide has helped you export a SQL query to CSV today. If you would like more SSMS-related tips, feel free to have a look at my SSMS page which is frequently updated.

  • How to Check Table Sizes in SQL Server

    How to Check Table Sizes in SQL Server

    There are many ways to check the size of a table in SQL Server. The way you will do it will likely depend on what your task at hand is.

    For example, a quick look at the Disk Usage by Top Tables report on a database you’ve never seen before would instantly show you the large tables in a database. But if you want this information saved somewhere else, e.g. to a table in SQL, it’s an easy task for us to run a TSQL script and output to Excel or CSV.

    In this post, I’m sharing a variety of methods to check table sizes in MS SQL, including:
    # SSMS Disk Usage by Top Tables Report
    # Check Table Properties in SSMS
    # MSSQL sp_spaceused System Stored Proc
    # MSSQL Script to Get Table Sizes

    SSMS Disk Usage by Top Tables Report

    To open this report:-
    1. Open SQL Server Management Studio (SSMS).
    2. Expand Databases.
    3. Right-click the Database and select Reports > Standard Reports > Disk Usage by Top Tables

    Disk Usage by Top Tables SSMS

    The report shows that this database has 2 tables close to 700MB in size, both containing the same number of rows but there are some differences in the indexes.

    Disk Usage by Top Tables Report

    I find this method one of the most efficient methods for getting the disk usage of tables in MSSQL.

    Check Table Properties in SSMS

    Another way to check the sizes of tables in SQL Server Management Studio (SSMS) is to open the Table Properties within the Object Explorer.

    To do this, expand the database and tables. You may need to add a filter to find your table.

    SSMS Object Explorer

    Now, right-click the table and select Properties as shown in the screenshot below.

    SSMS Table Properties

    Open the Storage tab on the left-hand sidebar. It may take a second for the window to load, but this will show the index space, data space usage and the table row count.

    SSMS Table Properties Storage Tab

    MSSQL sp_spaceused System Stored Proc

    For this and the following methods to get SQL table sizes, we need to open a new query window.

    We can run the sp_spaceused MS SQL system stored procedure, adding the Schema and Table names as parameters.

    -- Check Table Sizes (MS SQL System SP)
    sp_spaceused '[dbo].[FactResellerSalesXL_CCI]';
    sp_spaceused SQL

    MSSQL Script to Get Table Sizes

    Lastly, we can run a query on SQL Server system tables to get the table sizes.

    -- Get Table Size SQL Server Script
    SELECT 
        t.NAME AS TableName,
        s.Name AS SchemaName,
        p.rows AS RowCounts,
        CAST(ROUND(((SUM(a.total_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS TotalSpaceMB,
        CAST(ROUND(((SUM(a.used_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS UsedSpaceMB, 
        CAST(ROUND(((SUM(a.total_pages) - SUM(a.used_pages)) * 8) / 1024.00, 2) AS NUMERIC(36, 2)) AS UnusedSpaceMB
    FROM 
        sys.tables t
    INNER JOIN      
        sys.indexes i ON t.OBJECT_ID = i.object_id
    INNER JOIN 
        sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
    INNER JOIN 
        sys.allocation_units a ON p.partition_id = a.container_id
    LEFT OUTER JOIN 
        sys.schemas s ON t.schema_id = s.schema_id
    WHERE 
        t.NAME NOT LIKE 'dt%' 
        AND t.is_ms_shipped = 0
        AND i.OBJECT_ID > 255 
    GROUP BY 
        t.Name, s.Name, p.Rows
    ORDER BY 
        t.Name
    
    Get Index Sizes SQL Server

    That’s us done for now. For more SQL Tips from a random MS SQL DBA in the field, feel free to check out my SQL DBA Blog page which includes a list of my latest posts.