由于“设置不正确”导致作业失败:QUOTED_IDENTIFIER

时间:2017-11-03 08:16:22

标签: sql-server jobs

我试图将查询称为作业:

 DECLARE @Id VARCHAR(max) -- database name  
DECLARE @VIN VARCHAR(max) -- database name  
DECLARE @SystemType NVARCHAR(max) -- path for backup files  
DECLARE @CarType NVARCHAR(max) -- filename for backup  
DECLARE @CarTipe NVARCHAR(max) -- used for file name
DECLARE @FuelType NVARCHAR(max) -- used for file name
DECLARE @FuelSystem NVARCHAR(max) -- used for file name
DECLARE @Model NVARCHAR(max) -- used for file name
DECLARE @SubmitDatetime datetime -- used for file name
DECLARE @UserId NVARCHAR(max) -- used for file name
DECLARE @MotorNum NVARCHAR(max) -- used for file name
DECLARE @Chassis NVARCHAR(max) -- used for file name
DECLARE @ValidTest NVARCHAR(max) -- used for file name
DECLARE @IsWorn NVARCHAR(max) -- used for file name

use [PayeshDB]
DECLARE db_cursor CURSOR FOR  
 select Id,VIN,SystemType,CarType,CarTipe,FuelType,FuelSystem,Model,SubmitDatetime,UserId
 ,MotorNum,Chassis,ValidTest,IsWorn from payeshdb.dbo.AutomotiveTemps


OPEN db_cursor  
FETCH NEXT FROM db_cursor INTO @Id,@VIN,@SystemType,@CarType,@CarTipe,@FuelType,@FuelSystem,@Model
,@SubmitDatetime,@UserId,@MotorNum,@Chassis,@ValidTest,@IsWorn

WHILE @@FETCH_STATUS = 0  
BEGIN  
       if (select count(*) from InquiryStatus where UserId=@userid and SubmitDatetime=convert(date,@SubmitDatetime))=0

    begin 
    insert into InquiryStatus (UserId, SubmitDatetime,TotalCarburetorInquiry,TotalGasInquiry,TotalGasolineInquiry
    ,TotalInjectorInquiry,TotalInquiry,TotalInvalidTestInquiry,TotalNotWornInquiry,TotalPetrolInquiry,TotalValidTestInquiry,TotalWornInquiry
    ,TotalCarburetorResult,TotalGasolineResult,TotalGasResult,TotalInjectorResult,TotalinValidResult,TotalNotWornResult,TotalPetrolResult,TotalValidResult,TotalWornResult)values 
    (@userid,convert(date,@SubmitDatetime),0,0,0,0,0,0,0,0,0,0,'<Result></Result>','<Result></Result>','<Result></Result>','<Result></Result>','<Result></Result>','<Result></Result>','<Result></Result>','<Result></Result>','<Result></Result>')
    end
    else begin 
    update InquiryStatus set  TotalInquiry=TotalInquiry+1 ,
    TotalCarburetorInquiry=CASE WHEN @FuelSystem='1' THEN TotalCarburetorInquiry+1 else TotalCarburetorInquiry  END,
    TotalGasInquiry=CASE WHEN @FuelType='2' THEN TotalGasInquiry+1  else TotalGasInquiry END,
    TotalGasolineInquiry=CASE WHEN @FuelType='3' THEN TotalGasolineInquiry+1 else TotalGasolineInquiry END,
    TotalInjectorInquiry=CASE WHEN @FuelSystem='2' THEN TotalInjectorInquiry+1 else TotalInjectorInquiry  END,
    TotalInvalidTestInquiry=CASE WHEN @ValidTest='0' THEN TotalInvalidTestInquiry+1 else TotalInvalidTestInquiry END,
    TotalNotWornInquiry=CASE WHEN @IsWorn='0' THEN TotalNotWornInquiry+1 else TotalNotWornInquiry  END,
    TotalPetrolInquiry=CASE WHEN @FuelType='1' THEN TotalPetrolInquiry+1 else TotalPetrolInquiry  END,
    TotalValidTestInquiry=CASE WHEN @ValidTest='1' THEN TotalValidTestInquiry+1  else TotalValidTestInquiry END,
    TotalWornInquiry=CASE WHEN @IsWorn='1' THEN TotalWornInquiry+1  else TotalWornInquiry END
    where UserId=@userid and SubmitDatetime=convert(date,@SubmitDatetime)
      end


    --update TotalCarburetorResult
    if @FuelSystem='1'
    begin 
    declare @currentValue bigint
    declare @xmlResult xml
    select @xmlResult=TotalCarburetorResult from InquiryStatus where UserId=@userid and SubmitDatetime=convert(date,@SubmitDatetime)
    if (SELECT @xmlResult.exist(N'/Result/item[@model=(sql:variable("@model")) and @CarType=(sql:variable("@CarType")) and @ SystemType=(sql:variable("@SystemType"))]'))=1
    begin 
            select  @currentValue= @xmlResult.value(N'(/Result/item[@model=(sql:variable("@model")) and @CarType=(sql:variable("@CarType")) and @ SystemType=(sql:variable("@SystemType"))])[1]', 'Bigint') +1
            UPDATE InquiryStatus
      SET TotalCarburetorResult.modify(N'replace value of (/Result/item[@model=(sql:variable("@model")) and @CarType=(sql:variable("@CarType")) and @ SystemType=(sql:variable("@SystemType"))]/count/text())[1] with (sql:variable("@currentValue"))') where  UserId=@userid and SubmitDatetime=convert(date,@SubmitDatetime)

    end
    else

    begin

    declare @myxml xml
    set @myxml='<item model="'+@Model+'" CarType="'+@CarType+'" SystemType="'+@SystemType+'"><count>1</count></item>'


UPDATE InquiryStatus
SET TotalCarburetorResult.modify(N'insert sql:variable("@myxml") into (/Result)[1]') where  UserId=@userid and SubmitDatetime=convert(date,@SubmitDatetime)

     end

end


       FETCH NEXT FROM db_cursor INTO @Id,@VIN,@SystemType,@CarType,@CarTipe,@FuelType,@FuelSystem,@Model
,@SubmitDatetime,@UserId,@MotorNum,@Chassis,@ValidTest,@IsWorn
END  

CLOSE db_cursor  
DEALLOCATE db_cursor 

但是当我的工作开始时,我收到了这个错误:

Date        11/3/2017 11:44:25 AM
Log     Job History (AnalysisAutomotiveTemp)

Step ID     1
Job Name        AnalysisAutomotiveTemp
Step Name       Analysis
Duration        00:00:02
Sql Severity    16
Sql Message ID  1934
Operator Emailed    
Operator Net sent   
Operator Paged  
Retries Attempted   0

Message
Executed as user: ......$. CONDITIONAL failed because the following SET options have incorrect settings: 'QUOTED_IDENTIFIER'. Verify that SET options are correct for use with indexed views and/or indexes on computed columns and/or filtered indexes and/or query notifications and/or XML data type methods and/or spatial index operations. [SQLSTATE 42000] (Error 1934).  The step failed.

我用谷歌搜索了我的错误,我发现我应该在我的查询中设置它:

[SET QUOTED_IDENTIFIER ON][1]

但我不知道我应该把这个命令放在哪里。

1 个答案:

答案 0 :(得分:1)

最好使用QUOTED_IDENTIFIER ONQUOTED_IDENTIFIER OFF是为了向后兼容而提供的,如果使用双引号括起文字的错误编写代码无法修复。

这里的问题是xquery表达式中的SQL变量用双引号括起来。下面未经测试的示例在xquery表达式中使用2个单引号,以便将它们解释为单引号。

DECLARE @Id VARCHAR(max) -- database name  
DECLARE @VIN VARCHAR(max) -- database name  
DECLARE @SystemType NVARCHAR(max) -- path for backup files  
DECLARE @CarType NVARCHAR(max) -- filename for backup  
DECLARE @CarTipe NVARCHAR(max) -- used for file name
DECLARE @FuelType NVARCHAR(max) -- used for file name
DECLARE @FuelSystem NVARCHAR(max) -- used for file name
DECLARE @Model NVARCHAR(max) -- used for file name
DECLARE @SubmitDatetime datetime -- used for file name
DECLARE @UserId NVARCHAR(max) -- used for file name
DECLARE @MotorNum NVARCHAR(max) -- used for file name
DECLARE @Chassis NVARCHAR(max) -- used for file name
DECLARE @ValidTest NVARCHAR(max) -- used for file name
DECLARE @IsWorn NVARCHAR(max) -- used for file name

use [PayeshDB]
DECLARE db_cursor CURSOR FOR  
 select Id,VIN,SystemType,CarType,CarTipe,FuelType,FuelSystem,Model,SubmitDatetime,UserId
 ,MotorNum,Chassis,ValidTest,IsWorn from payeshdb.dbo.AutomotiveTemps


OPEN db_cursor  
FETCH NEXT FROM db_cursor INTO @Id,@VIN,@SystemType,@CarType,@CarTipe,@FuelType,@FuelSystem,@Model
,@SubmitDatetime,@UserId,@MotorNum,@Chassis,@ValidTest,@IsWorn

WHILE @@FETCH_STATUS = 0  
BEGIN  
       if (select count(*) from InquiryStatus where UserId=@userid and SubmitDatetime=convert(date,@SubmitDatetime))=0

    begin 
    insert into InquiryStatus (UserId, SubmitDatetime,TotalCarburetorInquiry,TotalGasInquiry,TotalGasolineInquiry
    ,TotalInjectorInquiry,TotalInquiry,TotalInvalidTestInquiry,TotalNotWornInquiry,TotalPetrolInquiry,TotalValidTestInquiry,TotalWornInquiry
    ,TotalCarburetorResult,TotalGasolineResult,TotalGasResult,TotalInjectorResult,TotalinValidResult,TotalNotWornResult,TotalPetrolResult,TotalValidResult,TotalWornResult)values 
    (@userid,convert(date,@SubmitDatetime),0,0,0,0,0,0,0,0,0,0,'<Result></Result>','<Result></Result>','<Result></Result>','<Result></Result>','<Result></Result>','<Result></Result>','<Result></Result>','<Result></Result>','<Result></Result>')
    end
    else begin 
    update InquiryStatus set  TotalInquiry=TotalInquiry+1 ,
    TotalCarburetorInquiry=CASE WHEN @FuelSystem='1' THEN TotalCarburetorInquiry+1 else TotalCarburetorInquiry  END,
    TotalGasInquiry=CASE WHEN @FuelType='2' THEN TotalGasInquiry+1  else TotalGasInquiry END,
    TotalGasolineInquiry=CASE WHEN @FuelType='3' THEN TotalGasolineInquiry+1 else TotalGasolineInquiry END,
    TotalInjectorInquiry=CASE WHEN @FuelSystem='2' THEN TotalInjectorInquiry+1 else TotalInjectorInquiry  END,
    TotalInvalidTestInquiry=CASE WHEN @ValidTest='0' THEN TotalInvalidTestInquiry+1 else TotalInvalidTestInquiry END,
    TotalNotWornInquiry=CASE WHEN @IsWorn='0' THEN TotalNotWornInquiry+1 else TotalNotWornInquiry  END,
    TotalPetrolInquiry=CASE WHEN @FuelType='1' THEN TotalPetrolInquiry+1 else TotalPetrolInquiry  END,
    TotalValidTestInquiry=CASE WHEN @ValidTest='1' THEN TotalValidTestInquiry+1  else TotalValidTestInquiry END,
    TotalWornInquiry=CASE WHEN @IsWorn='1' THEN TotalWornInquiry+1  else TotalWornInquiry END
    where UserId=@userid and SubmitDatetime=convert(date,@SubmitDatetime)
      end


    --update TotalCarburetorResult
    if @FuelSystem='1'
    begin 
    declare @currentValue bigint
    declare @xmlResult xml
    select @xmlResult=TotalCarburetorResult from InquiryStatus where UserId=@userid and SubmitDatetime=convert(date,@SubmitDatetime)
    if (SELECT @xmlResult.exist(N'/Result/item[@model=(sql:variable(''@model'')) and @CarType=(sql:variable(''@CarType'')) and @ SystemType=(sql:variable(''@SystemType''))]'))=1
    begin 
            select  @currentValue= @xmlResult.value(N'(/Result/item[@model=(sql:variable(''@model'')) and @CarType=(sql:variable(''@CarType'')) and @ SystemType=(sql:variable(''@SystemType''))])[1]', 'Bigint') +1
            UPDATE InquiryStatus
      SET TotalCarburetorResult.modify(N'replace value of (/Result/item[@model=(sql:variable(''@model'')) and @CarType=(sql:variable(''@CarType'')) and @ SystemType=(sql:variable(''@SystemType''))]/count/text())[1] with (sql:variable(''@currentValue''))') where  UserId=@userid and SubmitDatetime=convert(date,@SubmitDatetime)

    end
    else

    begin

    declare @myxml xml
    set @myxml='<item model="'+@Model+'" CarType="'+@CarType+'" SystemType="'+@SystemType+'"><count>1</count></item>'


UPDATE InquiryStatus
SET TotalCarburetorResult.modify(N'insert sql:variable(''@myxml'') into (/Result)[1]') where  UserId=@userid and SubmitDatetime=convert(date,@SubmitDatetime)

     end

end


       FETCH NEXT FROM db_cursor INTO @Id,@VIN,@SystemType,@CarType,@CarTipe,@FuelType,@FuelSystem,@Model
,@SubmitDatetime,@UserId,@MotorNum,@Chassis,@ValidTest,@IsWorn
END  

CLOSE db_cursor  
DEALLOCATE db_cursor