user8101021 user8101021 -4 years ago 32
ASP.NET (C#) Question

if not exists begin update insert error

I look at the code and checking again and again, really don't know what happen to my code and getting error below -

strSql= "BEGIN if (not EXISTS (select empno tablenameA where empno = @empno)) " & _
" begin " & _
" Update tablenameA set ota_verifier = @ota_verifier, ota_approval = @ota_approval, ota_payroll = @ota_payroll, ota_hr = @ota_hr, " & _
" ota_viewall = @ota_viewall, ota_HRMng = @ota_HRMng, ota_MD = @ota_MD, ota_SP=@ota_SP " & _
" WHERE empno = @empno " & _
" end " & _
" else " & _
" begin " & _
" Insert INTO tablenameA (empno, ota_empname, ota_deptcode, ota_verifier, ota_approval, ota_payroll, ota_hr, ota_viewall, ota_AD, ota_HRMng, ota_MD, ota_SP) " & _
" VALUES (@empno, @ota_empname, @ota_deptcode, @ota_verifier, @ota_approval, @ota_payroll, @ota_hr, @ota_viewall, @ota_AD, @ota_HRMng, @ota_MD, @ota_SP) " & _
" end " & _
" END "



Incorrect syntax near '.'. Incorrect syntax near the keyword 'else'.

Answer Source

**You need add FROM **

strSql=     "BEGIN if (not EXISTS (select ota_empno FROM [GOGREEN].[dbo].[tbOvertimeAccess] where ota_empno = @ota_empno)) " & _
            " begin " & _
            " Update tbOvertimeAccess set ota_verifier = @ota_verifier, ota_approval = @ota_approval, ota_payroll = @ota_payroll, ota_hr = @ota_hr, " & _
            " ota_viewall = @ota_viewall, ota_HRMng = @ota_HRMng, ota_MD = @ota_MD, ota_SP=@ota_SP " & _
            " WHERE ota_empno =  @ota_empno " & _
            " end    " & _
            " else   " & _
            " begin  " & _
            " Insert INTO tbOvertimeAccess (ota_empno, ota_empname, ota_deptcode, ota_verifier, ota_approval,  ota_payroll, ota_hr, ota_viewall, ota_AD, ota_HRMng, ota_MD, ota_SP) " & _
            " VALUES (@ota_empno, @ota_empname, @ota_deptcode, @ota_verifier, @ota_approval, @ota_payroll, @ota_hr, @ota_viewall, @ota_AD, @ota_HRMng, @ota_MD, @ota_SP) " & _
            "   end  " & _
            "   END "
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download