如果在SQL Server中存在条件,则存在

时间:2015-12-28 05:04:39

标签: sql-server sql-server-2008

我收到了SQL异常

  

')'

附近的语法不正确

这是存储过程:

if exists(select o.icd10code 
          from ICD10Master.dbo.icd10obginfectiousCrosswalk i
          inner join Icd10TempDisease t on i.icdcode = t.Code1
          where o.trimstatus = @condition   
            and t.[Status] in (0,1))
begin
    select 
        @obgCode = o.icd10code,
        @content = o.trimester,
        @history = t.history 
    from 
        ICD10Master.dbo.icd10obginfectiousCrosswalk i
    inner join 
        Icd10TempDisease t on i.icdcode = t.Code1
    where 
        o.trimstatus = @condition   
        and t.Status in (0,1)

    insert into Icd10TempDisease(disorder, cause, site, site1, site2, manifestation, stage, tconcept1, tconcept2, type, history, finding, code1, annotId, jobid, [content], startId, endId, poa, source, AccountNo, worktype)
    values(999999, 0, 0, 0, 0, 0, 9, 9, 0, 13, @history, 'positive', @obgCode, 0, @jobid, @content, 0, 0, 'N', 'obgcomplications', @accno, @worktype)
end 
else if (@condition = 4)
begin
    select 
        @obgCode = o.icd10code, @content = o.trimester,
        @history = t.history 
    from   
        ICD10Master.dbo.icd10obginfectiousCrosswalk i
    inner join 
        Icd10TempDisease t on i.icdcode = t.Code1 and t.Status in (0, 1, 5)
    inner join 
        ICD10Master.dbo.obgcomplications o on i.obgcode = o.code
    where 
        o.trimstatus = @condition2)

    insert into Icd10TempDisease(disorder, cause, site, site1, site2, manifestation, stage, tconcept1, tconcept2, type, history, finding, code1, annotId, jobid, [content], startId, endId, poa, source, AccountNo, worktype)
    values(999999, 0, 0, 0, 0, 0, 9, 9, 0, 13, @history, 'positive', @obgCode, 0, @jobid, @content, 0, 0, 'N', 'obgcomplications', @accno, @worktype)
end

如果存在条件,我会收到错误。

1 个答案:

答案 0 :(得分:1)

在第二个SELECT语句的末尾,您有一个额外的(不必要的)右括号。删除它,错误应该消失:

select @obgCode = o.icd10code,@content = o.trimester,@history = t.history from   ICD10Master.dbo.icd10obginfectiousCrosswalk i
inner join Icd10TempDisease t on i.icdcode = t.Code1 and t.Status in(0,1,5)
inner join ICD10Master.dbo.obgcomplications o on i.obgcode=o.code
where o.trimstatus =@condition2