美好的一天,我一直忙於創建一個表的查詢,如果它不存在,我完全滿意我所做的。當然可以更好一點,但我仍然是一個新手^^。我想要仔細檢查的是我的更新腳本,如果表存在,然後從文本文件更新它。這是迄今爲止我的腳本:想要仔細檢查我的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
任何人都可以告訴我的查詢,如果它是好的,如果有什麼我應該做的。歡迎任何建議!我在這裏學習。謝謝^^
來看,它在測試系統/測試數據或敷在交易和回滾 –