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.
Discussion (5)0
Comments
#Dim sc As %Status =$$$ERROR($$$SQLError, SQLCODE, $g(%msg))
Thanks.
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
SQLCODE and %msg are closely related.
Instead of <FONT COLOR="#0000ff">$$$SQLError</FONT>, you can use <FONT COLOR="#0000ff">$$$SQLCode</FONT>.
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 }