Dynamic SQL Query using Linked Table Help
I am stuck at a crossroad and was wondering if I could get a second pair of eyes to help with a query that I am sending from a Task to a Service to process some data.
I have a Linked Table (view) that points back to a MS SQL View that I am trying to query. However, my execution keeps either failing or not returning any rows. When I take the SQL Query and execute it within the Management Portal I get results, however Cache is not seeing it when trying to execute this code. I put a bunch of zwrite statements to try to pinpoint the issue, but I don't understand if the Query works in the Management Portal, why it's not working in the code.
I tried both versions of passing a ? or a the pDate vairable in the SQL Query, and either calling tStatement.%Execute(pDate) or tStatement.%Execute() neither are returning any kind of RowCount.
Method SelectTerminations(pDate As %DateTime) As %Status
{
Set tSC=$$$OK
#Dim ex as %Exception.SystemException
try
{
zwrite "pDate: "_pDate
set sqlQuery = "SELECT EMPLID, MedCtrID, LastName, FirstName, EFFDT FROM OSU_Workday_SQL.PDMTerminations WHERE EFFDT > :pDate"
zwrite "Query: "_sqlQuery
set tStatment = ##class(%SQL.Statement).%New()
Set qStatus = tStatment.%Prepare(sqlQuery)
zwrite "Prepare Status: "_qStatus
$$$ThrowOnError(qStatus)
set rset = tStatment.%Execute()
zwrite "SQLCODE: "_rset.%SQLCODE
zwrite "Row Count: "_rset.%ROWCOUNT
if rset.%SQLCODE<0
{
zwrite "SQLCODE was gt 0"
$$$ThrowOnError(rset.%Message)
}
#Dim tTermService As OSU.DataSource.Workday.TermService
set tTermConfigName="OSU.DataSource.Workday.TermService"
zwrite "CreateBusinessService"
$$$ThrowOnError(##class(Ens.Director).CreateBusinessService(tTermConfigName,tTermService))
zwrite "Created Business Service Link"
while rset.%Next()
{
zwrite "Looping through rset"
set tTerm = ##class(OSU.Workday.Messages.WorkdayTerminations).%New()
zwrite "creating tTerm"
set tTerm.EMPLID = rset.%Get("EMPLID")
set tTerm.MedCtrID = rset.%Get("MedCtrID")
set tTerm.Lastname = rset.%Get("LastName")
set tTerm.Firstname = rset.%Get("FirstName")
set tTerm.EFFDT = rset.%Get("EFFDT")
zwrite "save tTerm object"
set tSC = tTerm.%Save()
$$$ThrowOnError(tSC)
set (tTermOutput,tHint) = ""
zwrite "send tTerm object to tTermService"
$$$ThrowOnError(tTermService.OnProcessInput(tTerm,.tTermOutput,.tHint))
}
}
Catch ex
{
Set tSC = ex.AsStatus()
}
Return tSC
}Comments
a quick check from the hip.
.... within the Management Portal I get results .....
>> SMP knows 3 modes: Display (3/2/2026) - Logical (+$h 67631) - ODBC (2026-03-02)
>> Which one is working for you ?
I'd guess WHERE EFFDT > :pDate is the critical point
If the formatting doesn't match, it may cause strange results.
first you may try to run your query in your code for a few records
to see by what format EFFDT is returned internally