Disclosure: This article may contain affiliate links. When you purchase, we may earn a small commission.

Top 5 SQL Server Management Studio Tips for Programmers and DBAs

Hello all, we all love tips? isn't it? Well, I certainly do, they not only make life easier on work but also gives us a joy of discovery. Every time I discover a not-so-trivial tip I feel happy and become more productive and that's why I am always in hunt of useful tips which can help me in my work. So, when I started working with Microsoft SQL Server, it was no different. I started from scratch so learning tips was not on card but after spending a couple of months I realized that I can do a lot better by applying some of the general SQL tips I have learned over the years and learning few tips from colleagues and DBAs which helps us with database. 

This article is collection of such tips. Even though the title suggests 10 tips, it doesn't have that many now, though it will definitely have that many and more in future. 

Even though I am good at discovering and learning tips but I am poor at documenting them and equally good at forgetting them. I have thought about this article for a long time and even wrote many tips to include in this one but I somehow lost them. They may be berried somewhere in those unpublished drafts or manuscripts which I wrote. 

So, finally I decided to push whatever I remember top of my head now and will update and include more tips later. At least whatever I will put will help someone in their work and save some of their time and may even provide a bit of joy of discovering these tips. 

Though, there is no criteria on this tips they are generally help you in your job. Some of the tips will help you to write better and faster SQL queries while other will make it easier to work in SQL Server Management Studio, my favorite tool to work with SQL Server database. 

Btw, learning tips only help if you have basic knowledge, if you don't know SQL Server at all or a beginner, I suggest you to join some comprehensive courses like Introduction to SQL Server before jumping into these tips. 



5 Practical SQL Server Tips for Programmers and DBAs

Anyway, without wasting anymore of your time, here are some of my practical tips to work better with Microsoft SQL Server and Management Studio:

1. Use Alias

Always use alias while writing SQL query, particularly with SQL Server Management as it not only reduces the length of the query and makes it easier to refer to the data with just one or two letter, but also it helps SQL Editor like SSMS in Code Completion. For example, SQL Server Management Studio will show all the columns from table which further helps you to write query.

If you write below query:

select COUNT(*) from Instruments.dbo.OptionsESM o where o.Disabled=0 

when you type o. SSMS will show all the columns. This is super useful when your table has large number of columns


2. Avoid database name on SQL Scripts

Avoid prepending database name in SQL query while writing them in script. This will allow you to run the exact same address into other copies of same database for testing. For example, if you want to run your SQL queries on Accounts table in Production but before that you want to test it the prod copy of database to avoid any syntax errors. 

Most likely you will ask your DBA to copy prod database into UAT server with names like Accounts_PROD_DATE, now if you have used database names like [Accounts].[dbo].[Customer] then you need to change that line all over the place in script, which is both error prone and risky. 

If you have just written [dbo].[Customer] then you can just write click on the database in SQL Server Management studio and copy the whole script and run it, without any change. 

Good Example:

alter table [dbo].[Customer] add SubAccountId bigint null

Bad Example

alter table [Accounts].[dbo].[Customer] add SubAccountId bigint null


3. Extracting Stored Procedure code

Sometime you need to copy the proc from one database to other database like from UAT to Production. In order to get the SQL to create the stored proc, I used to go to the database in SQL Server Management Studio, then find the proc under "Programmability" node, look for the proc, which can be very painful if you have hundreds of proc, and then right click on the proc and select "Script Stored Procedure As" - > "Create to" or "Alter to" depending upon whether its a new proc or existing proc. This method works but it is too time consuming. 

The better way to get the code for a stored procedure is to use the "sp_helptext" system proc, which will give you the code instantly rather than you look for. Just run it like below:

exec sp_helptext usp_AccountByCustomerId_Get

This will give you the proc code like below:

REATE PROCEDURE [dbo].[usp_AccountByCustomerId_Get] 

This is fast and good and work all the time. But, it always give you CREATE PROCEDURE, hence if you have to run into production and stored procedure already exists then you need to change CREATE to ALTER before running. 


4. Use SELECT to generate dynamic queries

Sometime we need to copy data from one database to another. AFAIK, there is no direct way where you can select the data, right click and export as insert into statement. 

If there is then please let us know. You need to write a script for yourself to do this, for this you need to know all columns and their data types. This can be obtained by using sp_help stored procedure. 

In such cases, SELECT command can be really useful for generating queries with data from a database. For example, if you want to copy some rows from a table in one environment to other like DEV to UAT then you can do that by using SELECT statements. 

For example, suppose you need to copy 100s of new books from your DEV to UAT database for testing? You don't have insert statement written for you and you don't want to write them by hands, in this case you can use SELECT queries like below to generate those insert statement using DEV data:

select 'insert into [MyUATBooksTable] (ISBN, Title, User) values (' 
+ CAST(p.ISBN as VARCHAR) 
+ ', '
+ p.Title 
+ ''', CURRENT_USER)'
from MyDEVBooksTable p where Title like "%SQL%"

This will generate insert statements which you can execute in your UAT database. 

Top 5 SQL Server Management Studio Tips for Programmers and DBAs


5. Use BEFORE and AFTER for verifying updates

If you are updating data on your database for a limited number of records like 10 to 15 then you can use "BEFORE" and "AFTER" with SELECT clause to print the list before and after the change. You can use this to verify and compare the data updates. 

Though, I always prefer an automated test to confirm the data integrity, this quickly way is also great when you are updating a limited number of records in UAT or Production environment.

Here is the example where I am updating price of all Josh Bloch book in my Books data to give you 50% discount

select 'BEFORE', Title, Author, Price from Book where Author="Josh Bloch"

update Book set Price=20 where Author="Josh Bloch"

select 'AFTER', Title, Author, Price from Book where Author="Josh Bloch"


That's all about the best practical SQL Server tips for programmers and DBAs. I knew more tips but I am also really good at forgetting them. Hopefully this article will now encourage me and provide a single place to write whatever SQL Server tip I learn along the way. If you have a SQL Server tip which have helped you in your work, feel free to share with us. 

Other SQL Articles and Tutorials You may like
  • How to join three tables in one single SQL query (solution)
  • Write a SQL query to find all table names on a database in MySQL (solution)
  • 5 Courses to learn Database and SQL Better (courses)
  • The real difference between WHERE and HAVING clause in SQL? (answer)
  • Top 5 Courses to learn Microsoft SQL Server in-depth (courses)
  • How to migrate SQL queries from Oracle to SQL Server? (answer)
  • Top 5 Websites to learn SQL online for FREE? (resource)
  • What is the difference between UNION and UNION ALL in SQL? (answer)
  • Write a SQL query to copy or backup a table in MySQL (solution)
  • How do you find the duplicate rows in a table on a database? (solution)
  • 5 Advanced SQL books to level up your SQL skills (books)
  • Top 5 Courses to learn MySQL Database for Beginners (Courses)
  • What is the difference between View and Materialized View in Database? (answer)
  • Difference between clustered and non-clustered indexes in SQL? (answer)
  • Difference between Primary and Candidate key in the table? (answer)
  • 5 Courses to learn Oracle and Microsoft SQL Server database (courses)
  • Difference between Self and Equi Join in SQL? (answer)
  • 4 Free Books to learn Microsoft SQL Server database (books)
  • Top 5 Websites to learn SQL online for FREE? (websites)
  • 5 Free Courses to learn Database and SQL (free courses)
  • 5 Free Courses to learn Oracle and SQL Server? (courses)
  • 5 Free Courses to learn T-SQL and SQL Server for Beginners (Courses)
  • Difference between the Unique and Primary keys in the table? (answer)
  • Top 5 Courses to learn PostgreSQL in-depth (courses)
  • 5 Free Courses to learn T-SQL and SQL Server for Beginners (Courses)
  • Difference between Primary and Foreign keys in the table? (answer)
Thanks for reading this article so far. If you like these SQL Server tips then please share with your friends and colleagues. If you have any questions or feedback then please drop a note. If you have SQL Server or SQL Server Management Studio tip, feel free to share with us by dropping a note. 

As I have said before, We all can learn from each other and remember, each reader sharing a tip, we would have 100s of useful practical tips on our sleeves.

2 comments :

Anonymous said...

Your links "https://javarevisited.blogspot.com/2022/11/top-5-sql-server-management-studio-tips.html" do not open in Apple Macintosh OS 13.0.Safari 16.1 .

javin paul said...

Thanks for pointing out, are you getting any error? what does console logs says?

Post a Comment