当前位置:  开发笔记 > 编程语言 > 正文

无法运行整个T-SQL查询,但部分我可以

如何解决《无法运行整个T-SQL查询,但部分我可以》经验,为你挑选了1个好方法。

盖伊,我对我的这个问题有疑问.我已经尽我所能,现在它已经走到了尽头.如果我运行2分离它完美的工作,但一旦我选择整个事情运行作为片断它给我一个关于临时表已经存在的错误,即使我检查Temp表是否存在并放在最后和开始每个"批次"我会称之为.

我真的不知道发布什么部分的查询所以我只是要发布整个事情.如果有人能够让我了解它为什么这样做以及其他提示你可能会看到我做错了.

Use test
    IF OBJECT_ID('tempdb..#TEMP') IS NOT NULL DROP TABLE #TEMP

    IF OBJECT_ID('MetricsServerAudit') IS NOT NULL
        BEGIN 
            CREATE TABLE  #TEMP ([TIME] nvarchar(max) NULL,[DATE] nvarchar(max) NULL,[USER_LOGIN] nvarchar(max) NULL,[USER_NAME] nvarchar(max) NULL,[MODEL_NAME] nvarchar(max) NULL,[SCORECARD_IDENTIFIER] nvarchar(max) NULL,  [SCORECARD_NAME] nvarchar(max) NULL,[ELEMENT_IDENTIFIER] nvarchar(max) NULL,[ELEMENT_NAME] nvarchar(max) NULL,[SERIES_IDENTIFIER] nvarchar(max) NULL,[SERIES_NAME] nvarchar(max) NULL,[PERIOD_NAME] nvarchar(max) NULL,[ACTION_TYPE] nvarchar(max) NULL,[ACTION] nvarchar(max) NULL,[PREVIOUS_VALUE] nvarchar(max) NULL,[VALUE] nvarchar(max) NULL,[UNIT] nvarchar(max) NULL)

                    BULK INSERT #TEMP FROM 'C:\QPR_Logs\Audit\MetricsServerAudit.txt'
                    WITH (FIELDTERMINATOR ='\t', ROWTERMINATOR = '\r', FIRSTROW = 2, KEEPNULLS)

                    UPDATE #TEMP SET [DATE]= REPLACE(CONVERT(VARCHAR(11),[DATE],103),'/' ,'-') 
                    ALTER TABLE #TEMP ALTER COLUMN [DATE] DATE
                    UPDATE #TEMP SET [TIME] = '12:00:00' Where [TIME] = ''
                    UPDATE #TEMP SET [TIME] = REPLACE(REPLACE(REPLACE([TIME], CHAR(10), ''), CHAR(13), ''), CHAR(9), '')
                    UPDATE #TEMP SET [TIME] = REPLACE([TIME], '/', ':')
                    UPDATE #TEMP SET [TIME] = left([TIME], 8)
                    UPDATE #TEMP SET [DATE] = '2015-01-01' Where [DATE] is null


                    INSERT INTO [dbo].[MetricsServerAudit]([DateStamp],[TIME],[DATE],[USER_LOGIN],[USER_NAME],[MODEL_NAME],[SCORECARD_IDENTIFIER],[SCORECARD_NAME],[ELEMENT_IDENTIFIER],[ELEMENT_NAME],[SERIES_IDENTIFIER],[SERIES_NAME],[PERIOD_NAME],[ACTION_TYPE],[ACTION],[PREVIOUS_VALUE],[VALUE],[UNIT])
                    SELECT CONCAT([DATE],'', [TIME]) AS [DateStamp], [TIME],[DATE],[USER_LOGIN],[USER_NAME],[MODEL_NAME],[SCORECARD_IDENTIFIER],[SCORECARD_NAME],[ELEMENT_IDENTIFIER],[ELEMENT_NAME],[SERIES_IDENTIFIER],[SERIES_NAME],[PERIOD_NAME],[ACTION_TYPE],[ACTION],[PREVIOUS_VALUE],[VALUE],[UNIT]
                    FROM #TEMP
                    WHERE NOT EXISTS(SELECT [TIME] FROM [dbo].[MetricsServerAudit] WHERE [TIME] = [TIME])
                    DROP TABLE #TEMP
        END

Else --SEPERATOR 
    IF OBJECT_ID('tempdb..#TEMP') IS NOT NULL DROP TABLE #TEMP

    IF OBJECT_ID('MetricsServerAudit') IS NULL
        BEGIN 
            CREATE TABLE  MetricsServerAudit ([DateStamp] nvarchar(max) NULL, [TIME] nvarchar(max) NULL,[DATE] date NULL,[USER_LOGIN] nvarchar(max) NULL,[USER_NAME] nvarchar(max) NULL,[MODEL_NAME] nvarchar(max) NULL,[SCORECARD_IDENTIFIER] nvarchar(max) NULL,[SCORECARD_NAME] nvarchar(max) NULL,[ELEMENT_IDENTIFIER] nvarchar(max) NULL,[ELEMENT_NAME] nvarchar(max) NULL,[SERIES_IDENTIFIER] nvarchar(max) NULL,[SERIES_NAME] nvarchar(max) NULL,[PERIOD_NAME] nvarchar(max) NULL,[ACTION_TYPE] nvarchar(max) NULL,[ACTION] nvarchar(max) NULL,[PREVIOUS_VALUE] nvarchar(max) NULL,[VALUE] nvarchar(max) NULL,[UNIT] nvarchar(max) NULL)
        END
            IF OBJECT_ID('tempdb..#TEMP') IS NULL
                BEGIN
                    CREATE TABLE  #TEMP ([TIME] nvarchar(max) NULL,[DATE] nvarchar(max) NULL,[USER_LOGIN] nvarchar(max) NULL,[USER_NAME] nvarchar(max) NULL,[MODEL_NAME] nvarchar(max) NULL,[SCORECARD_IDENTIFIER] nvarchar(max) NULL,  [SCORECARD_NAME] nvarchar(max) NULL,[ELEMENT_IDENTIFIER] nvarchar(max) NULL,[ELEMENT_NAME] nvarchar(max) NULL,[SERIES_IDENTIFIER] nvarchar(max) NULL,[SERIES_NAME] nvarchar(max) NULL,[PERIOD_NAME] nvarchar(max) NULL,[ACTION_TYPE] nvarchar(max) NULL,[ACTION] nvarchar(max) NULL,[PREVIOUS_VALUE] nvarchar(max) NULL,[VALUE] nvarchar(max) NULL,[UNIT] nvarchar(max) NULL)

                    BULK INSERT #TEMP FROM 'C:\QPR_Logs\Audit\MetricsServerAudit.txt'
                    WITH (FIELDTERMINATOR ='\t', ROWTERMINATOR = '\r', FIRSTROW = 2, KEEPNULLS)

                    UPDATE #TEMP SET [DATE]= REPLACE(CONVERT(VARCHAR(11),[DATE],103),'/' ,'-') 
                    ALTER TABLE #TEMP ALTER COLUMN [DATE] DATE
                    UPDATE #TEMP SET [TIME] = '12:00:00' Where [TIME] = ''
                    UPDATE #TEMP SET [TIME] = REPLACE(REPLACE(REPLACE([TIME], CHAR(10), ''), CHAR(13), ''), CHAR(9), '')
                    UPDATE #TEMP SET [TIME] = REPLACE([TIME], '/', ':')
                    UPDATE #TEMP SET [TIME] = left([TIME], 8)
                    UPDATE #TEMP SET [DATE] = '2015-01-01' Where [DATE] is null                 


                    INSERT INTO [dbo].[MetricsServerAudit]([TIME],[DATE],[USER_LOGIN],[USER_NAME],[MODEL_NAME],[SCORECARD_IDENTIFIER],[SCORECARD_NAME],[ELEMENT_IDENTIFIER],[ELEMENT_NAME],[SERIES_IDENTIFIER],[SERIES_NAME],[PERIOD_NAME],[ACTION_TYPE],[ACTION],[PREVIOUS_VALUE],[VALUE],[UNIT])
                    SELECT [TIME],[DATE],[USER_LOGIN],[USER_NAME],[MODEL_NAME],[SCORECARD_IDENTIFIER],[SCORECARD_NAME],[ELEMENT_IDENTIFIER],[ELEMENT_NAME],[SERIES_IDENTIFIER],[SERIES_NAME],[PERIOD_NAME],[ACTION_TYPE],[ACTION],[PREVIOUS_VALUE],[VALUE],[UNIT]
                    FROM #TEMP
                    DROP TABLE #TEMP

                    UPDATE [dbo].[MetricsServerAudit] SET [DateStamp] = CONCAT([DATE],'', [TIME])
                    UPDATE [dbo].[MetricsServerAudit] SET [DateStamp] = REPLACE([DateStamp], '/', ':')
                    UPDATE [dbo].[MetricsServerAudit] SET [DateStamp] = CONVERT(datetime,(SUBSTRING([DateStamp],1,10)+' '+SUBSTRING([DateStamp],11,15)))
                    ALTER TABLE [dbo].[MetricsServerAudit] ALTER COLUMN [DateStamp] DATETIME
                END

通过else是我所说的分隔符所以它上面的所有东西都是"Batch1",它下面的一切都是"Batch2"

我得到的错误是:

Msg 2714, Level 16, State 1, Line 36
There is already an object named '#TEMP' in the database.

John Bell.. 5

ELSE 不是批处理分隔符,并且您使用但不删除的任何临时表都将抛出此错误.

除非您专门更改了批处理分隔符,否则应GO在批处理之间使用.这样,任何临时表都将在批处理分离之间删除.



1> John Bell..:

ELSE 不是批处理分隔符,并且您使用但不删除的任何临时表都将抛出此错误.

除非您专门更改了批处理分隔符,否则应GO在批处理之间使用.这样,任何临时表都将在批处理分离之间删除.

推荐阅读
个性2402852463
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有