SQR - how to use FROM [dynamic table name} within BEGIN-SELECT?

398 views Asked by At

I have to create an SQR that generates a list of EEIDs, if there were any changes to the Pension data in the past day. The SQR compiles and works perfectly when I hardcode in the table names.

However, when I tried using variables for the table names, I get a compile error I've pasted the portion of SQR that I'm trying to fix When I start using $tableName and $auditTableName as table variables, that's when I get the error and I'm not sure what is going wrong

Can anyone help?

Please and Thank You

!***************************
begin-procedure Process-Main
!***************************  
let $tableName = 'PS_PENSION_PLAN'
let $auditTableName = 'PS_AUDIT_PENSION_PLN'    
let $dummy-dyn-variable = ''

begin-SELECT DISTINCT
L.EMPLID
L.EMPL_RCD

    do someProcName(&L.EMPLID, &L.EMPL_RCD)

FROM [$dummy-dyn-variable]
(
    SELECT DISTINCT
    PP.EMPLID,
    PP.EMPL_RCD,
    PP.EFFDT,
    '1901-01-01 12:00:00' AS AUDIT_STAMP
    FROM [$dummy-dyn-variable] [$tableName] PP
    UNION
    SELECT DISTINCT
    A.EMPLID,
    A.EMPL_RCD,
    A.EFFDT,
    A.AUDIT_STAMP
    FROM [$dummy-dyn-variable] [$auditTableName] A
)L
WHERE DATEDIFF(DAY,CAST(L.AUDIT_STAMP AS DATE),SYSDATE) = 1
ORDER BY 1,2
end-SELECT
   
end-procedure

Edit:

does the UNION have anything to do with this? I keep receiving is this error:

(SQR 5528) ODBC SQL dbdesc: SQLNumResultCols error 102 in cursor 1:
   [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'FROM'.
(SQR 5528) ODBC SQL dbdesc: SQLNumResultCols error 8180 in cursor 1:
   [Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepared.

Edit2:

Ok, initial problem solved with [$dummy-dyn-variable], which led to the next problem with the DO command. I've updated the code above with DO someProcName(param_a, param_b)

I am now getting an error saying:

(SQR 2002) DO arguments do not match procedure's

Weird part, if I remove the dynamic table variables and hardcode the table names in the FROM section, then it compiles properly without errors. This makes me believe that the error is not related to my someProcName (maybe?)

am I missing something here?

0

There are 0 answers