Learn more about SQL Server tools

mssqltips logo
 

Tutorials          DBA          Dev          BI          Career          Categories          Webcasts          Whitepapers          Today's Tip          Join

Tutorials      DBA      Dev      BI      Categories      Webcasts

DBA    Dev    BI    Categories

 

Using Source Control with SQL Server Management Studio


By:   |   Read Comments   |   Related Tips: More > DBA Best Practices

Attend these FREE MSSQLTips webcasts >> click to register


Problem

In a previous tip, Getting started with source control for SQL Server, we looked at how to setup source control to be used with SQL Server Management Studio.  In this tip, we look at how to use source control with SSMS.

Solution

This tip is aimed to show how to use source control with Microsoft Team Foundation Server 2012 in SQL Server Management Studio (SSMS) to check-out and check-in code for co-development amongst SQL Developers. In order to fully follow this tip, please refer to my previous tip - Getting started with source control for SQL Server.

Using Source Control Plug-in Directly from SSMS

First, let’s check to see if the Source Control Plug-In for SSMS has been installed.  In SSMS, on the menus click on Tools > Options and make sure that Team Foundation Server plug-in appears in the “Current source control plug-in” drop down as shown below.

Team Foundation Server Plug-in in SQL Server Management Studio

Step 1: Open Project

From the SSMS menu, click on File > Source Control then open our first project by clicking on: “Open from Source Control…” as shown below.

Source Control Menu in SQL Server Management Studio

The first time we try to open a project controlled by source control, we have to select our source control server and click OK as shown below.

Connect to a Team Foundation Server in SQL Server Management Studio

The selection of our first project is quite simple, just click on the folder that contains our files and click OK.

Step 2: Check-out Procedure

Our project consists of two T-SQL files, please note the lock icon on the left side of the file name. This icon indicates that our files have not been check-out yet. In order to modify them we will have to check them out first.

Files in Source Control in the SSMS Object Explorer

Right click on the file that we want to edit and select "Check Out for Edit".

Check Out for Edit option in SQL Server Management Studio

During the check-out process we are free to enter a comment. Please note that the check-out dialog box is a Source Control Dialog Box.

Dialog box to enter comment for Source Control in SSMS

Once the file has been checked out, we can see a red tick on the left side of the file name in Solution Explorer. For the sake of this exercise, let’s add two lines of code (SELECT @@SERVERNAME and @@VERSION) to the file as shown below.

Add code to file in source control

Step 3: Check-in Procedure

In order to save our changes and make them visible to the rest of the developers, we need to check-in our file. Right click on the file name and select "Check In" as shown below.

Check-in a file to Source Control in SSMS

Please note that the source control "Check-In" dialog box will pop-up for us to enter a change comment before clicking on "Check In". At this point comments play an important role, they allow the development team to see what changes were applied by the last person who worked on the code.

Check-in a file into Source Control

Step 4: View Change History

If we want to see all the changes that have been applied to a specific file we can right click on the file name and select "View History" as shown below.

View History for Source Controlled File in SSMS

The Source Control dialog box, will show the date that the file was modified, the User (developer) that modified the file and comments pertaining to the code that was modified.

View History of Comments in Source Control

Conclusion

The main advantages of using Source Control are:

  • Share code development
  • Change track history
  • Ability to restore code for a specific change in time
  • Automatic code backup by the Source Control Application
Next Steps


Last Update:


signup button

next tip button



About the author
MSSQLTips author Matteo Lorini Matteo Lorini is a DBA and has been working in IT since 1993. He specializes in SQL Server and also has knowledge of MySQL.

View all my tips
Related Resources





Post a comment or let the author know this tip helped.

All comments are reviewed, so stay on subject or we may delete your comment. Note: your email address is not published. Required fields are marked with an asterisk (*).

*Name    *Email    Notify for updates 


SQL tips:

*Enter Code refresh code     



Learn more about SQL Server tools