Incorrect Syntax Near GO, T-SQL EXEC()

前端 未结 2 466
鱼传尺愫
鱼传尺愫 2021-01-21 23:45

I am using the following script:

DECLARE @dbName NVARCHAR(20) = \'ABC\';
EXEC ( N\' USE \' + @dbName + \'
GO

-- Create Schema
CREATE SCHEMA meta
GO

-- Create L         


        
2条回答
  •  礼貌的吻别
    2021-01-22 00:24

    You can use following script for solution to your requirement with the help of unsupported undocumented stored procedure sp_MSForEachDB

    EXEC sp_MSForEachDB '
    Use [?]; 
    IF ''[?]'' = ''[ABC]'' 
    begin
        -- Create Schema
        exec sp_executesql N''CREATE SCHEMA meta''
    end
    '
    EXEC sp_MSForEachDB '
    Use [?]; 
    IF ''[?]'' = ''[ABC]'' 
    begin 
        -- Create Log Table
        CREATE TABLE meta.LogAudit(
        [EventDate] [datetime] NOT NULL DEFAULT (getdate()),
        [EventType] [nvarchar](64) NULL,
        )
    end'
    

提交回复
热议问题