sql 文件中的正则表达式

regular expressions on sql file

我需要对数百个 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

为此,我会使用 Notepad++

有什么提示或想法吗?

搜索:

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

替换为:

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

您可以使用此正则表达式替换:

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

已测试: