Nazeer_hanne
Nazeer_hanne

Reputation: 223

If exists else if condition in SQL Server

I'm getting a SQL exception

Incorrect syntax near ')'

Here is the stored procedure:

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

I'm getting error at if exists condition.

Upvotes: 0

Views: 1250

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522712

You have an extra (unnecessary) closing parenthesis at the end of your second SELECT statement. Remove it and the error should go away:

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

Upvotes: 1

Related Questions