EXECUTE IMMEDIATE with multiple lines of columns to insert

user1941350 picture user1941350 · Jan 18, 2013 · Viewed 14.8k times · Source

just want to get an idea if this is the correct way to do an EXECUTE IMMEDIATE with multiple columns and lines and assigning it to a variable? I tried looking at examples but am not sure if I am concatenating the lines correctly?

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’;   

Answer

Vincent Malgrat picture Vincent Malgrat · Jan 18, 2013

A string can be multi-line in Oracle. As such, you could simply write:

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;

Using concatenation (||) and several substrings would work as well of course.