存储过程使用 WHERE 子句执行立即错误

Stored procedure execute Immediate error with WHERE clause

我正在尝试从我的存档 table 复制一行到我的原始文件 table。

如果没有我的 WHERE 子句,table2 的整个 table 将被复制到 table1。 我当然不想要这个。因此,根据列出的 gridview 的 ID 值,table 将仅复制 ID 相同的行。

当我调试行时,我得到了 DisplaySup.Rows(0).Cells(2).Text.

列出的正确 ID
(     
val_ID table2.V_ID%type
) 

is 
begin 

execute immediate 'insert into table1 (select * from table2 where V_ID = val_ID)'; 
end; 

但我收到错误

ORA-00904: "VAL_ID": invalid identifier

Table2 和 Table1 有相同的列;所以他们都有标题为 V_ID 的专栏。我不确定为什么 Val_ID 会标记错误。

VB.net 编码行:

SupArchive.Parameters.Add("val_ID", OleDbType.VarChar).Value = DisplaySup.Rows(0).Cells(2).Text

所以我尝试引用:EXECUTE IMMEDIATE with USING clause giving errors

像这样修复 WHERE:

(     
val_ID table2.V_ID%type
) 

is 
begin 

execute immediate 'insert into table1 (select * from table2 where V_ID = '||val_ID||')'; 
end; 

但我得到错误:

ORA-00904: "val_ID": invalid identifier

关于如何修复我的存储过程有什么建议吗?

更新:

已尝试执行建议:

(
val_ID table2.V_ID%type
)
AS
BEGIN

execute immediate 'insert into table1 (col1, col2, col3...)(select col1, col2, col3... from table2 where V_ID = :val_ID)' using val_ID;

end;

但出现错误:

ORA-00904: "col72": invalid identifier

for col72 after Select statement

我的表格示例(两者相同)table2 的目的是在 table1 中删除一行时,table2 可以 re-create 用户已删除

表 1

ID 公司名称名字姓氏....(72 列)

表 2

ID 公司名名字姓氏...(72 列)

您最好在插入语句中使用绑定变量。此外,您需要列出要插入的列以及要插入的列,以避免 "too many values" 错误。

例如:

declare
  val_ID table2.V_ID%type := 1;
begin 
  execute immediate 'insert into table1 (col1, col2, ...) (select col1, col2, ... from table2 where V_ID = :val_ID)' using val_id;
end;
/

尽管在这种情况下完全没有必要使用动态 sql,所以您可以这样做:

declare
  val_id table2.v_id%type := 1;
begin 
  insert into table1 (col1, col2, ...)
  select col1, col2, ...
  from   table2
  where  v_id = val_id;
end;
/

完成 运行 程序后不要忘记提交!