我在pl sql中使用以下过程
CREATE OR REPLACE "MY_PROCEDURE" (v_id number, v_name varchar2) AS
BEGIN
EXECUTE IMMEDIATE ' CREATE TABLE T_TEMPO ( t_id number , t_name varchar2(250) , t_value number )';
EXECUTE IMMEDIATE 'INSERT INTO T_TEMPO (t_id , t_name, t_value)
SELECT id , name , value
from TABLE_2
where TABLE_2.id = || v_id ||
AND TABLE_2.name = || v_name || ';
但这不起作用我有一个缺少的表达式错误,我想它不能在execute immediate中评估变量v_id和v_name的值。 有人可以帮忙吗?
答案 0 :(得分:4)
您需要引用值(必要时使用引号):
create or replace procedure my_procedure
( v_id number
, v_name varchar2 )
as
begin
execute immediate 'CREATE TABLE T_TEMPO (t_id number, t_name varchar2(250), t_value number )';
execute immediate 'INSERT INTO T_TEMPO (t_id, t_name, t_value)
SELECT id, name, value
FROM table_2
WHERE table_2.id = ' || v_id ||
' AND table_2.name = ''' || v_name || '''';
end my_procedure;
或(通常更好)将它们作为绑定变量传递:
create or replace procedure my_procedure
( v_id number
, v_name varchar2 )
as
begin
execute immediate 'CREATE TABLE T_TEMPO (t_id number, t_name varchar2(250), t_value number )';
execute immediate 'INSERT INTO T_TEMPO (t_id, t_name, t_value)
SELECT id, name, value
FROM table_2
WHERE table_2.id = :id
AND table_2.name = :name' using v_id, v_name;
end my_procedure;
btw我会使用变量的v
前缀和参数的p
。
答案 1 :(得分:0)
请使用引号外的变量:
EXECUTE IMMEDIATE ' CREATE TABLE T_TEMPO ( t_id number , t_name varchar2(250) , t_value number )';
EXECUTE IMMEDIATE 'INSERT INTO T_TEMPO (t_id , t_name, t_value)
SELECT id , name , value
from TABLE_2
where TABLE_2.id = ' || v_id ||
' AND TABLE_2.name = '''|| v_name || '''';