Skip to main content


class %SQL.StatementResult extends %Library.RegisteredObject

%SQL.StatementResult Base Dynamic Statement Result Descriptor.

Dynamic SQL Statement Result

%SQL.StatementResult is the result descriptor class. Executing a dynamic statement (by calling statement.%Execute or ##class(%SQL.Statement).%ExecDirect where statement is an instance of %SQL.Statement) returns an instance of %SQL.StatementResult.

The content of a statement result object varies depending on the statement type and whether or not the statement was successfully executed. The interface to the result object is the same for all statement types but the content of the result object varies.

When retrieving results, first check for successful execution by examining %SQLCODE. For many statement types, success/failure is all that is needed. The %SQLCODE property contains the SQL error code value. Successful completion values are 0 (successful completion) and 100 (the operation has either found no data or reached the end of the data). Error condition values are negative numbers. Many statements affect some number of rows. The number of rows affected by the statement execution is contained in the %ROWCOUNT property. For a SELECT statement, if the cursor is positioned after the last row, the value of %ROWCOUNT indicates the number of rows contained in the result set. At any other time, %ROWCOUNT contains the number of rows retrieved thus far. %ROWID is set to the RowID of the last record modified by the most recent INSERT, UPDATE, INSERT OR UPDATE, DELETE, or TRUNCATE TABLE operation. After an INSERT statement, the %ROWID property contains the system-assigned value of the RowID (Object ID) assigned to the new record.

A successfully executed SELECT statement returns a single result set. The number of columns in the result set is contained in %ResultColumnCount. A cursor (iterator) is available to retrieve rows from the result set. To advance the cursor to the next row, call %Next(). The initial position of the cursor is before the first row. %Next() returns 1 to indicate that it is positioned on a row or 0 to indicate that it is positioned after the last row (at the end of the result set). If the cursor is positioned after the last row, the value of %ROWCOUNT indicates the number of rows contained in the result set. At any other time, %ROWCOUNT contains the number of rows retrieved thus far. For more information on the result set interface refer to the %SQL.IResultSet class.

A CALL statement result might contain output values, including a return value and any parameters defined as INPUT-OUTPUT or OUTPUT, as well as a collection of dynamic result sets. The %OutputColumnCount property indicates the number of output values. You can retrieve individual output values by using the output value iterator - %NextOutputValue().

This is an example of retrieving all of the output values returned by a procedure:

		if result.%OutputColumnCount > 0 {
			set tPosition = ""
			set tValue = result.%NextOutputValue(.tPosition)
			while tPosition '= "" {
				write !,tPosition," = ",tValue
				set tValue = result.%NextOutputValue(.tPosition)
		} else { write !,"No output values returned." }

In the above example, the value of tPosition is the position of the formal parameter whose value is returned in tValue. If the procedure declares a return value then the return value is always at position 0 (zero). Input parameters are not contained in the output values but the presence of an input parameter is reflected as a gap in the output value position.

A CALL statement can also return dynamic result sets as a collection referred to as a result set sequence. There are two mechanisms for accessing result sets - serial binding and parallel binding. Serial binding is automatically used whenever any part of its interface is invoked. The serial binding interface consists of the %ResultColumnCount property, a private property %ProcCursor, and the methods %Next(), %MoreResults(), %GetMetadata(), %Get(), %GetData(), %Print() and several internal methods. The first reference to any of the serial binding interface members triggers the first result set from the result set sequence to be bound to the %ProcCursor property and also to the %CurrentResult property. Subsequent references to the serial binding interface operate on that result set. When the currently bound result set is completely processed, you can invoke %MoreResults() to retrieve the next result set from the result set sequence by calling %NextResult() and then binding that result to %ProcCursor.

The parallel binding interface is implemented as the %NextResult() method and the %CurrentResult property. Each call to %NextResult() returns an oref to the next result set in the collection of dynamic resultsets returned by the procedure. When all result sets have been returned then %NextResult() returns NULL (value = "" in COS). The result returned by %NextResult() is also bound to the %CurrentResult property.

%CurrentResult always references the most recently bound result set and it can be used for direct access to that result set, bypassing the serial binding interface. The user must be careful since both parallel binding (%NextResult()) and serial binding (automatic and %MoreResults()) affects the value of %CurrentResult.

Property Inventory

Method Inventory


parameter %statementactual;
parameter %statementmetadata;
parameter %statementmetadataext;
parameter %statementobjects;
parameter %statementparameters;


property %CurrentResult as %Library.RegisteredObject;
Holds oref of the most recently bound result set. This property is set automatically by %NextResult(). Note that %NextResult() is also invoked by %MoreResults(). Users should not ever set this property. This property is public for backward compatibility reasons.
Property methods: %CurrentResultGet(), %CurrentResultGetSwizzled(), %CurrentResultIsValid(), %CurrentResultNewObject(), %CurrentResultSet()
property %Message as %Library.String (MAXLEN = 256);
%Message contains additional information about an error reported as a negative value in %SQLCODE. Additional information is not always available.
Property methods: %MessageDisplayToLogical(), %MessageGet(), %MessageIsValid(), %MessageLogicalToDisplay(), %MessageLogicalToOdbc(), %MessageNormalize(), %MessageSet()
property %OutputColumnCount as %Library.Integer [ InitialExpression = 0 ];
%OutputColumnCount is the number of columns defined as RETURNVALUE, INPUT-OUTPUT or OUTPUT (Caché method return value, byref and output parameters) by an SQL invoked routine. This value is always zero unless the DYNAMIC_FUNCTION is CALL (%StatementType = 45).
Property methods: %OutputColumnCountDisplayToLogical(), %OutputColumnCountGet(), %OutputColumnCountIsValid(), %OutputColumnCountLogicalToDisplay(), %OutputColumnCountNormalize(), %OutputColumnCountSet()
property %ROWCOUNT as %Library.Integer;
Contains the number of rows affected by executing the statement.
It is the responsibility of the stored procedure author to explicitly set %ROWCOUNT.

For example:

&sql(UPDATE Person Set Name="Mo" WHERE Age > :number)

Property methods: %ROWCOUNTDisplayToLogical(), %ROWCOUNTGet(), %ROWCOUNTIsValid(), %ROWCOUNTLogicalToDisplay(), %ROWCOUNTNormalize(), %ROWCOUNTSet()
property %ROWID as %Library.String);
The value of the %ROWID. This may or may not be relevant. It is primarily used when a dynamic INSERT statement has been executed. In that case, %ROWID contains the rowid of the last row inserted into the database by executing the statement.
Property methods: %ROWIDDisplayToLogical(), %ROWIDGet(), %ROWIDIsValid(), %ROWIDLogicalToDisplay(), %ROWIDLogicalToOdbc(), %ROWIDNormalize(), %ROWIDSet()
property %ResultColumnCount as %Library.Integer [ InitialExpression = 0 ];
%ResultColumnCount is the number of columns in the result set. This value is always zero unless the current result is a RESULTSET object (instance of %SQL.IResultSet) or a procedure context object (instance of %Library.IProcedureContext). For a CALL statement result, this value represents the column count of the result set currently bound using serial binding.
Property methods: %ResultColumnCountDisplayToLogical(), %ResultColumnCountGet(), %ResultColumnCountIsValid(), %ResultColumnCountLogicalToDisplay(), %ResultColumnCountNormalize(), %ResultColumnCountSet()
property %SQLCODE as %Library.Integer [ InitialExpression = 0 ];

%SQLCODE indicates whether or not the statement was executed successfully. If %SQLCODE is not negative (greater than or equal to zero) then the statement was successfully executed. Otherwise, the value of %SQLCODE contains the error code and the %Message property might contain more information about the error.

Property methods: %SQLCODEDisplayToLogical(), %SQLCODEGet(), %SQLCODEIsValid(), %SQLCODELogicalToDisplay(), %SQLCODENormalize(), %SQLCODESet()
property %StatementType as %Library.Integer;
%StatementType is the dynamic statement type number as assigned by Cache SQL. Refer to %StatementTypeName for statement type values.
Property methods: %StatementTypeDisplayToLogical(), %StatementTypeGet(), %StatementTypeIsValid(), %StatementTypeLogicalToDisplay(), %StatementTypeNormalize(), %StatementTypeSet()
property %StatementTypeName as %Library.String [ Calculated ];

%StatementTypeName is the statement type name.

16ALTER INDEX (Not supported)
25GET (Not supported)
32%CHECKPRIV (SQL Admin Privilege)
33GRANT (SQL Admin Privilege)
34REVOKE (SQL Admin Privilege)
""Anything not list above
Property methods: %StatementTypeNameCompute(), %StatementTypeNameDisplayToLogical(), %StatementTypeNameGet(), %StatementTypeNameIsValid(), %StatementTypeNameLogicalToDisplay(), %StatementTypeNameLogicalToOdbc(), %StatementTypeNameNormalize(), %StatementTypeNameSQLCompute()


method %ConstructClone(deep As %Boolean = 0, ByRef cloned As %String, location As %String) as %RegisteredObject
Inherited description: Clone the current object to a new object. If deep is 1 then this does a deep copy which will also copy any subobjects and if deep is 0 then it will create another reference to any subobjects and increment the reference count appropriately. It returns the new cloned object.

Note that even if deep=0 when you clone a parent object in a parent child relationship or a one object of a one to many relationship then it will construct clones of all the child/many objects. This is because a child/many object can only point at a single parent and so if we did not create a clone of these then you would have a relationship with zero items in it. If you really just want to clone the object without these child/many objects then pass deep=-1 to this method.

After the clone is constructed it will call %OnConstructClone(object,deep,.cloned) on the clone if it is defined so that you can perform any additional steps e.g. taking out a lock. This works just the same way as %OnNew() does.

The object is the oref of the original object that was cloned. The cloned array is just used internally when doing a deep clone to prevent recursive loops, do not pass anything in at all for this parameter on the initial call. If you write a %OnConstructClone and from here you wish to call %ConstructClone on another object pass in the cloned array, e.g. 'Do oref.%ConstructClone(1,.cloned)' so that it can prevent recursive loops.

The location is used internally to pass the new location for stream objects.

method %DispatchMethod(pMethod As %String, Args...)
Inherited description: Is used to implement an unknown method call. It is also used to resolve an unknown multidimensional property reference (to get the value of a property) because that syntax is identical to a method call.
method %Display(pDelimiter As %String = $Char(9))
Display the contents of this object on the current device
method %DisplayFormatted(pFormat As %String = -1, pFileName As %String(MAXLEN="")="", ByRef pMessages As %SQL.Manager.Messages = "", ByRef pFilesUsed As %String(MAXLEN="")=0, pTranslateTable As %String(MAXLEN="")="")

Display the contents of the result object. If formatted display is available then format the results using the requested format and, if appropriate, open the formatted results using the host OS. The output is directed to one or more files and messages are placed in a result set object. All file names used are returned in an array.



The format applied to the result content. This parameter is also used to determine the file name extension.

Supported formats are:

-1%Display() format
2PDF (requires a renderer such as FOP)

If pFormat is specified as any number not listed above then it will default to TXT.

pFormat can also be specified as XML, HTML, PDF, TXT or CSV.


The base file name to be used to generate actual file names used for output. If no value is specified then a file name will be generated, using the TEMP folder defined for the Cache instance. This value is not expected to include an extension. An extension is added to this value to form the actual file used. Also, if nested results exist then a number is appended to the file name specified to produce a unique name for each result.


Instance of a system result set class. If no value is passed then the system message result class is instantiated. This parameter is passed by reference. It is up to the caller to process the result set oref that is returned. pMessages.Count() returns the number of messages contained in the result set. pMessages.%Display() will display the messages on the current device. pMessages.%DisplayFormatted() is also implemented and can be used to display the messages using the selected format.


This pass-by-reference parameter will contain the number of files used to display the result content and the name of each file. pFilesUsed is the number of files and pFilesUsed(file_number) is the name of the file. The sequence of the files is the same sequence as the results are processed. For simple result objects, there is a single file. For context objects that can contain result set sequences, the results are output in the order they are returned and the files used are present in pFilesUsed in that same order.


This is the translate table used for the output files when the format is CSV or TXT. This parameter is optional.

abstract method %Get(colname As %String = "") as %Library.String
Returns the value of the column with the name colname in the current row of the result set.

If colname is not a valid column name, this method throws a error.

abstract method %GetData(colnbr As %Integer = 0) as %Library.String
Returns the value of column colnbr in the current row of the result set.
method %GetMetadata() as %SQL.StatementMetadata
Return the result set metadata as a %SQL.StatementMetadata instance.
method %MoreResults() as %Integer

%MoreResults() invokes %NextResult() to return the next result object from a statement result object (aka 'context object') and updates %ProcCursor with that value. This is called "serial binding". %ResultColumnCount is updated to reflect the number of columns contained in the newly bound result. If a %ProcCursor was previously set to a result object then the reference to that result object was removed. Under normal circumstances the previously bound result set is destructed.

If the new value of %ProcCursor is a valid result object then %MoreResults() returns 1, otherwise it returns 0 to indicate that no more results are available.

method %Next(ByRef sc As %Library.Status = $$$OK) as %Library.Integer
Advance to the next row in the result referenced by %ProcCursor. Returns 0 if the cursor is at the end of the result set. An optional argument contains a %Library.Status value on return. This %Status value indicates success or failure of the %Next call. %SQLCODE is also set by %Next. This implementation is overridden by classes that implement the result set interface.
method %NextOutputValue(ByRef pPosition As %Integer = "") as %CacheString

Return the output value from the position following pPosition. If the caller passed pPosition by reference then it will contain the position corresponding to the output value returned. If the pPosition argument is NULL then no values exist beyond the value passed by the caller and the value returned is also NULL.

The output value at position 0 is always the called routine's return value.

method %NextResult() as %Library.RegisteredObject
%NextResult() returns the next result object from a statement descriptor (aka 'context object'). Typically, multiple results are only returned from an SQL invoked procedure. Such results are dynamic result sets whose metadata is not available until retrieval time. %NextResult() for non-CALL statements simply returns NULL, indicating that no more results are available. For an SQL invoked procedure that returns a single preplanned result set and for SELECT statements (which return a single result set when successfully executed) this method returns the current result set object when called for the first time. Subsequent calls return a null oref.
method %NormalizeObject() as %Status
Inherited description: Normalizes all of an object's property values by invoking the data type Normalize methods. Many data types may allow many different representations of the same value. Normalization converts a value to its cannonical, or normalized, form.
abstract method %Print(pDelimiter As %String = " ") as %Status
%Print() - This is an abstract method. Refer to %SQL.IResultSet for the concrete method implemented for result set results.
method %SerializeObject(ByRef serial As %Binary, partial As %Integer = 0) as %Status
Inherited description: This method retrieves all of the serial values for referenced objects and places them into the instance variables, Validates, Normalizes, and serializes the object (with a save of the persistent image if persistent).
This method is not meant to be called directly. It is called by %Save and by %GetSwizzleObject.
method %ValidateObject(force As %Library.Integer = 0, checkserial As %Library.Boolean = 1) as %Status
Inherited description: This method validates an object. The %Save method of a persistent class calls it before filing any objects in the database. The %ValidateObject method of a referencing object can call it. You can also call it explicitly at any time.

%ValidateObject() does the following:

  1. It tests if any required property values are missing.
  2. If the PROPERTYVALIDATION class parameter is set to ValidateOnSave, it validates each non-null property value by calling the property method IsValid on each literal property and the object's %ValidateObject method for object-valued properties.
  3. If present, it will call a user-supplied %OnValidateObject() method.

If any of these tests fail, %ValidateObject() immediately returns an error value. It is up to the caller of %ValidateObject to process the error value.

Returns a %Status value indicating success or failure.

Inherited Members

Inherited Methods


FeedbackOpens in a new tab