Should you store your SQL Stored Procedures in Source Control?

前端 未结 17 1300
南方客
南方客 2021-02-05 18:33

When developing an application with lots of stored procedures, should you store them in some sort of source versioning system (such as source-safe, TFS, SVN)? If so, why? And is

相关标签:
17条回答
  • 2021-02-05 19:05

    Storing stored procedures is a great idea. Its a pain though. Just how do you get all that stuff into subversion? You can manually do it, but then its tedious and you end up not doing it at all.

    I use a tool from the subsonic project.

    sonic.exe version /server servername /db databasename /out outputdirectory 
    

    This command saves everything to 2 text files. One contains database schema, stored procs, user accounts, constraints, and primary keys. The other one contains the data.

    Now that you have these two files you can use subversion(cvs,source safe) to move it into source control.

    More info for using The Command Line Tool (SubCommander)

    0 讨论(0)
  • 2021-02-05 19:06

    Yes. All code should be stored in source control.

    Simply put, code is code and mistakes happen. It's nice to be able to go back and see what changed over time and be able to go back to those changes.

    We have to add it manually to a source control system, but you can create addons for the Sql Server the Management System. I haven't ever created one to automatically add it to source control, but I suppose you could. Also, all the code is stored in sql tables, so you could in theory create a process or something to go through the table(s) and retrieve all the code and commit it automatically.

    Update: I would always write extra code to check and see if the code exists and if it doesn't create a filler procedure and then the actual script do and alter procedure.

    IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE 
    id = OBJECT_ID(N'[dbo].[SomeStoredProcedure]') AND 
    OBJECTPROPERTY(id,N'IsProcedure') = 1)
    
    EXEC sp_executesql N'CREATE PROCEDURE [dbo].[SomeStoredProcedure] AS
    
    SELECT ''SPROC Template'''
    
    GO
    
    SET ANSI_NULLS ON
    
    GO
    
    SET QUOTED_IDENTIFIER ON
    
    GO
    
     ALTER PROCEDURE SomeStoredProcedure
    

    Doing a drop and recreate will remove all the user permissions you have setup for it.

    0 讨论(0)
  • 2021-02-05 19:09

    I recommend that you do store them. You never know when you'll need to rollback, or dig into logic you may have removed..

    Here's a good way to easily grab your Stored Procs into files that you can throw into whatever source control you desire..

    Stored Procedures to .sql files

    0 讨论(0)
  • 2021-02-05 19:10

    ABSOLUTELY POSITIVELY WITHOUT QUESTION NO EXCEPTIONS IN ALL PERPETUITY THROUGHOUT THE UNIVERSE YES!

    0 讨论(0)
  • 2021-02-05 19:14

    Most definitely yes. Then the question becomes how you store them in source control. Do you drop and recreate the stored procedure or just alter, do you add permissions at the end of the script or in a separate script. There was a post on Coding Horror a while back about the topic that I found interesting. Is Your Database Under Version Control?

    0 讨论(0)
  • 2021-02-05 19:14

    SQL is code. All code belongs under source code control.

    That is all.

    0 讨论(0)
提交回复
热议问题