Oracle動態SQL語句

2021-05-06 07:09:27 字數 3550 閱讀 4410

在使用odp.net進行oracle程式設計時,有時候sql語句非常複雜,需要採用動態構造查詢語句的情況,有兩種方法可以構造動態的sql語句,並執行返回結果集。

1、在資料訪問層構造sql語句

例如下面的語句,將構造完整的sql語句賦值給commandtext,再傳遞到資料庫進行執行,返回結果集。

loadcommand.commandtype 

=commandtype.text

loadcommand.commandtext = "select * from users"

dataadapter .selectcommand = loadcommand

dataadapter . fill(data)

dataadapter .selectcommand = loadcommand

dataadapter . fill(data)

該方法需要將整個sql的構造過程放在dataaccess層,業務邏輯發生變化,修改不方便,而且每次查詢需要傳遞給資料庫很長的查詢字串,傳遞引數的效率也不高。

2、在儲存過程中構造動態sql語句並執行

以下為乙個完整的事例(經過刪減),其中refcursor 為自定義游標型別

procedure

g_search(p_yearno      

innumber

,p_controltype 

innumber

,p_progress    

inchar

,p_departid    

invarchar2

,p_projectname 

innvarchar2,

c_projects    out refcursor) 

ise_errinterruption exception;

v_errid       

number

; --

variable to hold the errorlog id

v_errcode     

number

; --

variable to hold the error message code

v_errtext     

varchar2

(512

); --

variable to hold the error message text

v_errproc     

varchar2(50

) :=

'g_search';

v_departid    

varchar2(16

);v_projectname nvarchar2(

128);

v_sql         

varchar2

(512

);v_where       

varchar2

(256

);begin

v_sql   :='

select projectid, parentid, projectname ';

v_sql   :

=v_sql ||'

from projects a';

v_where :='

where';

--年度

ifp_yearno 

<

9999

then

v_where :

=v_where ||'

a.yearno = '||

p_yearno ||'

and'

;else

v_where :

=v_where ||'

a.yearno < '||

p_yearno ||'

and'

;endif;

--控制類別

ifp_controltype =9

then

v_where :

=v_where ||'

a.controltype < 9 and';

else

v_where :

=v_where ||'

a.controltype = '||

p_controltype ||'

and'

;endif;

--進度

ifp_progress 

<'z

'then

v_where :

=v_where ||'

a.progress = 

'''||

p_progress 

||'''

and'

;else

v_where :

=v_where ||'

a.progress < 

'''||

p_progress 

||'''

and'

;endif;

iftrim(p_departid) 

<>'%

'then

v_where :

=v_where ||'

a.departid = 

'''||

p_departid 

||'''

and'

;else

v_where :

=v_where ||'

a.departid like 

'''||

p_departid 

||'''

and'

;endif;

--專案名稱

v_projectname :

=nvl(p_projectname,'%

');ifv_projectname 

<>'%

'then

v_projectname :='

%'||p_projectname ||'

%';endif;

v_where :

=v_where ||'

a.projectname like '||

''''

||v_projectname 

||'''

and'

;v_sql :

=v_sql 

||v_where;

open

c_projects 

forv_sql;

--commit;

exception

--根據需要定義錯誤異常

when

others 

then

--rollback;

v_errid   :

=sqlcode;

v_errtext :

=end

g_search;

該方法只需要傳遞給儲存過程一些引數,使用游標返回資料。引數傳遞效率較高,而且業務邏輯在儲存過程中,調整比較方便。該方法關鍵的在下面的語句:

open c_projects for v_sql;

它直接使用游標開啟構造的查詢字串即可。

注意事項:

Oracle動態sql語句

在pl sql程式開發中,可以使用dml語句和事務控制語句,但是還有很多語句 比如ddl語句 不能直接在pl sql中執行。這些語句可以使用動態的sql來實現。動態sql是指在pl sql塊編譯sql語句是不確定的。動態sql執行ddl語句 drop procedure procedure5 cre...

ORACLE 動態執行SQL語句

oracle 動態sql oracle 動態sql有兩種寫法 用 dbms sql 或 execute immediate,建議使用後者。試驗步驟如下 1.ddl 和 dml ddl begin execute immediate drop table temp 1 execute immediat...

ORACLE 動態執行SQL語句

oracle 動態sql oracle 動態sql有兩種寫法 用 dbms sql 或 execute immediate,建議使用後者。試驗步驟如下 1.ddl 和 dml sql ddl begin execute immediate drop table temp 1 execute imme...