Written by

Question CJ H · Oct 17, 2017

How to wrap %SQLCODE into %Status

I have a method would return %Status,

this method would run some sql queries and if the execution of these queries fail, I would like to return asap.

So how could I wrap the %SQLCODE into a %Status variable?

Thank for your help.

Comments

Eduard Lebedyuk · Oct 17, 2017
#Dim sc As %Status =  $$$ERROR($$$SQLError, SQLCODE, $g(%msg))
0
CJ H  Oct 17, 2017 to Eduard Lebedyuk

Thanks.

0
David Brock  Oct 18, 2017 to Eduard Lebedyuk

set status = $$$ERROR($$$SQLError, $System.SQL.SQLCODE(SQLCODE))

Would return a %Status value with the text of the SQLCODE error available.

The previous answer would give the SQLCODE error number.

Use whichever is appropriate

0
Vitaliy Serdtsev  Oct 18, 2017 to David Brock

SQLCODE and %msg are closely related.

Instead of <FONT COLOR="#0000ff">$$$SQLError</FONT>, you can use <FONT COLOR="#0000ff">$$$SQLCode</FONT>.

0
Vitaliy Serdtsev · Oct 18, 2017

Another variants:

#Dim sc As %Status $$$OK
s:SQLCODE sc ##class(%Exception.SQL).CreateFromSQLCODE(SQLCODE,%msg).AsStatus()
s:SQLCODE sc $system.Error.FromSQLCode(SQLCODE,%msg).Status

It should also be noted the %sqlcontext, so final code will look as follows:

if SQLCODE {
 set:$IsObject($get(%sqlcontext)) %sqlcontext.%SQLCODE=SQLCODE%sqlcontext.%Message=$get(%msg)
 set sc=$$$ERROR($$$SQLCode,SQLCODE,$g(%msg))
else set sc=$$$OK }
0