Attach to Process

SQL

This is sound advice. Using -- to comment out scripts in SQL is generally okay. But there is a risk of the script breaking, when all of a sudden your script is listed in just one line. So the better practice is to use /* comment goes here */ instead. Hats off to Brent Ozar's post for this tip.

Link: Never, Ever, Ever Start T-SQL Comments with Two Dashes


I figured I would add some sample script to illustrate the problem. So here goes.

Original Script

delete from dbo.Customers
--where createDate >= '20211201'
where customerId = 99

Take for example the script above. You originally wrote that script to delete all Customer records in the database that were created since the month of December. You then realize that it will delete more records than you wanted, so you comment out the createDate filter, and use a customerId filter instead.

Now in its current form, the above script would work just fine. You run it and it will delete the record where customerId is equal to 99. All is good.

But what happens if that script is somehow parsed by some other tool, and the resulting script displayed in just one line? Here is what that script looks like.

Broken Script

delete from dbo.Customers --where createDate >= '20211201' where customerId = 99

As you can see from the resulting broken script listed above, instead of just deleting the record where customerId is equal to 99, you will now be deleting all the records in the Customers table. Yikes!

Tags: #SQL #SqlServer