Thank you for sharing Mike.
Please notice that Dynamic SQL looks for the host variable in the global scope.
For example, consider following method:
Class Sample.Sqlbindtest [ Abstract ]
{
ClassMethod test()
{
set minage = 80
set myquery = 3
set tStatement = ##class(%SQL.Statement).%New()
set myquery(1) = "SELECT top 10 %ID AS id, Age , Name, %ODBCOUT(DOB) DOB, Home_State"
set myquery(2) = "FROM Sample.Person WHERE Age > :minage"
set myquery(3) = "ORDER BY 2"
set qStatus = tStatement.%Prepare(.myquery)
set tResult = tStatement.%Execute()
do tResult.%Display()
}
}
The query will refer to minage variable defined at the variable scope outside of method itself:
SAMPLES>d ##class(Sample.Sqlbindtest).test()
id Age Name DOB Home_State
0 Rows(s) Affected
SAMPLES>set minage=20 // now we define minage
SAMPLES>d ##class(Sample.Sqlbindtest).test()
id Age Name DOB Home_State
41 21 Beatty,Emily I. 1997-01-21 ID
163 21 Mastrolito,David X. 1996-04-14 AL
32 22 Adam,Sophia V. 1995-12-12 CO
33 22 Xiang,Laura L. 1995-03-02 MN
87 22 Paladino,Violet P. 1995-09-14 MN
139 22 Goncharuk,Stavros D. 1996-01-24 RI
173 22 Zucherro,Dmitry R. 1995-03-28 AK
56 23 Zubik,Quigley N. 1994-03-26 ID
46 24 Rogers,Mo D. 1994-02-16 OR
142 24 Orwell,Zelda M. 1993-07-12 AL
10 Rows(s) Affected
- Log in to post comments