How can I pass dynamic array of items to %ExecDirect method.
I am having code like this,
set args = []
set resultSet = ##class(%SQL.Statement).%ExecDirect( , sql_whereClause, args)
Instead of placing args[1],args[2] etc into the above I want to pass the whole args at a time if it possible.?
kill args set args($i(args))="arg1" set args($i(args))="arg2" set args($i(args))="arg3" ... set resultSet = ##class(%SQL.Statement).%ExecDirect( , sql_whereClause, args...)
I tried this but it didn't work. Also here is how I am using args to set value inside.
do args.%Push(requestBody.firstname)
do args.%Push(requestBody.surname)
then I did the same thing. -
set resultSet = ##class(%SQL.Statement).%ExecDirect( , sql_whereClause, args...)
I had some doubt in your initial question.
NowYOUR mistake is evident
you compose a JSON array args=[arg1value,agr3value,arg3value,..]
using %Push(..)
to %SQL.Statement this is just kind of a strange structured String
%SQL.Statement doesn't deal with JSON Arrays or Objects
with arg... you have to pass a local variable array
which is a core structure of Object Script (since ever)
arg=3 ;;<max subscript count>
arg(1)=arg1value
arg(2)=arg2value
arg(3)=arg3value
as already described by @Julius Kavay
Simple summary.
passing a dynamic array to %SQLStatement is not possible and not implemented
Correct.
But (in case of need) there is a workaround (assuming, args is an JSON-array of arguments):
kill tmp set tmp=args.%Size() for i=1:1:tmp set tmp(i)=args.%Get(i-1) set resultSet = ##class(%SQL.Statement).%ExecDirect( , sql_whereClause, tmp...)