Building a channel to query an external database and insert the results into an internal database (both SQL Server) and to do the following things:
(for reference the "aht" database is the internal db I'm trying to do the insert)
1) Get the aht userid for integration
2) Get the aht client information from aht database.
3) Get the aht patient id from aht database, blank if they donít exist yet
4) Get the aht Diagnosis id from aht database, blank if doesnít match
5) Get the aht CaseManager id from aht database, 0 if doesnít exist (QUERY IS DONE BUT always returning nothing even though the query works in regular sql)
6) IF casemanager is 0 (not in aht database) Insert casemanager into aht database and get the new case manager id
7) IF aht patient id is blank, create the patient (using data values from mirth and from javascript (ahtDiagId,ahtClientId,ahtCMID,ahtUserId)
8) IF aht patient id is not blank, UPDATE the patient (using data values from mirth and from javascript (ahtDiagId,ahtClientId,ahtCMID,ahtUserId)

The problem is the query to select a casemanager id is always returning a blank. But if I take the logged query it returns values. So its inserting tons of duplicates.

Any suggestions would be appreciated. I've attached the channel for review.