%SQL.StatementResult
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
- %CurrentResult
- %Message
- %OutputColumnCount
- %ROWCOUNT
- %ROWID
- %ResultColumnCount
- %SQLCODE
- %StatementType
- %StatementTypeName
Method Inventory
- %ConstructClone()
- %DispatchMethod()
- %Display()
- %DisplayFormatted()
- %Get()
- %GetData()
- %GetMetadata()
- %MoreResults()
- %Next()
- %NextOutputValue()
- %NextResult()
- %NormalizeObject()
- %Print()
- %SerializeObject()
- %ValidateObject()
Parameters
Properties
It is the responsibility of the stored procedure author to explicitly set %ROWCOUNT.
For example:
&sql(UPDATE Person Set Name="Mo" WHERE Age > :number)
Set result.%SQLCODE=SQLCODE
Set result.%ROWCOUNT=%ROWCOUNT
%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.
%StatementTypeName is the statement type name.
1 | SELECT |
2 | INSERT (also 'INSERT OR UPDATE') |
3 | UPDATE |
4 | DELETE |
5 | COMMIT |
6 | ROLLBACK |
7 | GRANT |
8 | REVOKE |
9 | CREATE TABLE |
10 | ALTER TABLE |
11 | DROP TABLE |
12 | CREATE VIEW |
13 | ALTER VIEW |
14 | DROP VIEW |
15 | CREATE INDEX |
16 | ALTER INDEX (Not supported) |
17 | DROP INDEX |
18 | CREATE ROLE |
19 | DROP ROLE |
20 | SET TRANSACTION |
21 | START TRANSACTION |
22 | %INTRANSACTION |
23 | %BEGTRANS (Alias for START TRANSACTION) |
24 | %INTRANS (Alias for %INTRANSACTION) |
25 | GET (Not supported) |
26 | SET OPTION |
27 | STATISTICS (UPDATE STATISTICS, not supported)) |
28 | %CHECKPRIV |
29 | CREATE USER |
30 | ALTER USER |
31 | DROP USER |
32 | %CHECKPRIV (SQL Admin Privilege) |
33 | GRANT (SQL Admin Privilege) |
34 | REVOKE (SQL Admin Privilege) |
35 | CREATE FUNCTION |
36 | CREATE METHOD |
37 | CREATE PROCEDURE |
38 | CREATE QUERY |
39 | DROP FUNCTION |
40 | DROP METHOD |
41 | DROP PROCEDURE |
42 | DROP QUERY |
43 | CREATE TRIGGER |
44 | DROP TRIGGER |
45 | CALL |
46 | SAVEPOINT |
47 | LOCK TABLE |
48 | UNLOCK TABLE |
49 | CREATE DATABASE |
50 | DROP DATABASE |
51 | USE DATABASE |
"" | Anything not list above |
Methods
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.
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.
Parameters
Name | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
pFormat |
The format applied to the result content. This parameter is also used to determine the file name extension. Supported formats are:
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. |
||||||||||||
pFileName |
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. |
||||||||||||
pMessages |
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. |
||||||||||||
pFilesUsed |
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. |
||||||||||||
pTranslateTable |
This is the translate table used for the output files when the format is CSV or TXT. This parameter is optional. |
If colname is not a valid column name, this method throws a error.
%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.
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.
This method is not meant to be called directly. It is called by %Save and by %GetSwizzleObject.
%ValidateObject() does the following:
- It tests if any required property values are missing.
- 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.
- 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.