Using the 'SqlComputeCode' keyword to project a calculated property into the sql table

StackOverflow https://stackoverflow.com/questions/13494872

  •  01-12-2021
  •  | 
  •  

Question

I have a class with several calculated properties. I would like to add these properties to the projected sql table so that I can query on them. However, I noticed that calculated properties are not projected into the sql table by default. To get them to project into the sql table, the keyword SqlComputeCode must be added in brackets after specifying the data type

Property Age As %String [SqlComputeCode...]

However, I'm unsure of the exact syntax to be used here and didn't find the documentation about it particularly helpful (I think Intersystems needs to show more examples). Because the property is calculated I already have a get method for it to do the calculation.

Method AgeGet() As %String
{
   ...
   Quit "Something"
}

I just want to use the calculation in this get method in my sql table projection. Can anyone help me out? Thanks in advance...

Was it helpful?

Solution

You can't access an instance method from within an SQL computation. So you would need to refactor the method you have - most straightforwardly to a class method.

Property Age As %String [SqlComputeCode = {S {Age}=##CLASS(MyClass).GetAge({DateOfBirth})}, SqlComputed]

ClassMethod GetAge(DateOfBirth as %Date)
{
    Q ##CLASS(MyDateRoutineClass).DifferenceInYears(##CLASS(MyDateRoutineClass).Now(),DateOfBirth)
}

Method AgeGet() As %String
{
   ...
   Quit ..GetAge(..DateOfBirth)
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top