Class parameter in class query

Primary tabs

Is it possible to use a class parameter in class query?

Example:

Class SomeClass
{

Parameter SOMENAME = "Name1";

Query SomeQuery() As %SQLQuery
{
SELECT ID || ..#SOMENAME  || Name FROM Table
}

}
  • + 1
  • 0
  • 234
  • 1
  • 2

Answers

Class dc.SomeClass
{

Parameter SOMENAME = "Name1";

Query SomeQuery(SOMENAME = {..#SOMENAME}) As %SQLQuery
{
  SELECT ID || :SOMENAME || Name FROM Table1
}

Query SomeQuery1() As %SQLQuery
{
  #define SOMENAME ##Expression("'"_$parameter("dc.SomeClass","SOMENAME")_"'")
  SELECT ID || $$$SOMENAME || Name FROM Table1
}

Query SomeQuery2() As %SQLQuery
{
  #define SOMENAME ##Expression("'"_##class(dc.SomeClass).#SOMENAME_"'")
  SELECT ID || $$$SOMENAME || Name FROM Table1
}

ClassMethod Test()
{
  ;d ##class(dc.SomeClass).Test()
  
  rs=##class(%ResultSet).%New("dc.SomeClass:SomeQuery")
  sc=rs.Execute()
  i $$$ISERR(sc{
    d $System.Status.DisplayError(sc)
  else {
    rs.%Display()
  }
}
}

Fantastic help. Thank for sharing this. I didn't know you could use ##Expression in basic class queries. Studio does complain about it though but works just fine at run time!

This can be done with SQL procedure:

Query SomeQuery() As %SQLQuery
{
SELECT ID || SomeClass.GetParam('SomeClass', 'SOMENAME') FROM Table
}

ClassMethod GetParam(class, param) As %String [ CodeMode = expression, SqlProc ]
{
$parameter(class, param)
}