2015-04-26 71 views
1

我需要對數百個SQL腳本文件執行搜索和替換操作。這個想法是把這樣的:sql文件上的正則表達式

CREATE PROCEDURE [dbo].[proc_whatever] 
    (
     @id INT, 
     @parameter VARCHAR(1) 
    ) 
AS 
    BEGIN 
     ... 
    END 

進入這個:

IF OBJECT_ID('[dbo].[proc_whatever]') IS NULL 
     EXEC('CREATE PROCEDURE [dbo].[proc_whatever] AS SET NOCOUNT ON;') 
GO 

ALTER PROCEDURE [dbo].[proc_whatever] 
    (
     @id INT, 
     @parameter VARCHAR(1) 
    ) 
AS 
    BEGIN 
     ... 
    END 

要做到這一點,我會用記事本++

任何建議或想法?

+0

感謝所有回答的人。我非常感謝你的幫助 – abx78

回答

1

你可以使用這個表達式替換:

Find What:  CREATE\s+PROCEDURE\s+(\[.*?\]\.\[.*?\])\s+\(\s+(@id INT,\[email protected] VARCHAR\(\d+\)\s+)\s+\) 
Replace With: IF OBJECT_ID\('$1'\) IS NULL\r\n EXEC\('CREATE PROCEDURE $1 AS SET NOCOUNT ON;'\)\r\nGO\r\n\r\nALTER PROCEDURE $1\r\n \(\r\n  $2\) 

測試:

enter image description here

1

搜索:

CREATE PROCEDURE ([\[\]a-zA-Z_.]+)\s*\(

取代:

IF OBJECT_ID \('$1'\) IS NULL \n EXEC \('CREATE PROCEDURE $1 AS SET NOCOUNT ON;'\) \n GO \n ALTER PROCEDURE $1 \(