2015-12-08 43 views
0

美好的一天,我一直忙於創建一個表的查詢,如果它不存在,我完全滿意我所做的。當然可以更好一點,但我仍然是一個新手^^。我想要仔細檢查的是我的更新腳本,如果表存在,然後從文本文件更新它。這是迄今爲止我的腳本:想要仔細檢查我的T-Sql更新查詢是否正確

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 

任何人都可以告訴我的查詢,如果它是好的,如果有什麼我應該做的。歡迎任何建議!我在這裏學習。謝謝^^

+0

來看,它在測試系統/測試數據或敷在交易和回滾 –

回答

0

我認爲你可以做更直接的事情。 我返工你的紙條(但我沒有測試,因爲我沒有你的test.txt文件)

其實,我做了修改放入系統:

  • 使用表變量代替一個臨時表。您不需要刪除或檢查它是否已經存在。這是一個變量,並將在 腳本
  • 年底被破壞檢查表是否存在等用一個簡單的選擇在SYS.TABLES表
  • 與轉換語句來替換你的ALTER語句。您應該避免爲業務規則更改表。它可能會產生錯誤,無法回滾。
  • 使用交易。如果腳本產生錯誤,至少表最終將不會創建

    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; 
    
+0

嗨塞巴斯蒂安,我得到了一個很少的錯誤,但修復它們,剩下2個。 1是:'Msg 102,Level 15,State 1,Line 15 '@ TmpWASUserAudit'附近的語法不正確。' 第二個是:'Msg 102,Level 15,State 1,Line 37 ';'這就是結尾。 –

+0

Thx Christoph :)如果您同意,請將我的評論標記爲anwser :) –