Should you store your SQL Stored Procedures in Source Control?

前端 未结 17 1302
南方客
南方客 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: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.

提交回复
热议问题