You are on page 1of 2
2aa2015 [EXECUTE IMMEDIATE option for Dyramic SQL and PLSQL.— Database urralcom Teak Inleash yourcode's potential | Get Started > yf Featured Database Articles MSSOL, Oracle Oracle Bz MS Access Poste Mar 17,2008, MySOL eesounees EXECUTE IMMEDIATE option for Suet Dynamic SQL and PL/SQL 9. Ssrints & sample By Datahaselournal som Staff 4 leks . Tar kel] Database Forum, by Amar Kumar Padhi fs) Its . LATE is the repl for DBMS_SQL package fr See [EXECUTE IMMEDIATE i the replacement for DBMS_SQL package fom Oracle Hlonsanis pase and immediately exceuesa dynamic SQL eee ‘Free Newsletters: ‘statement or a PL/SQL block created on the fly. Dynamically created and and tomorro\ Dalahassis executed SQL statement are performance overhead, EXECUTE oe IMMEDIATE ans at reducing the overhead and pve beter performance. [EEO is also ease to code as compared wo caler means The enor messages, bees generated when using this fetture are more user friendly. Though DBMS_SQL-is stil available itis advisable to use EXECUTE IMMEDIATE calls bezause ofits benefits over the package een Usage tips 1, EXECUTE IMMEDIATE will not commit and an explicit commit shouldbe done DML transaction carried out Ifthe DML command is processed via EXECUTE IMMEDIATE, one needs ‘o explicitly commit any changes that may tave been done before or as pact of the EXECUTE IMMEDIATE itself. If the DDL comzsind is processed ¥ EXECUTE IMMEDIATE, it wil commit all previously changed dat 2. Maltsow queries are not supporte for returning values, the altemative is to use temporary table to store the record (see example below) oF make use of REF cursors 3. Do not use a semi-colon when executing SQL statements and use semi- colon atthe end when executing a PLISQL block. 4, This feature is nt covered at large in the Oracle Manuals, Below are «examples of ell possible ways of using Execute immediate. Hope its handy, 5. For Forms Developers, this featare will not work in Forms i ro-end a8 it soa PLISQL 8063, Example of EXECUTE IMMEDIATE usage 1. To nana DDL statement in PLISQL. begin recute Anadiate ‘set role all"; 2. Te pass values to a dynamic statement (USING clause) declare Todepnar varchar2(20) :~ ‘testing’: Choe” varenara(ae) $= “owbat"s vezi haphwwn databasejourna.con featur esloracloartcle php2 10868 EXECUTE .MMEDIATE-opon-er-Dynamic-SOL-and.PLSQL Him 8 2aa2015 [EXECUTE IMMEDIATE option for Dyramic SQL and PLSQL.— Databaseourracom evacute inediate ‘iraert into dept valves (2, 22, 23) sing 5, 1_drpran, 1ioc; 3. To retrieve values from a dynamie taemest (INTO clause), eetare Sent varchar2(28); bain Trccute imediate “select count(1) from eno neo 1aenes ins output. aut_Line() ent) 4. To calla routine dynamically: The bind variables used for parameters of the rounine have to be specified along with the parameter type. IN type is the defat, others have tobe specified expliily octane Torautin varchar2(t@9) :=,“gen2t61.get_rovent "s ACfbinae Vareraratza) = enp's Kent” ninbers Lstecus Varcherztze0); begin fekecute mediate “begin © || 1 rourin |] °(:25 53, :4)5 ends" ‘sing in iuthovan, out 1ent,-in out Zistotus: ns output put tive ercor")s ond it; 5, To retue value into @ PL/SQI-recond type: The same option canbe used Tr %eowtype variables also, ‘ype erpatinec £5 record (empne munber(4), ‘gnane_varenara(38), se eet ‘epee “nuaber(2))s veain ° fracute inediate ‘select enon, enane, geptro * [I rom exp ante ewe = 7338 nto onpetls 6, To pase and rettieve values: The INTO clause shovld precede the USING clase eetare Than archoraQaays Wise varenor2(20); begin frecute inediate ‘select énane, Toe fron dept where deptno = 1 neo loner, soc sing Teest 7 7. Maltirow query option. Use the inset statement to populate a temp table {rsh option, Ue the temporary table to earry ou further processing. Altematively, you may use REF cursors to by-pass this dawack, declare Tesalpls_integer :> 2000; begin recute Amediate “insert into tenp(enoro, anave) * |] Select nore, erate trot ero ° |] thee ants oS sing 1 $01: EXECUTE IMMEDIATE isa much easier and more efficient method of processing dynamic stacoments han could have been possible before. As the ‘tention isto exceute dynamic statemen's, proper handling of exceptions becomes al the more importa. Care shouldbe taken to tap all posible exceptions. ‘Top White Papers and Webcasts. 4 Ways to Empower your I Service Sbme Mere Sofware | Get Started > haiphwwn databasejourna con featur esloracloartcle php2 10868 EXECUTE AMMEDIATE-opon-er-Dynamic-SOL-and.PLSQLim 2s

You might also like