plsql EXECUTE IMMEDIATE动态执行sql

select * from BANK t;

DECLARE
v_money bank.money%TYPE;
BEGIN

--简单执行sql,注意sql字符串不要带分号结束
EXECUTE IMMEDIATE 'SELECT money FROM bank WHERE id = 5';

--带参数,参数使用using传入,按顺序的传入
EXECUTE IMMEDIATE 'SELECT money FROM bank WHERE id = :param1 or id =:param2'
USING '4','5';

--执行sql后,取值,using在赋值后面
EXECUTE IMMEDIATE 'SELECT money FROM bank WHERE id = :param1'
INTO v_money
USING '2';

DBMS_OUTPUT.PUT_LINE( 'Column Variable: ' || v_money );

END;

 注意:

--using是用于变量参数,但变量是参与sql语句的就不ok啊
EXECUTE IMMEDIATE 'SELECT sysdate FROM dual WHERE :param'
USING '1=1';
--sql 会变成下面的无效sql
SELECT sysdate FROM dual WHERE '1=1'

 

posted @ 2020-07-20 11:05  舒碧  阅读(1602)  评论(5编辑  收藏  举报