开发者

How to create a duplicate copy of a SQL Server stored proc or view with a new name?

开发者 https://www.devze.com 2023-03-30 07:44 出处:网络
Suppose I have a stored procedure proc_MyTestProc. I want to create a backup copy of this proc called proc_MyTestProc_{timestamp}, and create a new instance of this proc in its place. Here is my curr

Suppose I have a stored procedure proc_MyTestProc.

I want to create a backup copy of this proc called proc_MyTestProc_{timestamp}, and create a new instance of this proc in its place. Here is my current method:

  1. Run sp_rename on proc_MyTestProc, appending the timestamp.
  2. Run the new CREATE script for the new version of proc_MyTestProc.

The problem with this method is that it doesn't work with replication. The original procedure retains the same object ID. The procedure created in step #2 is what I want to be replicated to another DB, yet it has a new object ID, so it doesn't replicate.

Is there an alternative to SP_RENAME that creates a copy of the object with a new object ID?

I am running开发者_JAVA技巧 SQL Server 2005.


DECLARE @old VARCHAR(255),
        @new VARCHAR(255),
        @sql NVARCHAR(MAX),
        @myid UNIQUEIDENTIFIER

SET @myid = NEWID();


SELECT @old = 'invoice_validation_sp', @new = @old + CONVERT(VARCHAR(20),GETDATE(), 112)

SELECT @sql = REPLACE(OBJECT_DEFINITION(OBJECT_ID),@old, @new) 
FROM sys.procedures 
WHERE name = @old

EXECUTE sp_executesql @sql

You can always do something like this...

0

精彩评论

暂无评论...
验证码 换一张
取 消