-
How to Show Line Numbers in SSMS
This post contains a quick 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.
Show Line Numbers in SSMS
To enable line numbers in Management Studio, click Tools at the top menu bar and select Options.
Navigate to Text Editor > Transact-SQL > General and tick Line Numbers.
Click OK, and you’ll see now see line numbers in the SSMS query window.
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.
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).
-
Add a Filegroup to a Database in SQL Server
This post contains a demo on how to add a new Filegroup for a database in SQL Server. Most databases work well with single data/log files and do not need this.
Adding filegroups to a SQL Server database can improve performance by distributing data across multiple disk drives or supporting specific features like Change Data Capture (CDC). While many databases perform well with default data (
.mdf
) and log (.ldf
) files, introducing user-defined data files (.ndf
) within filegroups can optimize storage and organisation.Here’s a quick summary of when to use Filegroups:
> Performance Improvements:
Spreading data across multiple disk drives can reduce I/O contention.
> Data Organization:
Isolating specific data types or features (e.g., CDC) in separate filegroups.
> Scalability:
Efficiently managing large databases by segmenting data into logical groups.For further explanations on SQL Server Filegroups, check out this MS Docs page. And while you’re at it you could be reading about the SQL Server Database File Recommendations.
Adding New Filegroup for a Database
In the demo below we’re adding 2x new files to a databases. I’m going to start this demo by creating a sample database and adding some tables:
Next, I’m going to add the new Filegroup by running the following:
-- Create data files for Filegroup ALTER DATABASE [sqlDBA] ADD FILE ( NAME = sqldba_data1, FILENAME = 'D:\mssql_data\sqldba_data1.ndf', SIZE = 500MB, FILEGROWTH = 50MB ), ( NAME = sqldba_data2, FILENAME = 'D:\mssql_data\sqldba_data2.ndf', SIZE = 500MB, FILEGROWTH = 50MB ) TO FILEGROUP [SQLDBA_FG1]; -- Check files for a database USE [sqlDBA]; GO SELECT file_id, type, type_desc, data_space_id, name, physical_name, state, size, max_size, is_percent_growth, growth FROM sys.database_files;
I’ll now check the sys.database_files system table to verify the new Database Filegroup:
-- get database file information SELECT file_id, type, type_desc, data_space_id, name, physical_name, state, size, max_size, is_percent_growth, growth FROM sys.database_files;
To view or manage database files through the SSMS interface:
> Select Tasks > Shrink > Files to inspect the files and their configuration.
> Right-click the database in SSMS.When to Use Filegroups
A common use case for filegroups is isolating CDC data, as explained in How to Enable Change Data Capture (CDC) in SQL Server.
By adding filegroups thoughtfully, you can enhance database performance, improve data management, and prepare your system for future scaling needs. Hope this helps!
-
How to Check PowerShell Version
This guide explains how to check your PowerShell version on a Windows computer using the
$PSVersionTable
command.Running the latest PowerShell version ensures access to new features and cmdlets. Some scripts may rely on commands not available in older versions, causing compatibility issues. We should keep it up-to-date making it part of your regular monthly patching.
1. Checking Your PowerShell Version
To check your PowerShell version, simply run
PSVersionTable
:-- Check PowerShell Version $PSVersionTable
In this example, the system is running PowerShell version 5.1.19041. We should upgrade to pwsh version 7.
2. Upgrading PowerShell
Windows PowerShell 5.1 is the latest built-in version on Windows, but newer versions, such as PowerShell 7 are available. To install the latest version, follow Microsoft’s official guide: Installing PowerShell, which includes installing on Windows, MacOS and Linux.
Before looking at rolling out upgrades across several Windows hosts, we should look at the current Supported PowerShell Versions for Windows compatibility table from Microsoft.
Hope this guide was useful!
-
Disabling Change Data Capture in SQL Server
This post contains a demo on how to disable CDC (Change Data Capture) in SQL Server 2022. The sp_cdc_disable_table and sp_cdc_disable_db in-built MSSQL SPs are what we should use to disable CDC in SQL Server which is explained more below.
The sp_cdc_disable_table and sp_cdc_disable_db are the in-built MSSQL SPs to do this which are explained below.
Disabling CDC can be a common task for a SQL DBA as we need to do this each time a new column is created on the source table. We run sp_cdc_enable_table to add all columns in a table to CDC, but then when a new column is created we need to drop CDC on the table and re-enable it in SQL.
My previous blog post explains how to enable CDC in SQL Server and will show you how to enable CDC. This post is a guide for the CDC disable action.
Before proceeding with a change like this on a production SQL environment, we should read Microsoft Docs on Enabling & Disabling CDC, and test the change to get familiar enough with CDC.
How to Disable CDC SQL Server
Disabling CDC for a table in MSSQL will drop all existing captured change data. If you are intending on performing this on a production SQL Server, you should verify whether we need to back up & copy existing CDC data.
To disable Change Data Capture on a table in SQL Server, we should first check for existing CDC configuration information by running sp_cdc_help_change_data_capture. Using this info we can populate the parameters for sp_cdc_disable_table, particularly the
capture_instance
name.USE [demoBlog]; GO -- Check current CDC config EXEC sys.sp_cdc_help_change_data_capture -- Disable CDC for a table EXECUTE sys.sp_cdc_disable_table @source_schema = N'dbo', @source_name = N'posts', @capture_instance = N'dbo_posts'
If you are removing CDC from all tables permanently, we can disable CDC on the database too.
Run sp_cdc_disable_db to disable CDC on a database in MS SQL as per the example below.
USE [demoBlog]; GO -- Disable CDC for a database EXEC sys.sp_cdc_disable_db GO -- Check if CDC is enabled on databases SELECT is_cdc_enabled,* FROM sys.databases;
-
How to Enable CDC in SQL Server
This is a post on how to enable Change Data Capture (CDC) in SQL Server.
Enabling this on a database and table will allow the recording of activity when tables and rows have been modified. This is a feature available for us to configure within SQL Server and Azure SQL out of the box.
CDC uses the SQL Agent to record inserts, updates and deletes that happen on a table. Enabling this might be useful to help give a business insight on changes to Personal Identifiable Information (PII) over time for GDPR requirements. Or, another example could be real-time tracking of orders for pricing updates to a product.
For more examples and information on what CDC is, we can refer to MS Docs : What is change data capture (CDC)?
If you are considering enabling this feature, as always we should test and read through the software documentation to ensure all potential impacts and limitations have been reviewed. Some main considerations on the systems side with this one include the additional disk space your CDC tables will need, and performance depending on the number of tables/changes happening.
The demo below covers the following:
> Enabling Change Data Capture for a Database
> Enabling Change Data Capture for a TableEnabling Change Data Capture for a Database
Before enabling CDC on a database we want to first check:
1. Is CDC already enabled.
Best to check this in larger or more unknown environments.
2. Is the database owner is set to something other than ‘sa’.
You’ll receive an error message if you don’t have it set to correctly.We can check the above configured settings by querying
sys.databases
.-- Check if Database is has CDC Enabled SELECT is_cdc_enabled, * FROM sys.databases; -- (IF_REQUIRED) Change Database Owner to 'sa' -- Error if <> 'sa' = "Could not update the metadata that indicates database demoBlog is enabled for Change Data Capture" ALTER AUTHORIZATION ON DATABASE::[demoBlog] TO [sa] GO
Enabling CDC for a SQL Server database is done executing the
sys.sp_cdc_enable_db
system stored procedure, as shown in this example:USE [demoBlog]; GO -- Enable CDC for Database ms sql EXEC sys.sp_cdc_enable_db GO /* New system tables > [cdc].[captured_columns]; [cdc].[change_tables]; [cdc].[ddl_history]; [cdc].[index_columns]; [cdc].[lsn_time_mapping]; [dbo].[systranschemas]; */
We can verify the database is enabled for CDC by checking sys.databases, and there’s new system tables for CDC as displayed in the screenshot above.
Enabling CDC for a Table
I’ve just enabled CDC on this demo database, so now it’s time to enable it on some tables.
We can check if CDC is already enabled by querying the
sys.tables
table and checking theis_tracked_by_cdc
column.To control the placement of change table data, we can create Filegroups ahead of enabling CDC on tables. Microsoft recommends we do this so that our change tables are separate from source tables.
I’m now creating a new File Group for CDC below:
-- Create File Group for CDC ALTER DATABASE [demoBlog] ADD FILEGROUP [DemoBlog_CT]; ALTER DATABASE [demoBlog] ADD FILE ( NAME = demoBlog_data1, FILENAME = 'D:\MSSQL_DATA\demoBlog_data1.ndf', SIZE = 500MB, FILEGROWTH = 50MB ) TO FILEGROUP [DemoBlog_CT]; SELECT * FROM sys.filegroups; SELECT * FROM sys.database_files;
Use
sys.sp_cdc_enable_table
to enable CDC on a table, as per my next example below.When a table is enabled for CDC, a record will be logged in the transaction log for each DML operation applied to the table from that point onwards.
USE [demoBlog]; GO -- Enable CDC on Table EXEC sys.sp_cdc_enable_table @source_schema = N'dbo', @source_name = N'posts', @role_name = NULL, @filegroup_name = N'DemoBlog_CT' GO -- Job 'cdc.demoBlog_capture' started successfully. -- Job 'cdc.demoBlog_cleanup' started successfully.
Upon executing the above SQL, new SQL Agent jobs are created for the CDC Collection.
One interesting note, “If both CDC and replication are enabled for a database, the log reader handles the population of the CDC change tables.”.
For an extended demo, here’s an example inserting a new of a row into our table, and then a querying the CDC metadata to watch it seed through:
I hope this post was useful and gave some good info! Feel free to add a comment below for any questions or suggested updates I can do here.