oracle 如何在sqlplus中定义名为“1”的绑定变量

xoefb8l8  于 2023-11-17  发布在  Oracle
关注(0)|答案(2)|浏览(124)

我有一个应用程序,已被称为使用绑定变量编写:1,2,3..我想行使存储在sql_area的执行计划,并使用相同的文本在sqlplus中执行查询
举例来说:
第一个月
如何在sqlplus中将“1”定义为绑定变量?
var 1 varchar2(1); doesn't work.
它不识别“1”作为变量名。

xkftehaa

xkftehaa1#

是的,这很烦人,但你可以像这样将查询 Package 到匿名PL/SQL块(脚本)中:

SQL> exec execute immediate 'select count(*) cnt from dual where :1=dummy' using 'x';

PL/SQL procedure successfully completed.

SQL> select sql_id from v$sqlarea a where sql_text like 'select count(*) cnt from dual%';

SQL_ID
-------------
8y7sav2n21055

1 row selected.

SQL> ed tests/run_sql.sql

SQL> !cat tests/run_sql.sql
declare
  vsql clob;
begin
  select sql_fulltext into vsql from v$sqlarea a where a.sql_id='&1';
  execute immediate vsql using &2;
end;
/

SQL> @tests/run_sql.sql 8y7sav2n21055 123

PL/SQL procedure successfully completed.

字符串
x1c 0d1x的数据
顺便说一下,SqlCl有另一个更简单的解决方法:使用alias

SQL> alias sel1=select :1 from dual;
SQL> sel1 123

:1
----------------------------------------------------
123

mzaanser

mzaanser2#

你试过这样做吗

VARIABLE in VARCHAR2(10)
EXEC :in := 'X';
EXEC EXECUTE IMMEDIATE 'select * from dual where dummy=:1' USING :in;

字符串
如果你想得到一个结果,使用这个

VARIABLE in VARCHAR2(10)
VARIABLE out VARCHAR2(10)
EXEC :in := 'X';
EXEC EXECUTE IMMEDIATE 'select ''hello ''||dummy from dual where dummy=:1' INTO :out USING :in;
PRINT out

相关问题