EXECUTE IMMEDIATE,要插入多行列

时间:2013-01-18 15:01:30

标签: plsql insert concatenation execute-immediate

只是想知道这是否是使用多个列和行执行EXECUTE IMMEDIATE并将其分配给变量的正确方法?我试着查看示例,但不确定我是否正确连接线路?

sql_stmt        VARCHAR2(200);

sql_stmt:='INSERT INTO (STORECODE, TILLID, TRANSACTIONNR, TRADINGDATE, TRANSTYPE, ' ||
          'OPERATORCODE TRAININGMODE, VOIDED, VALUEGROSS, VALUENETT, VALUEDUE) ' ||
          ‘VALUES (:1,:2,:3,:4,:5,:6,:7,:8,:9,:10)’;
          EXECUTE IMMEDIATE sql_stmt USING sSTORECODE………………………………………..fGROSS_AMOUNT,
      ‘0’;   

2 个答案:

答案 0 :(得分:11)

Oracle中的字符串可以是多行的。因此,您可以简单地写一下:

sql_stmt := 'INSERT INTO (STORECODE, TILLID, TRANSACTIONNR, TRADINGDATE, 
                          TRANSTYPE, OPERATORCODE TRAININGMODE, VOIDED, 
                          VALUEGROSS, VALUENETT, VALUEDUE) 
                  VALUES (:1,:2,:3,:4,:5,:6,:7,:8,:9,:10)';

EXECUTE IMMEDIATE sql_stmt USING p1, p2... p10;

使用连接(||)和几个子串当然也可以。

答案 1 :(得分:2)

set serveroutput on size unlimited;

declare
str_   varchar(1000 char);
date_ date;


begin
  --str_ := 'select sysdate' || chr(10);
  --str_ := str_ || 'from dual';
  str_ := 'select sysdate /* ''comment'' */
           from dual';

  execute immediate str_
  into date_;

  dbms_output.put_line(date_);


end;