Plsql 创建动态查询时如何使用union

Plsql 创建动态查询时如何使用union,plsql,Plsql,我试图在动态查询中使用union从3个diff表中查询数据,但是当我这样做时,我没有得到任何编译错误,但是我得到以下错误: ORA-00933:SQL命令未正确结束 ORA-06512:第38行“XXXXXXXX”处 代码如下: V_Stmt_Str :='select '|| V_Mantas_Table_Seq_Id ||' from '|| Table_Name || 'union' || 'select '|| V_Mantas_Table_Seq_Id ||' from '|

我试图在动态查询中使用union从3个diff表中查询数据,但是当我这样做时,我没有得到任何编译错误,但是我得到以下错误:

ORA-00933:SQL命令未正确结束 ORA-06512:第38行“XXXXXXXX”处

代码如下:

V_Stmt_Str :='select '|| V_Mantas_Table_Seq_Id ||' from '|| Table_Name     || 'union' ||  'select '|| V_Mantas_Table_Seq_Id ||' from '|| Table_Name1     ||
'union' ||  'select '|| V_Mantas_Table_Seq_Id ||' from '|| Table_Name2;         
dbms_output.put_line('I AM HERE'); 
EXECUTE IMMEDIATE V_Stmt_Str bulk collect into Seq_Updt_Bulk; ---- This is the line where i am getting the error. 

另外,如果我在动态查询中只使用1条select语句,那么它就可以工作。

下面是示例代码,我认为您需要检查sql是否正确生成

declare
v_sql varchar2(4000);
type t_emp is table of emp%rowtype;
v_emp t_emp;
begin
v_sql := 'select * from emp where account_calss =''saving'' union  select * from emp where account_calss = ''current''' ;
dbms_output.put_line(v_sql);
execute immediate v_sql bulk collect into v_emp;
   for x in v_emp.first..v_emp.last
     loop
       dbms_output.put_line(v_emp(x).emp_id);
     end loop  ;
end;

您是否检查了sql查询中的空格?您可能需要打印它并尝试执行它。'union'| |'select'| |我解决了其他问题,但当我使用v_emp.first..v_emp.last时,它说应该声明first和last。还有,为什么我不能在这里使用count作为1..v_emp.count我想你应该能够了解你在这里提到的所有内容,你使用的是什么数据库和版本;如果是pl sql,您应该能够使用所有。请您粘贴代码以获取更多帮助。以下是代码::V|Stmt|u Str:=“选择”V|Mantas|u Table|Seq|u Id | |“,”V|u Mantas|u Table|Intrl Id | | from'| V|Mantas Table|u Name;立即执行V_Stmt_Str批量收集到详细记录中;对于1..Details\u record.COUNT中的所有indx,这里我得到的错误为COUNT not defined/First,Last not defined