想要仔细检查我的T-Sql更新查询是否正确

时间:2015-12-08 07:07:31

标签: sql ssms bulkinsert

美好的一天,我一直忙于创建一个表格,如果它不存在,我对我所做的完全满意。当然它可能会好一点,但我仍然是一个新的^^。如果表存在,那么我想要仔细检查的是我的更新脚本,然后从文本文件更新它。到目前为止,这是我的脚本:

USE Test
GO
    IF OBJECT_ID('Final') 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:\Test\Test.txt'
                    WITH (FIELDTERMINATOR ='\t', ROWTERMINATOR = '0x0a', FIRSTROW = 2, KEEPNULLS)

                    UPDATE #TEMP SET [DATE]= REPLACE(CONVERT(VARCHAR(11),[DATE],103),'/' ,'-')
                    UPDATE #TEMP SET [DATE] = '2015-01-01' Where [DATE] is null
                    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)

                    INSERT INTO [dbo].[Final]([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].[Final] WHERE [TIME] = [TIME])
                    DROP TABLE #TEMP
        END

这是创建查询,如果它不存在:

USE Test
GO
    IF (OBJECT_ID('Final') IS NULL and OBJECT_ID('tempdb..#Temp') IS NULL)
        BEGIN 
            CREATE TABLE  Final ([DateStamp] nvarchar(max) NULL, [TIME] nvarchar(max) NULL,[DATE] date NULL,[USER_LOGIN] nvarchar(max) NULL,[USER_NAME] nvarchar(max) NULL,[PRODUCT_NAME    ] nvarchar(max) NULL,[MODEL_NAME    ] nvarchar(max) NULL,[OPERATION ] nvarchar(max) NULL,[CLIENT_IP ] nvarchar(max) NULL,[OBJECT    ] nvarchar(max) NULL,[VIEW_TYPE ] nvarchar(max) NULL,[VIEW] nvarchar(max) NULL)
            CREATE TABLE  #Temp ([TIME] nvarchar(max) NULL,[DATE] nvarchar(max) NULL,[USER_LOGIN] nvarchar(max) NULL,[USER_NAME] nvarchar(max) NULL,[PRODUCT_NAME   ] nvarchar(max) NULL,[MODEL_NAME    ] nvarchar(max) NULL,[OPERATION ] nvarchar(max) NULL,[CLIENT_IP ] nvarchar(max) NULL,[OBJECT    ] nvarchar(max) NULL,[VIEW_TYPE ] nvarchar(max) NULL,[VIEW] nvarchar(max) NULL)

                    BULK INSERT #Temp FROM 'C:\Test\Test.txt'
                    WITH (FIELDTERMINATOR ='\t', ROWTERMINATOR = '0x0a', MAXERRORS = 5, FIRSTROW = 2, KEEPNULLS)

                    UPDATE #Temp SET [DATE]= REPLACE(CONVERT(VARCHAR(11),[DATE],103),'/' ,'-') 
                    UPDATE #Temp SET [DATE] = '2015-01-01' Where [DATE] = ''
                    UPDATE #Temp SET [DATE] = '2015-01-01' Where [DATE] is null 
                    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)

                    INSERT INTO [dbo].[Final]([DateStamp],[TIME],[DATE],[USER_LOGIN],[USER_NAME],[PRODUCT_NAME  ],[MODEL_NAME   ],[OPERATION    ],[CLIENT_IP    ],[OBJECT   ],[VIEW_TYPE    ],[VIEW])
                    SELECT CONCAT([DATE],'', [TIME]) AS [DateStamp],[TIME],[DATE],[USER_LOGIN],[USER_NAME],[PRODUCT_NAME    ],[MODEL_NAME   ],[OPERATION    ],[CLIENT_IP    ],[OBJECT   ],[VIEW_TYPE    ],[VIEW]
                    FROM #Temp
                    DROP TABLE #Temp

                    UPDATE [dbo].[Final] SET [DateStamp] = CONVERT(DATETIME,(SUBSTRING([DateStamp],1,10)+' '+SUBSTRING([DateStamp],11,15)))
                    ALTER TABLE [dbo].[Final] ALTER COLUMN [DateStamp] DATETIME
        END

任何人都可以告诉我,如果我是按照正确的程序完成的,我对此部分有点不确定以及在更新查询中将其添加到何处。

UPDATE [dbo].[Final] SET [DateStamp] = CONVERT(DATETIME,(SUBSTRING([DateStamp],1,10)+' '+SUBSTRING([DateStamp],11,15)))
   ALTER TABLE [dbo].[Final] ALTER COLUMN [DateStamp] DATETIME

有人可以告诉我有关查询的信息以及它是否有用以及是否还有其他问题我应该做。欢迎任何建议!我在这里学习。谢谢^^

1 个答案:

答案 0 :(得分:0)

我认为你可以做一些更直截了当的事情。 我已经重写了你的脚本(但我没有测试,因为我没有你的test.txt文件)

实际上,我做了一些改变:

  • 使用表变量而不是临时表。您无需删除或检查它是否已存在。它是一个变量,并将在最后被销毁 你的剧本
  • 使用sys.tables表中的直接选择
  • 检查表的存在
  • 用convert语句替换你的alter语句。您应该避免为业务规则更改表。它可能会产生错误,无法回滚。
  • 使用交易。如果脚本引发错误,则至少不会创建表Final

    Declare @FinalTableExist bit  = 0;
    IF Exists (Select * from sys.tables where name = 'Final')
        Set @FinalTableExist = 1;
    
    Declare @TmpTable (
        [TIME] nvarchar(max) NULL,
        [DATE] nvarchar(max) NULL,
        [USER_LOGIN] nvarchar(max) NULL,
        [USER_NAME] nvarchar(max) NULL,
        [PRODUCT_NAME   ] nvarchar(max) NULL,
        [MODEL_NAME    ] nvarchar(max) NULL,
        [OPERATION ] nvarchar(max) NULL,
        [CLIENT_IP ] nvarchar(max) NULL,
        [OBJECT    ] nvarchar(max) NULL,
        [VIEW_TYPE ] nvarchar(max) NULL,
        [VIEW] nvarchar(max) NULL)
    
    if (@FinalTableExist = 0)
    Begin
        Begin try
    
            CREATE TABLE  Final (
                [DateStamp] DateTime NULL, 
                [TIME] time NULL,
                [DATE] date NULL,
                [USER_LOGIN] nvarchar(max) NULL,
                [USER_NAME] nvarchar(max) NULL,
                [PRODUCT_NAME    ] nvarchar(max) NULL,
                [MODEL_NAME    ] nvarchar(max) NULL,
                [OPERATION ] nvarchar(max) NULL,
                [CLIENT_IP ] nvarchar(max) NULL,
                [OBJECT    ] nvarchar(max) NULL,
                [VIEW_TYPE ] nvarchar(max) NULL,
                [VIEW] nvarchar(max) NULL)
    
    
                BULK INSERT @TmpTable FROM 'C:\Test\Test.txt'
                WITH (FIELDTERMINATOR ='\t', ROWTERMINATOR = '0x0a', MAXERRORS = 5, FIRSTROW = 2, KEEPNULLS)
    
                UPDATE @TmpTable SET [DATE]= REPLACE(CONVERT(VARCHAR(11),[DATE],103),'/' ,'-') 
                UPDATE @TmpTable SET [DATE] = '2015-01-01' Where [DATE] = ''
                UPDATE @TmpTable SET [DATE] = '2015-01-01' Where [DATE] is null 
                UPDATE @TmpTable SET [TIME] = '12:00:00' Where [TIME] = ''
                UPDATE @TmpTable SET [TIME] = REPLACE(REPLACE(REPLACE([TIME], CHAR(10), ''), CHAR(13), ''), CHAR(9), '')
                UPDATE @TmpTable SET [TIME] = REPLACE([TIME], '/', ':')
                UPDATE @TmpTable SET [TIME] = left([TIME], 8)
    
                INSERT INTO [dbo].[Final](
                        [DateStamp],
                        [TIME],
                        [DATE],
                        [USER_LOGIN],[USER_NAME],[PRODUCT_NAME  ],[MODEL_NAME   ],[OPERATION    ],[CLIENT_IP    ],[OBJECT   ],[VIEW_TYPE    ],[VIEW])
                SELECT Convert(Datetime, CONCAT([DATE],' ', [TIME])),
                       Convert(Time, [TIME]),
                       Convert(Date, [DATE]),
                        [USER_LOGIN],[USER_NAME],[PRODUCT_NAME    ],[MODEL_NAME   ],[OPERATION    ],[CLIENT_IP    ],[OBJECT   ],[VIEW_TYPE    ],[VIEW]
                FROM @TmpTable
            End Try
            BEGIN CATCH
                SELECT ERROR_NUMBER() AS ErrorNumber,
                        ERROR_SEVERITY() AS ErrorSeverity,
                        ERROR_STATE() as ErrorState,
                        ERROR_PROCEDURE() as ErrorProcedure,
                        ERROR_LINE() as ErrorLine,
                        ERROR_MESSAGE() as ErrorMessage
            END CATCH;