我真的不确定为什么我收到这个错误,因为如果Table不在那里我创建它并且如果表存在那么它不应该进入这个查询部分那么为什么它要去再次进入它并尝试进行转换?
这是整个代码:
Use test
GO
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
GO --SEPERATOR
IF (OBJECT_ID('MetricsServerAudit') IS NULL and OBJECT_ID('tempdb..#TEMP') 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)
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
DROP TABLE #TEMP
IF (NOT EXISTS(select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = '[DateStamp]' AND DATA_TYPE = 'DATETIME' AND TABLE_NAME = 'MetricsServerAudit'))
BEGIN
UPDATE [dbo].[MetricsServerAudit] SET [DateStamp] = CONVERT(DATETIME,(SUBSTRING([DateStamp],1,10)+' '+SUBSTRING([DateStamp],11,15)))
ALTER TABLE [dbo].[MetricsServerAudit] ALTER COLUMN [DateStamp] DATETIME
END
END
这是出现错误的部分:
UPDATE [dbo].[MetricsServerAudit] SET [DateStamp] = CONVERT(DATETIME,(SUBSTRING([DateStamp],1,10)+' '+SUBSTRING([DateStamp],11,15)))
它位于整个代码的第4行。
这是完整的错误:
Msg 8116,Level 16,State 1,Line 51
对于子字符串函数的参数1,参数数据类型datetime无效。
任何人都可以告诉我为什么会这样做以及如何解决它?
答案 0 :(得分:1)
这是一个编译错误。如果MetricsServerAudit.DateStamp存在并且是日期时间,则此代码不会编译,因为子字符串不允许将datetime作为其第一个参数。您的选择是:
CONVERT(DATETIME, SUBSTRING(CONVERT(VARCHAR, [DateStamp])..