- Log in to post comments
Additionally, it's best that you avoid concatenation for building the string of your query, and should instead use parameters to avoid issues with unexpected characters breaking your query (or, in some scenarios, opening yourself up to exploits via SQL injection).
My example above should instead be:
Set tStatement = ##class(%SQL.Statement).%New()
Set tQuery = "SELECT * From Ens_Util.LookupTable WHERE TableName = ?"
$$$THROWONERROR(tSC,tStatement.%Prepare(tQuery))
Set tResult = tStatement.%Execute(tTable)
While tResult.%Next(){
//Do what you need to with the results here
$$$TRACE("Result: "_tResult.KeyName_", "_tResult.DataValue)
}Note that the table name is a ? in the initial query string, and the value of the table name is being referenced in the %Execute() method
- Log in to post comments
Hey Nezla, if you are looking to interact with an internal table, then using the outbound adapter for SQL is not what will work for you. This is because the adapter relies on the servers ODBC config to look for the specified table in the external database based on the DSN you've configured.
You will instead want to swap your ObjectScript to something like:
Set tStatement = ##class(%SQL.Statement).%New()
Set tQuery = "SELECT * From Ens_Util.LookupTable WHERE TableName ='"_tTable_"'"
$$$THROWONERROR(tSC,tStatement.%Prepare(tQuery))
Set tResult = tStatement.%Execute()
While tResult.%Next(){
//Do what you need to with the results here
$$$TRACE("Result: "_tResult.KeyName_", "_tResult.DataValue)
}Then, if you still need to also query for something in the external database based on the DSN configured, then you can interact with the external database using the ..Adapter.ExecuteQuery approach you had attempted.
- Log in to post comments
The Method as shown in my examples was "%Next" but you have missed the %.
See this more complete example:
Which gives this in a trace:
Also, please note that in my initial response, I had a mistake in the column names I referenced. I had put "KeyName" and "KeyValue" whereas the second should have been "DataValue" as you already had in your first screenshot.