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)