Go to DotNetFunda.com
 Online : 695 |  Welcome, Guest!   Login
 
Home > Articles > Sql Server > Source control for existing stored procedures (Continued..)

  • Download the OOPS, ASP.NET and ADO.NET online training sessions videos and related document for FREE, click here.

Submit Article | Articles Home | Search Articles |

Source control for existing stored procedures (Continued..)

 Posted on: 11/5/2007 8:49:27 AM by Deeraj | Views: 3978 | Category: Sql Server | Level: Advance | Print Article
In my previous article, http://www.dotnetfunda.com/articles/article20.aspx, I have demonstrated on how source control can be made available on stored procedures during the initial stages. Now, let us consider a scenario where source control was lacking for stored procedures for an online database which already was in production.

.NET Training Videos!
Buy online comprehensive training video pack just for $35.00 only, see what's inside it.

Please do read the complete article before implementing the same.

Now due to change requests/enhancements we may need to alter stored procedures and in order to keep track of all the modifications, we need source control. This source control can be accomplished either using DDL trigger (More about DDL trigger in my subsequent article(s)) or the way this article explains in a moment.

Consider we have a few stored procedures in a database, and as a DBA I need to track all the changes to any of the stored procedures. The challenge lies in writing the source code of a stored procedure to a file and the file name should be same as the stored procedure name. Once the files are available the same procedure as demonstrated in my earlier article, http://www.dotnetfunda.com/articles/article20.aspx can be used to achieve the needful task with a minor change on the implementation procedure.

I need to make use of copy and paste functionality (People call this re-usability and one of my friend calls it code adaptability :) ). I need to open the stored procedure, copy all the code, open a new query editor paste the code and save the file name as whatever was my stored procedure''s name. This would have been an easier task if my database had just around 10-20 stored procedures. But, real-time database demands more stored procedures, multiples of 100 or may be more. In this scenario, it will be a tedious job to perform copy+paste on every stored procedure.

SQL Server 2005 comes up with a nice little tool, bcp. Yes, bcp meaning Bulk copy utility. I made use of this utility to accomplish my task. This utility has saved enormous amount of labor involved in using/adapting copy+paste functionality. Now is the time to get into the details.

Execute the following dynamic query in one of your sql query editor window.

select 'bcp "SELECT routine_definition from dpi_development.information_schema.routines where routine_name=' + '''' + routine_name + '''' + '" ' + 'QueryOut d:\dpi\' + routine_name + '.Sql -S alexis\sqlsrv01 -U dpi -P Value*() -c'

from information_schema.routines order by routine_name


The above query would fetch the routine definitions from the catalog view, information_schema.routines. By making use of bcp we then save the results to a file with the same name as the stored procedure name.

In the above query,

bcp – bulk copy utility
dpi_development – Name of the database
information_schema.routines – catalog view that holds definitions of all the stored procedures in a given database.
QueryOut – Parameter to bcp, which specifies the location where the output will be saved.
S – Parameter to bcp, which specifies the SQL Server 2005.
U - Parameter to bcp, which specifies the UserName used to connect to the server
P - Parameter to bcp, which specifies the Password used to connect to the server
c - Parameter to bcp, which specifies the character type. Leave blank to have the default character type.
T – Parameter to bcp, which is meant for a trusted connection to SQL Server.

The above query would fetch, the following output. However a glimpse of the same was shown below for the reasons of demonstration.



Now copy the records in the results pane and open the Visual Studio .NET 2005 command prompt as shown in the picture below.



Paste the results in the command prompt. One should see the screen as follows. Forget not to delete the files with the same name including extension at the location specified for the bcp parameter, QueryOut. Otherwise the bcp utility fails to save the file.



Now open the location, d:\temp to view the .sql files stored under the sp''s name.
Once the sql files are available, Create a new project, as explained in, http://www.dotnetfunda.com/articles/article20.aspx,

Right click on the project name and choose Add -> Existing Item.



Browse to the directory where the .sql files have been saved as shown in the following figure and click on Add.



The solution explorer should now contain the newly added stored procedure and the status should be checked-out.



OK now we are all set for a usual source control. Forget not to save the file once the development/modification is done. Press F5 for the changes to take affect in the database.

Happy Development and safe source control :).
If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Interesting?   Share and Bookmark this kick it on DotNetKicks.com


Experience:4 year(s)
Home page:
Member since:Monday, October 29, 2007
Level:Starter
Status: [Member]
Biography:Qualification: BCA, MScIS, MDCA
 Latest post(s) from Deeraj

   ◘ Recursive FTP folder deletes in .NET 2.0 posted on 8/26/2008 2:16:16 PM
   ◘ Generating XML from relational database tables posted on 8/4/2008 8:25:14 AM
   ◘ ToolTip for List Items posted on 7/24/2008 4:16:14 AM
   ◘ Debugging Stored Procedures in SQL Server 2005 posted on 11/12/2007 6:56:44 AM
   ◘ Tracking Object (Table/Function/Stored Procedure etc.,) changes in SQL Server 2005 posted on 11/7/2007 5:53:44 AM


 Responses
Posted by: Meg_s | Posted on: 09 Jul 2008 08:18:39 AM

Hi Deeraj

The article for adding Source control for existing stored procedures was a very helpful starting point in setting up SourceSafe for me. But I did face one issue. All the stored procedures that were created using the bcp utility were truncated!

Has anyone had a similar problem? Possible solutions/workarounds??

Thanks

Posted by: Pvbabu | Posted on: 04 Nov 2008 09:16:35 AM

This is very nice article. I am having two issues
1) When I copy and paste query, error.
2) I have copyied bcp "SELECT routine_definition from information_schema.routines where routine_name= 'p_DeleteFund'" QueryOut C:\_Venu\Scripts\p_DeleteFund.Sql -S (local) -U sa -P 007 -T -C VSCommand promt. IT is asking of series of questions
The file is create but not copied stored procedure into file.
Can you please help me?

Posted by: Deeraj | Posted on: 10 Nov 2008 09:47:06 AM

In your Line of Code the database name is missing. You should prefix information_schema.routines with yourdatabasename.information_schema.routines. Because bcp doesnot have a switch to specify database name separately.

So your query should be
bcp "SELECT routine_definition from <YOUR_DATABASE_NAME_GOES_HERE>.information_schema.routines where routine_name= 'inser_order'"
QueryOut C:\_Venu\Scripts\p_DeleteFund.Sql -S (local) -U sa -P 007 -T -C

HTH.

-Dheeraj

Posted by: Prashanthnp@spinc.org | Posted on: 27 Aug 2009 11:57:43 AM

The file is getting created with the stored procedure name. But the file contents are missing .
Any guess what may be the problem ?

Submit Article

About Us | The Team | Advertise | Contact Us | Feedback | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found copied contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
All rights reserved to DotNetFunda.Com. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
(Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher)