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)