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

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

  •  01-12-2021
  •  | 
  •  

문제

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...

도움이 되었습니까?

해결책

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)
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top