oracle儲存過程中如何執行動態SQL語句

2021-04-22 04:58:50 字數 1104 閱讀 8699

有時需要在oracle儲存過程中執行動態sql語句 ,例如表名是動態的,或字段是動態的,或查詢命令是動態的,可用下面的方法:

set serveroutput on

declare

n number;

sql_stmt varchar2(50);

t varchar2(20);

begin

execute immediate 'alter session set nls_date_format=''yyyymmdd''';

t := 't_' || sysdate;

sql_stmt := 'select count(*) from ' || t;

execute immediate sql_stmt into n;

dbms_output.put_line('the number of rows of ' || t || ' is ' || n);

end;

如果動態sql語句 很長很複雜,則可用包裝.

create or replace package test_pkg

istype cur_typ is ref cursor;

procedure test_proc (v_table varchar2,t_cur out cur_typ);

end;

/create or replace package body test_pkg

isprocedure test_proc (v_table varchar2,t_cur out cur_typ)

issqlstr varchar2(2000);

begin

sqlstr := 'select * from '||v_table;

open t_cur for sqlstr;

end;

end;

/在oracle中批量匯入,匯出和刪除表名以某些字元開頭的表

spool   c:/a.sql  

select   'drop table ' || tname || ';' from tab where tname   like   't%';  

spool   off  

@c:/a

oracle在儲存過程中執行DDL語句

預設情況下,oracle對儲存過程是使用所有者許可權,也就是說 如果使用者b條用a 使用者下的儲存過程,使用的是a使用者的物件許可權和系統許可權。如果a使用者沒有許可權的話,使用者b執 行就會報錯。所以第一種辦法就是授予使用者執行儲存過程中ddl的許可權。另一種辦法是通過在儲存過程中使用authid...

如何在儲存過程中執行DTS包

資料轉換服務 dts 在管理和開發的多種領域都有會涉及 dts 資料倉儲 將資料從原始的處理系統和 中提取出來以供報表使用 建立olap 將大量資料從文字檔案或其它非資料庫格式的檔案中拷貝到資料庫 生成microsoftoffice文件報表 使用distributedtransactioncoord...

Oracle儲存過程中如何使用游標

本儲存過程的功能 把test tbl2中與test tbl1中id相同但salary不同的記錄中的salary的值更新為test tbl1中的salary的值 建立儲存過程 create or replace procedure p update test tbl2 is 定義游標 cursor c...