pl/sql程序編寫中遇到的一些問題及解決辦法
2024-07-21 02:06:35
供稿:網友
1、在pl/sql中,order by子句中的條件可以使用變量!
declare
v_orderbystr varchar2(30);
v_userid varchar2(30);
v_username varchar2(30);
v_gender number;
v_rownum number;
type tcur is ref cursor;
results tcur;
begin
v_rownum:=0;
v_orderbystr:='username';
open results for select userid,username,gender from
(select rownum as rowno, a.* from
(select * from home_user order by v_orderbystr) a
where rownum<10)
where rowno>=1;
loop
fetch results into v_userid,v_username,v_gender;
exit when results%notfound;
dbms_output.put_line(v_userid||' '||v_username||' '||v_gender);
v_rownum:=v_rownum+1;
end loop;
close results;
dbms_output.put_line(v_rownum);
end;
2、而在寫動態sql的存儲過程中,發現在使用using子句時 ,發現不能把表名作為占位符的參數!而只能通過下邊的辦法來替代,即直接將表名與字符串相連,其他的變量則可以被占位符來替代;
v_sqlstr:='select * from(select rownum rowno,t.* from'
||'(select sequenceid msgid,themeid,id,topic,hits,replys,nickname'
||' from '||tablename||' where themeid=:a2 order by :a3) t where rownum<:a4'
||') where rowno>=:a5';
dbms_output.put_line(v_sqlstr);
open o_results for v_sqlstr using p_themeid,v_orderbystr,v_endrow,v_startrow;
3、在做一些翻頁查詢時,使用了偽列rownum,發現rownum只能用于rownum<10之類的應用,而不能是rownum>10;上例中實現了同時翻頁的功能;
4、利用已經存在的表建立一個新表,并復制源表的表結構:
create table newtable as (select * oldtable where 1=2)