Question
· Jun 21, 2023

I want to fetch both column name and value from resultset of sql query

I want to fetch both column name and value from resultset of sql query . I am using ExecDirect() method for SQL execution. 

please help me out.

Product version: IRIS 2023.1
Discussion (15)2
Log in or sign up to continue

After using your %ExecDirect() method, you should have a %SQL.StatementResult Object. Let's assume you called this object rs (for result set). So you did something like set rs = ##class(%SQL.Statement).%ExecDirect(blahblahblah). From there:

//Get the list of columns
set cols = rs.%GetMetaData().columns
//Loop through the result object
while rs.%Next(){
//Loop through the columns
     for x=1:1:rs.%ResultColumnCount{
     //Get the value of each column
         set colValue = rs.%GetData(x)
         //Get the name of each column
         set colName = cols.GetAt(x).colName
         //TODO: Add whatever you're doing with the name and value here
     }
}