|
|
|
Often it is necessary to execute several SQL statements one by one. Sometimes it can be performed by anonymous PL/SQL blocks. But unfortunately it doesn't work in all cases. For example DDL statements cannot be used in PL/SQL. Another way is using a lot of objects such as OraCommand. Usually it isn't a good solution. With only one OraScript object you can execute several SQL statements as one. This sequence of statements is named script. To separate single statements use semicolon (;) or slash (/) and for PL/SQL only slash. Note slash must be the first character in the line. OraScript implements methods to execute a series of SQL statements separated by special symbols, like SQL*Plus scripts.
Following code shows how execute simple script:
...
// Connection that will be used for script execution
OraConnection conn("scott/tiger@ora");
// Create OraScript instance and specify connection
OraScript script(&conn);
// Set script text
script.setSQL("DROP TABLE DEPT;\n"
"CREATE TABLE DEPT (\n"
" DEPTNO NUMBER(2) CONSTRAINT PK_DEPT PRIMARY KEY,\n"
" DNAME VARCHAR2(14),\n"
" LOC VARCHAR2(13)\n"
");\n"
"INSERT INTO DEPT VALUES (10,'ACCOUNTING','NEW YORK');\n"
"INSERT INTO DEPT VALUES (20,'RESEARCH','DALLAS');\n"
"INSERT INTO DEPT VALUES (30,'SALES','CHICAGO');\n"
"INSERT INTO DEPT VALUES (40,'OPERATIONS','BOSTON');");
// Execute script and ignore all errors
script.execute(true);
// Close connection
conn.close();
...
See Also
OraScript, OraConnection
OCL | Using OCL | Index