Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Stored Procedures to Version Control

Our team just experienced for the first time the hassle of not having version control for our DB. How can we add stored procedures at the very least to version control? The current system we're developing relies on SPs mainly.

like image 511
Jonn Avatar asked Mar 03 '10 09:03

Jonn


People also ask

Can you call a stored procedure from a UDF?

Is it possible to call a stored procedure in a user defined function in sql server, mysql or oracle ? In SQL Server: no, you cannot call stored procedures from inside functions.

How do I enable a stored procedure in SQL?

Use SQL Server Management StudioExpand Databases, expand the database in which the procedure belongs, and then expand Programmability. Expand Stored Procedures, right-click the procedure to grant permissions on, and then select Properties. From Stored Procedure Properties, select the Permissions page.

How do I use SQL version control?

Open SQL Server Management Studio and connect to a SQL Server instance. Right-click on your database in the Object Explorer pane and select "Connect to Version Control". This will open the "Connect Database to Version Control" dialog. Copy the https repository path from GitHub and paste it into VersionSQL.

Can we update using stored procedure?

A single stored procedure can be used to select, add, update, and delete data from a database table.


1 Answers

Background: I develop a system that has almost 2000 stored procedures.

The critical thing I have found is to treat the database as an application. You would never open an EXE with a hex editor directly and edit it. The same with a database; just because you can edit the stored procedures from the database does not mean you should.

Treat the copy of the stored procedure in source control as the current version. It is your source code. Check it out, edit it, test it, install it, and check it back in. The next time it has to be changed, follow the same procedure. Just as an application requires a build and deploy process, so should the stored procedures.

The code below is a good stored procedure template for this process. It handles both cases of an update (ALTER) or new install (CREATE).

IF EXISTS(SELECT name             FROM sysobjects            WHERE name = 'MyProc' AND type = 'P' AND uid = '1')    DROP PROCEDURE dbo.MyProc GO  CREATE PROCEDURE dbo.MyProc AS  GO 

However following sample is better in situations where you control access to the stored procedures. The DROP-CREATE method loses GRANT information.

IF NOT EXISTS(SELECT name             FROM sysobjects            WHERE name = 'MyProc' AND type = 'P' AND uid = '1')    CREATE PROCEDURE dbo.MyProc    AS    PRINT 'No Op' GO  ALTER PROCEDURE dbo.MyProc AS  GO 

In addition, creating a process to build the database completely from source control can help in keeping things controlled.

Create a new database from source control. Use a tool like Red Gate SQL Compare to compare the two databases and identify differences. Reconcile the differences.

A cheaper solution is to simply use the "Script As" functionality of SQL Management Studio and do a text compare. However, this method is real sensitive to the exact method SSMS uses to format the extracted SQL.

like image 140
Darryl Peterson Avatar answered Sep 16 '22 17:09

Darryl Peterson