3

(I've found this which partly answers the question, the declare way would look neater though given that my sql is will be used in oracle and mssql:) Binding variables in dynamic PL/SQL

I have some dynamic sql which I'm executing using syntax like the below:

EXECUTE IMMEDIATE plsql_block USING employeeid, sortname;

and I can then access those variables inside the dynamic sql using :1, :2, etc. Can I use named parameters instead? something like

EXECUTE IMMEDIATE plsql_block USING employeeid => employeeid 

and then access them inside the dynamic sql using :employeeid rather than relying on position?

If not my thought is to do something like this at the beginning of the sql:

declare employeeid varchar(15 := :1; 

and then I can change my dynamic sql to my hearts content without worrying about positioning.

Is there a better way?

thanks

0

2 Answers 2

3

Your workaround is what I have used in the past.
There's also good old DBMS_Sql, which allows binding variables like you would be able to via OCI. (Which is basically what DBMS_Sql seems to be doing anyways ;-) )

Sign up to request clarification or add additional context in comments.

Comments

3

You can use named arguments in DBMS_SQL:

DECLARE
        res INT;
        cr INT;
BEGIN
        cr := DBMS_SQL.OPEN_CURSOR;
        DBMS_SQL.PARSE(cr, :plsql_block, DBMS_SQL.NATIVE);
        DBMS_SQL.BIND_VARIABLE(cr, ':employee', :employee);
        res := DBMS_SQL.EXECUTE(cr);
        DBMS_SQL.CLOSE_CURSOR(cr);
END;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.