从Dynamic SQL脚本将数据插入表中

时间:2016-07-21 14:56:10

标签: sql sql-server-2012 insert cursor dynamic-sql

我正在尝试运行此脚本:

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中引用的每个DB,并获取值并将它们插入到我的表中。

如果您需要更多信息来帮助我,请告诉我,我们将非常感谢您提供任何帮助。

1 个答案:

答案 0 :(得分:0)

您应该发布生成的查询,但我认为它看起来像:

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。因此,您应该首先使用@SQL语句(光标外)初始化insert。在游标内部,您可以使用SET @SQL = @SQL + ...,但不使用insert语句。

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