从动态 SQL 脚本向 table 插入数据

Inserting data into a table from a Dynamic SQL script

我正在尝试 运行 这个脚本:

DECLARE @Client VARCHAR(50)
DECLARE @SQL VARCHAR(MAX)
DECLARE @DBReporting VARCHAR(500)
DECLARE @DBSignet VARCHAR(500)
DECLARE @databasename varchar(100)

    SET @SQL = ''

    DECLARE db_cursor CURSOR FOR
    SELECT  name 
    FROM sys.databases 
    WHERE name like '%reporting%' 
           AND NOT Name Like '%UAT%'
           AND NOT Name Like '%Test%'
           AND NOT Name Like '%Demo%'
           AND NOT Name like '%staging%'
           AND NOT Name like '%server%'
           AND state_desc <> 'offline'

    OPEN db_cursor
    FETCH NEXT FROM db_cursor INTO @databasename

    WHILE @@FETCH_STATUS = 0
    BEGIN

        SET @Client = REPLACE(REPLACE(@databasename, 'SourcingPlatform_', ''), '_Reporting', '')

        SET @DBSignet = 'SourcingPlatform_' + @Client + '_Signet_Tradeflow'
        SET @DBReporting = 'SourcingPlatform_' + @Client + '_Reporting'

        SET @SQL = @SQL + 'INSERT INTO STS_Branding.[dbo].[S2C_KeyStats]
           ([Project]
           ,[DataDate]
           ,[EventTypeName]
           ,[CountOfAllEvents]
           ,[CreatedWithinLast3Months]
           ,[CreatedWithinLast6Months]
           ,[CreatedWithinLast12Months])
     VALUES

        SELECT ''' + @Client + ''' AS Client, convert(date, getdate()), EventTypeName collate Latin1_General_CI_AS,
        count(id) as CountOfAllEvents,

        (select COUNT(e3.ID) 
        from    ' + @DBReporting + '..REPORTS_Sourcing_Event E3
        where   DATEDIFF(month,CreateDate, GETDATE()) <= 3
        and     E.EventTypeName = E3.EventTypeName) as CreatedLast3Months,

        (select COUNT(e6.ID) 
        from    ' + @DBReporting + '..REPORTS_Sourcing_Event E6
        where   DATEDIFF(month,CreateDate, GETDATE()) > 3
        and     DATEDIFF(month,CreateDate, GETDATE()) <= 6
        and     E.EventTypeName = E6.EventTypeName) as CreatedLast6Months,

        (select COUNT(e12.ID) 
        from    ' + @DBReporting + '..REPORTS_Sourcing_Event E12
        where   DATEDIFF(month,CreateDate, GETDATE()) > 6
        and     DATEDIFF(month,CreateDate, GETDATE()) <= 12
        and     E.EventTypeName = E12.EventTypeName) as CreatedLast12Months,

        (select COUNT(e13.ID) 
        from    ' + @DBReporting + '..REPORTS_Sourcing_Event E13
        where   DATEDIFF(month,CreateDate, GETDATE()) > 12
        and     E.EventTypeName = E13.EventTypeName) as CreatedOver12Months

        FROM ' + @DBReporting + '..REPORTS_Sourcing_Event E

        Group By EventTypeName

        UNION '

        FETCH NEXT FROM db_cursor INTO @databasename
    END

    CLOSE db_cursor   
    DEALLOCATE db_cursor

SET @sql =  substring(@sql, 0, LEN(@sql) - len('UNION ')) + ' ORDER BY Client, EventTypeName collate Latin1_General_CI_AS'

--PRINT @SQL
exec(@SQL)

但是,我遇到语法错误。

我打印了@SQL 变量,生成的代码看起来不错。我在这里错过了一些非常简单的东西吗?还是我偏离了我想要实现的目标?

我想要实现的是一个脚本,它遍历第一个 select 中引用的每个数据库并获取值并将它们插入我的 table。

如果您需要更多信息来帮助我,请告诉我,此时任何帮助都将不胜感激。

您应该 post 生成的查询,但我认为它类似于:

INSERT INTO STS_Branding.[dbo].[S2C_KeyStats]
           ([Project]
           ,[DataDate]
           ,[EventTypeName]
           ,[CountOfAllEvents]
           ,[CreatedWithinLast3Months]
           ,[CreatedWithinLast6Months]
           ,[CreatedWithinLast12Months])
VALUES -- Remove this, it's incorrect in combination with SELECT     
SELECT   (lots of selects)

UNION

INSERT INTO STS_Branding.[dbo].[S2C_KeyStats]
           ([Project]
           ,[DataDate]
           ,[EventTypeName]
           ,[CountOfAllEvents]
           ,[CreatedWithinLast3Months]
           ,[CreatedWithinLast6Months]
           ,[CreatedWithinLast12Months])

SELECT   (lots of selects)

这显然是不可能的,你要union selects 而不是insert。因此,您应该首先使用 insert 语句(在光标外)初始化 @SQL。在游标内,您可以像您已经在做的那样使用 SET @SQL = @SQL + ...,但没有 insert 语句。

此外,请注意 substring1 基于 SQL,而不是 0,例如 C#。