将表名和列值作为动态sqls中的变量传递

时间:2013-11-11 05:42:43

标签: oracle dynamic-sql

这是我的代码:

create or replace
procedure postGateway (flgManual in nvarchar2, segmentID in number) as
sequel string(2000); 


  cursor download_cursor is
    select downloadid from ipcsdd_download_process where status LIKE 'W' OR status 

LIKE 'E';



 cursor table_cursor is

   select table_name from user_tab_columns where column_name = 'DOWNLOADID' and 

table_name like 'IPCSDD%' OR table_name like 'IPCSCUSTDD' group by table_name;

begin       

    for download in download_cursor
    loop
        dbms_output.put_line('DownloadID: ' || download.downloadid );

      for usertable in table_cursor
     loop

sequel:=' select * FROM'||usertable.table_name||'where downloadid='||download.downloadid;
execute immediate sequel;
      dbms_output.put_line(' select * from'||usertable.table_name||'where downloadid='||download.downloadid);  
    end loop;   

  end loop;

end postGateway ;

我在这里做的是:在第一个游标中,我试图获取状态为W或E的downloadid。在第二个游标中,我试图获取具有downloadid coloumn的表,并且那些表名应该以IPCSDD或IPCSCUSTDD。

现在我必须编写一个查询,以便在从IPCSDD开始的每个表中从光标2获取我需要查看是否存在从光标1获取的downloadid的数据。我尝试编写动态sql但是它给我错误说“00923. 00000 - ”FROM关键字未找到预期的位置“”。

我怎样才能做到这一点?

由于

1 个答案:

答案 0 :(得分:0)

您只是忽略了在关键字之前和之后添加空格,在FROM之后添加空格,在之前添加空格:

sequel:=' select * FROM '||usertable.table_name||' where downloadid='||download.downloadid;