2

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

Fred Altman
  • 221
  • 1
  • 2
  • 5

1 Answers1

3

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)
}
psr
  • 2,870
  • 18
  • 22
  • 2
    There is another example of this in the Sample.Person class in the SAMPLES namespace of your Cache database. – codeymccodeface Nov 21 '12 at 20:53
  • 2
    In modern versions you may use `{*}` for a link to current property `S {*}=..GetAge({DateOfBirth})` – SSH Nov 22 '12 at 06:06