æ¨å¥½ï¼å¾é«å
´ä¸ºæ¨è§£çã
/*å¨ææ§è¡åå¨è¿ç¨DEMO*/
DECLARE
v_Procedure_Name VARCHAR2(320); --åå¨è¿ç¨å
v_Input_Parameter1 VARCHAR2(320); --ä¼ å
¥åæ°1
v_Input_Parameter2 VARCHAR2(320); --ä¼ å
¥åæ°2
v_Return_Int INTEGER; --æ¥æ¶è¿åå¼
v_Return_String VARCHAR2(320); --æ¥æ¶è¿åå¼
v_Sql_Str VARCHAR2(320); --å¨ææ§è¡SQLè¯å¥
BEGIN
/*è·ååå¨è¿ç¨åï¼å¯ä»¥ä»æ°æ®åºä¸å¨æ读åéè¦æ§è¡çåå¨è¿ç¨ï¼æ¬DEMOç´æ¥èµå¼*/
v_Procedure_Name := 'Wms_Application_Out_p.Application_Out_List_Refuse(:i_Str1,:i_Str2,_Return_Int,_Return_String)';
/*æ¬DEMOåå®éè¦è°ç¨çåå¨è¿ç¨æä¸¤ä¸ªä¼ å
¥åæ°åä¸¤ä¸ªä¼ åºåæ°*/
v_Input_Parameter1 := 'test01'; --ä¼ å
¥åæ°1,å¨å®é
åºç¨ä¸æ ¹æ®å
·ä½æ
åµæºåèµå¼
v_Input_Parameter2 := 'test01'; --ä¼ å
¥åæ°2,å¨å®é
åºç¨ä¸æ ¹æ®å
·ä½æ
åµæºåèµå¼
/*å¨æçæéè¦æ§è¡åå¨è¿ç¨çSQLè¯å¥*/
v_Sql_Str := 'BEGIN ' || v_Procedure_Name || '; END;';
/*æ§è¡è¯¥SQLè¯å¥*/
EXECUTE IMMEDIATE v_Sql_Str
USING IN v_Input_Parameter1, IN v_Input_Parameter2, OUT v_Return_Int, OUT v_Return_String; --åæ°ç±»ååä¼ é顺åºå¿
é¡»ä¸åå¨è¿ç¨ä¸çä¿æä¸è´
/*è¾åºè¿åå¼*/
Dbms_Output.Put_Line(v_Return_Int || v_Return_String);
END;
/*å¨ææ§è¡å½æ°DEMO*/
DECLARE
v_Function_Name VARCHAR2(320); --å½æ°å
v_Input_Parameter1 VARCHAR2(320); --ä¼ å
¥åæ°1
v_Return_String VARCHAR2(320); --æ¥æ¶è¿åå¼
v_Sql_Str VARCHAR2(320); --å¨ææ§è¡SQLè¯å¥
BEGIN
/*è·åå½æ°åï¼å¯ä»¥ä»æ°æ®åºä¸å¨æ读åéè¦æ§è¡çå½æ°ï¼æ¬DEMOç´æ¥èµå¼*/
v_Function_Name := 'Return_Str_f(:i_Str1)';
/*æ¬DEMOåå®éè¦è°ç¨çå½æ°æ1ä¸ªä¼ å
¥åæ°*/
v_Input_Parameter1 := 'test01'; --ä¼ å
¥åæ°1,å¨å®é
åºç¨ä¸æ ¹æ®å
·ä½æ
åµæºåèµå¼
/*å¨æçæéè¦æ§è¡å½æ°çSQLè¯å¥*/
v_Sql_Str := 'begin :v_Return_String := ' || v_Function_Name || '; end;';
/*æ§è¡è¯¥SQLè¯å¥*/
EXECUTE IMMEDIATE v_Sql_Str
USING OUT v_Return_String, IN v_Input_Parameter1; --æ¥æ¶è¿åå¼å¿
é¡»å¨åï¼ä¼ å
¥åæ°ç±»ååä¼ é顺åºå¿
é¡»ä¸å½æ°ä¸ä¿æä¸è´
/*è¾åºè¿åå¼*/
Dbms_Output.Put_Line(v_Return_String);
END;
/*æè°ç¨å½æ°*/
CREATE OR REPLACE FUNCTION Return_Str_f(i_Str1 VARCHAR2) RETURN VARCHAR2 IS
v_Sql_Str VARCHAR2(320);
v_Str1 VARCHAR2(320);
BEGIN
/*çæå¨ææ§è¡SQL*/
v_Sql_Str := 'SELECT ''' || i_Str1 || ''' FROM DUAL';
EXECUTE IMMEDIATE v_Sql_Str
INTO v_Str1; --æ¥æ¶è¿åå¼
/*è¾åºè¿åç»æ*/
Dbms_Output.Put_Line('v_str1=' || v_Str1);
RETURN v_Str1;
END;
ç¸å
³èµæï¼
Oracle PL/SQLä¸å¨ææ§è¡SQL EXECUTE IMMEDIATE
å¨plsqlä¸ç»å¸¸éå°sqlè¯å¥æ¯å¨è¿ç¨ä¸å¨æçæçï¼è¿ä¸ªæ¶åå¯ä»¥ç¨EXECUTE IMMEDIATEæ¥æ§è¡çæçsqlè¯å¥ã转ä¸ä¸ªjavaeyeï¼è²ä¼¼ä»ä¹æ¯è½¬çï¼æ¾ä¸å°åºå¤ï¼ä¸çEXECUTE IMMEDIATEç¨æ³ã
EXECUTE IMMEDIATE代æ¿äºä»¥åOracle8iä¸DBMS_SQL packageå
.å®è§£æ并马ä¸æ§è¡å¨æçSQLè¯å¥æéè¿è¡æ¶å建çPL/SQLå.å¨æå建åæ§è¡SQLè¯å¥æ§è½è¶
åï¼EXECUTE IMMEDIATEçç®æ å¨äºåå°ä¼ä¸è´¹ç¨å¹¶è·å¾è¾é«çæ§è½ï¼è¾ä¹ä»¥åå®ç¸å½è½»æç¼ç .尽管DBMS_SQLä»ç¶å¯ç¨ï¼
ä½æ¯æ¨è使ç¨EXECUTE IMMEDIATE,å 为å®è·çæ¶çå¨å
ä¹ä¸ã
使ç¨æå·§
1. EXECUTE IMMEDIATEå°ä¸ä¼æ交ä¸ä¸ªDMLäºå¡æ§è¡ï¼åºè¯¥æ¾å¼æ交
åå¦éè¿EXECUTE IMMEDIATEå¤çDMLå½ä»¤ï¼é£ä¹å¨å®æ以åéè¦æ¾å¼æ交æè
ä½ä¸ºEXECUTE IMMEDIATEèªå·±çä¸é¨å. åå¦éè¿EXECUTE IMMEDIATEå¤çDDLå½ä»¤,å®æ交ææ以åæ¹åçæ°æ®
2. ä¸æ¯æè¿åå¤è¡çæ¥è¯¢,è¿ç§äº¤äºå°ç¨ä¸´æ¶è¡¨æ¥åå¨è®°å½(åç
§ä¾åå¦ä¸)æè
ç¨REF cursors.
3. å½æ§è¡SQLè¯å¥æ¶ï¼ä¸è¦ç¨åå·ï¼å½æ§è¡PL/SQLåæ¶ï¼å¨å
¶å°¾é¨ç¨åå·.
4. å¨Oracleæåä¸ï¼æªå
·ä½è¦çè¿äºåè½ãä¸é¢çä¾åå±ç¤ºäºææç¨å°Execute immediateçå¯è½æ¹é¢.å¸æè½ç»ä½ 带æ¥æ¹ä¾¿.
5. 对äºFormså¼åè
,å½å¨PL/SQL 8.0.6.3.çæ¬ä¸ï¼Forms 6iä¸è½ä½¿ç¨æ¤åè½.
EXECUTE IMMEDIATEç¨æ³ä¾å
1. å¨PL/SQLè¿è¡DDLè¯å¥
begin
execute immediate 'set role all';
end;
2. ç»å¨æè¯å¥ä¼ å¼(USING åå¥)
declare
l_depnam varchar2(20) := 'testing';
l_locvarchar2(10) := 'Dubai';
begin
execute immediate 'insert into dept values(:1, :2, :3)'
using 50, l_depnam, l_loc;
commit;
end;
3. ä»å¨æè¯å¥æ£ç´¢å¼(INTOåå¥)
declare
l_cntvarchar2(20);
begin
execute immediate 'select count(1) from emp'
into l_cnt;
dbms_output.put_line(l_cnt);
end;
4. å¨æè°ç¨ä¾ç¨.ä¾ç¨ä¸ç¨å°çç»å®åéåæ°å¿
é¡»æå®åæ°ç±»å.é»è®¤ä¸ºINç±»å,å
¶å®ç±»åå¿
é¡»æ¾å¼æå®
declare
l_routin varchar2(100) := 'gen2161.get_rowcnt';
l_tblnam varchar2(20) := 'emp';
l_cntnumber;
l_status varchar2(200);
begin
execute immediate 'begin ' l_routin '(:2, :3, :4); end;'
using in l_tblnam, out l_cnt, in out l_status;
if l_status != 'OK' then
dbms_output.put_line('error');
end if;
end;
5. å°è¿åå¼ä¼ éå°PL/SQLè®°å½ç±»å;åæ ·ä¹å¯ç¨%rowtypeåé
declare
type empdtlrec is record (empnonumber(4),
enamevarchar2(20),
deptnonumber(2));
empdtl empdtlrec;
begin
execute immediate 'select empno, ename, deptno '
'from emp where empno = 7934'
into empdtl;
end;
6. ä¼ é并æ£ç´¢å¼.INTOåå¥ç¨å¨USINGåå¥å
declare
l_deptpls_integer := 20;
l_nam varchar2(20);
l_loc varchar2(20);
begin
execute immediate 'select dname, loc from dept where deptno = :1'
into l_nam, l_loc
using l_dept ;
end;
7. å¤è¡æ¥è¯¢é项.对æ¤é项ç¨insertè¯å¥å¡«å
临æ¶è¡¨ï¼ç¨ä¸´æ¶è¡¨è¿è¡è¿ä¸æ¥çå¤ç,ä¹å¯ä»¥ç¨REF cursorsçº æ£æ¤ç¼ºæ¾.
declare
l_sal pls_integer := 2000;
begin
execute immediate 'insert into temp(empno, ename) '
'select empno, ename from emp '
'wheresal :1'
using l_sal;
commit;
end;
对äºå¤çå¨æè¯å¥,EXECUTE IMMEDIATEæ¯ä»¥åå¯è½ç¨å°çæ´è½»æ并ä¸æ´é«æ.å½æå¾æ§è¡å¨æè¯å¥æ¶ï¼éå½å°å¤çå¼å¸¸æ´å éè¦.åºè¯¥å
³æ³¨äºææææå¯è½çå¼å¸¸.
å¦è¥æ»¡æï¼è¯·ç¹å»å³ä¾§ãé纳çæ¡ãï¼å¦è¥è¿æé®é¢ï¼è¯·ç¹å»ã追é®ã
å¸ææçåç对æ¨ææ帮å©ï¼æé纳ï¼
温馨提示:答案为网友推荐,仅供参考