25.01.2015 Views

Using Caché Objects - InterSystems Documentation

Using Caché Objects - InterSystems Documentation

Using Caché Objects - InterSystems Documentation

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Using</strong> <strong>Objects</strong> with <strong>Caché</strong> ObjectScript<br />

• GetParamName(n) returns a string specifying the name of the nth argument of the query.<br />

• GetColumnCount returns an integer specifying the number of columns that the query<br />

returns.<br />

• GetColumnName(n) returns a string specifying the name of the nth column (field).<br />

• GetColumnHeading(n) returns a string specifying the heading of the nth column (field).<br />

10.7.2 Preparing Queries for Execution<br />

To run a pre-defined class query, begin by instantiating a %ResultSet object as follows:<br />

Set rset = ##class(%ResultSet).%New()<br />

where rset is the OREF of the new %ResultSet object.<br />

Having instantiated the %ResultSet object, the next task is to associate it with a particular<br />

query in a particular class:<br />

Set rset.ClassName = class<br />

Set rset.QueryName = query<br />

where rset is the OREF of the %ResultSet object, class is the name of the class containing<br />

the query, and query is the name of the query to execute.<br />

For example:<br />

Set rset = ##class(%ResultSet).%New()<br />

Set rset.ClassName = "Sample.Person"<br />

Set rset.QueryName = "ByName"<br />

Do rset.Execute()<br />

While (rset.Next(.sc)) // go to the next row of the result set<br />

{<br />

If ($SYSTEM.Status.IsOK(sc)) // check if this succeeded without errors<br />

{<br />

Write rset.Data("Name"),! // perform busines logic<br />

}<br />

Else // if there was an error, break out of the While loop<br />

{<br />

Quit<br />

}<br />

}<br />

If $SYSTEM.Status.IsError(sc) // if there was an error, process that<br />

{<br />

// perform error processing<br />

}<br />

You can also use the %ResultSet object to prepare a dynamic SQL statement using the Prepare<br />

method:<br />

84 <strong>Using</strong> <strong>Caché</strong> <strong>Objects</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!