SQL查询转换错误

时间:2015-10-30 10:22:50

标签: sql tsql sql-server-2012

我在T-SQL查询上收到错误。

  

Msg 242,Level 16,State 3,Line 21
  将nvarchar数据类型转换为日期时间数据类型会导致超出范围的值。

到目前为止,我的查询看起来像这样

print [ innerList if len(innerList)==len(set(innerList)) else [] for innerList in my_list ]

他们谈论的第21行是

use Test

IF OBJECT_ID('tempdb..#MetricsServerUserAudit') IS NOT NULL
BEGIN   
    BULK INSERT #MetricsServerUserAudit 
    FROM 'C:\QPR_Logs\Audit\MetricsServerAudit.txt'
    WITH (FIRSTROW = 1)             

IF OBJECT_ID('MetricsServerUserAudit') IS NOT NULL
begin
    INSERT INTO dbo.MetricsServerUserAudit(DateStamp, [TIME], [DATE],[USER_LOGIN], [USER_NAME], [MODEL_NAME], [OPERATION], [TARGET_USER],[TARGET_GROUP], [ELEMENT_TYPE_NAME], [ELEMENT_TYPE_PERMISSION], [OBJECT_NAME], [OBJECT_PERMISSION])
        SELECT
            CONVERT(CHAR(10), [DATE], 101) + ' ' + CONVERT(CHAR(11),[TIME], 108) as DateStamp,  
            CONVERT(CHAR(11), [TIME], 108) AS [TIME], 
            CONVERT(CHAR(10), [DATE], 101) AS [DATE],
            [USER_LOGIN], [USER_NAME], [MODEL_NAME], [OPERATION],
            [TARGET_USER], [TARGET_GROUP], 
            [ELEMENT_TYPE_NAME], [ELEMENT_TYPE_PERMISSION],
            [OBJECT_NAME], [OBJECT_PERMISSION]  
        FROM
            dbo.MetricsServerUserAudit 
        WHERE
            LEN([TIME]) = 8 
            AND NOT EXISTS (SELECT * FROM MetricsServerUserAudit)

    drop table #MetricsServerUserAudit
end
end
else
    IF OBJECT_ID('tempdb..#MetricsServerUserAudit') IS NULL
    begin
        CREATE TABLE #MetricsServerUserAudit
        (
            [TIME] nvarchar(max) NOT NULL,
            [DATE] nvarchar(max) NOT NULL,
            [USER_LOGIN] nvarchar(max) NOT NULL,
            [USER_NAME] nvarchar(max) NOT NULL,  
            [MODEL_NAME] nvarchar(max) NOT NULL,
            [OPERATION] nvarchar(max) NOT NULL,
            [TARGET_USER] nvarchar(max) NOT NULL,
            [TARGET_GROUP] nvarchar(max) NOT NULL,
            [ELEMENT_TYPE_NAME] nvarchar(max) NOT NULL,
            [ELEMENT_TYPE_PERMISSION] nvarchar(max) NOT NULL,
            [OBJECT_NAME] nvarchar(max) NOT NULL,
            [OBJECT_PERMISSION] nvarchar(max) NOT NULL  
        )

        BULK INSERT #MetricsServerUserAudit 
        FROM 'C:\QPR_Logs\Audit\MetricsServerAudit.txt'
        WITH (FIRSTROW = 1)

        IF OBJECT_ID('MetricsServerUserAudit') IS NULL
            CREATE TABLE MetricsServerUserAudit
            (
                [DateStamp] DATETIME NOT NULL,
                [TIME] nvarchar(max) NOT NULL,
                [DATE] nvarchar(max) NOT NULL,
                [USER_LOGIN] nvarchar(max) NOT NULL,    
                [USER_NAME] nvarchar(max) NOT NULL,
                [MODEL_NAME] nvarchar(max) NOT NULL,
                [OPERATION] nvarchar(max) NOT NULL,
                [TARGET_USER] nvarchar(max) NOT NULL,   
                [TARGET_GROUP] nvarchar(max) NOT NULL,
                [ELEMENT_TYPE_NAME] nvarchar(max) NOT NULL,
                [ELEMENT_TYPE_PERMISSION] nvarchar(max) NOT NULL,
                [OBJECT_NAME] nvarchar(max) NOT NULL,   
                [OBJECT_PERMISSION] nvarchar(max) NOT NULL
             )

        INSERT INTO MetricsServerUserAudit(DateStamp,[TIME],[DATE],[USER_LOGIN],[USER_NAME],[MODEL_NAME],[OPERATION],[TARGET_USER], [TARGET_GROUP],[ELEMENT_TYPE_NAME],[ELEMENT_TYPE_PERMISSION],[OBJECT_NAME],[OBJECT_PERMISSION])
            select 
                convert(char(10),[DATE],101) + ' ' + convert(char(11),[TIME],108) as DateStamp, 
                convert(char(11),[TIME],108) as [TIME],convert(char(10),[DATE],101) as[DATE],
                [USER_LOGIN], [USER_NAME],
                [MODEL_NAME], [OPERATION], 
                [TARGET_USER], [TARGET_GROUP],
                [ELEMENT_TYPE_NAME], [ELEMENT_TYPE_PERMISSION],
                [OBJECT_NAME], [OBJECT_PERMISSION]  
            from 
                #MetricsServerUserAudit 
            where 
                LEN([TIME]) = 8 
                and not exists (select * from MetricsServerUserAudit)

        drop table #MetricsServerUserAudit
end

有人可以向我解释为什么我会收到此错误吗?表的设计是错的吗?我的查询不对吗?如果有更好的查询方式,请告诉我。我想了解更多。

0 个答案:

没有答案