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:
<FONT COLOR="#0000ff">#Dim </FONT><FONT COLOR="#800000">sc </FONT><FONT COLOR="#0000ff">As </FONT><FONT COLOR="#008080">%Status </FONT><FONT COLOR="#000000">= </FONT><FONT COLOR="#0000ff">$$$OK s</FONT><FONT COLOR="#000000">:</FONT><FONT COLOR="#800000">SQLCODE sc </FONT><FONT COLOR="#000000">= </FONT><FONT COLOR="#000080">##class</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#008080">%Exception.SQL</FONT><FONT COLOR="#000000">).</FONT><FONT COLOR="#0000ff">CreateFromSQLCODE</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">SQLCODE</FONT><FONT COLOR="#000000">,</FONT><FONT COLOR="#800000">%msg</FONT><FONT COLOR="#000000">).</FONT><FONT COLOR="#0000ff">AsStatus</FONT><FONT COLOR="#000000">() </FONT><FONT COLOR="#0000ff">s</FONT><FONT COLOR="#000000">:</FONT><FONT COLOR="#800000">SQLCODE sc </FONT><FONT COLOR="#000000">= </FONT><FONT COLOR="#0000ff">$system</FONT><FONT COLOR="#008080">.Error</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">FromSQLCode</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">SQLCODE</FONT><FONT COLOR="#000000">,</FONT><FONT COLOR="#800000">%msg</FONT><FONT COLOR="#000000">).</FONT><FONT COLOR="#0000ff">Status</FONT>It should also be noted the %sqlcontext, so final code will look as follows:
<FONT COLOR="#0000ff">if </FONT><FONT COLOR="#800000">SQLCODE </FONT><FONT COLOR="#800080">{
</FONT><FONT COLOR="#0000ff">set</FONT><FONT COLOR="#000000">:</FONT><FONT COLOR="#0000ff">$IsObject</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#0000ff">$get</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">%sqlcontext</FONT><FONT COLOR="#000000">)) </FONT><FONT COLOR="#800000">%sqlcontext</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">%SQLCODE</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#800000">SQLCODE</FONT><FONT COLOR="#000000">, </FONT><FONT COLOR="#800000">%sqlcontext</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">%Message</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#0000ff">$get</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">%msg</FONT><FONT COLOR="#000000">)
</FONT><FONT COLOR="#0000ff">set </FONT><FONT COLOR="#800000">sc</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#0000ff">$$$ERROR</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#0000ff">$$$SQLCode</FONT><FONT COLOR="#000000">,</FONT><FONT COLOR="#800000">SQLCODE</FONT><FONT COLOR="#000000">,</FONT><FONT COLOR="#0000ff">$g</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">%msg</FONT><FONT COLOR="#000000">))
</FONT><FONT COLOR="#800080">} </FONT><FONT COLOR="#0000ff">else </FONT><FONT COLOR="#800080">{ </FONT><FONT COLOR="#0000ff">set </FONT><FONT COLOR="#800000">sc</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#0000ff">$$$OK </FONT><FONT COLOR="#800080">}</FONT>