標籤:des style blog http 使用 os io 資料
Oracle基本文法集錦
1、表
create table test (names varchar2(12), dates date, num int, dou double); |
2、視圖
create or replace view vi_test as select * from test; |
3、同義字
create or replace synonym aa for dbusrcard001.aa; |
4、預存程序
create or replace produce dd(v_id in employee.empoy_id%type) as begin end dd; |
5、函數
create or replace function ee(v_id in employee%rowtype) return varchar(15) is var_test varchar2(15); begin return var_test; exception when others then end |
6、三種觸發器的定義
create or replace trigger ff alter delete on test for each row declare begin delete from test; if sql%rowcount < 0 or sql%rowcount is null then rais_replaction_err(-20004,"錯誤") end if end create or replace trigger gg alter insert on test for each row declare begin if :old.names = :new.names then raise_replaction_err(-2003,"編碼重複"); end if end
create or replace trigger hh for update on test for each row declare begin if updating then if :old.names <> :new.names then reaise_replaction_err(-2002,"關鍵字不能修改") end if end if end |
7、定義遊標
declare cursor aa is select names,num from test; begin for bb in aa loop if bb.names = "ORACLE" then end if end loop; end |
8、速度最佳化,前一語句不後一語句的速度快幾十倍
select names,dates from test,b where test.names = b.names(+) and b.names is null and b.dates > date(‘2003-01-01‘,‘yyyy-mm-dd‘)
select names,dates from test where names not in ( select names from b where dates > to_date(‘2003-01-01‘,‘yyyy-mm-dd‘)) |
9、尋找重複記錄
select names,num from test where rowid != (select max(rowid) from test b where b.names = test.names and b.num = test.num) |
10、尋找表TEST中時間最新的前10條記錄
select * from (select * from test order by dates desc) where rownum < 11 |
11、序號的產生
create sequence row_id minvalue 1 maxvalue 9999999999999999999999 start with 1 increment by 1 insert into test values(row_id.nextval,....) |
1.串連
---內串連
select * from dali.test1 a, dali.test2 b where a.a=b.a;
---左串連
select * from dali.test1 a, dali.test2 b where a.a=b.a(+);
---右串連
select * from dali.test1 a, dali.test2 b where a.a(+)=b.a;
---完全串連
select * from dali.test1 a, dali.test2 b where a.a=b.a(+)
union
select * from dali.test1 a, dali.test2 b where a.a(+)=b.a;
---迪卡爾
select * from dali.test1, dali.test2;
判斷是否為空白:
在SQl Server中為ISNULL(field1,0)
在Oracle中為NVL(field1,0)
oracle 預存程序的基本文法
1.基本結構
CREATE OR REPLACE PROCEDURE 預存程序名字
(
參數1 IN NUMBER,
參數2 IN NUMBER
) IS
變數1 INTEGER :=0;
變數2 DATE;
BEGIN
END 預存程序名字
2.SELECT INTO STATEMENT
將select查詢的結果存入到變數中,可以同時將多個列儲存多個變數中,必須有一條
記錄,否則拋出異常(如果沒有記錄拋出NO_DATA_FOUND)
例子:
BEGIN
SELECT col1,col2 into 變數1,變數2 FROM typestruct where xxx;
EXCEPTION
WHEN NO_DATA_FOUND THEN
xxxx;
END;
...
3.IF 判斷
IF V_TEST=1 THEN
BEGIN
do something
END;
END IF;
4.while 迴圈
WHILE V_TEST=1 LOOP
BEGIN
XXXX
END;
END LOOP;
5.變數賦值 V_TEST := 123;
6.用for in 使用cursor
...
IS
CURSOR cur IS SELECT * FROM xxx;
BEGIN
FOR cur_result in cur LOOP
BEGIN
V_SUM :=cur_result.列名1+cur_result.列名2
END;
END LOOP;
END;
7.帶參數的cursor
CURSOR C_USER(C_ID NUMBER) IS SELECT NAME FROM USER WHERE TYPEID=C_ID;
OPEN C_USER(變數值);
LOOP
FETCH C_USER INTO V_NAME;
EXIT FETCH C_USER%NOTFOUND;
do something
END LOOP;
CLOSE C_USER;
8.用pl/sql developer debug 串連資料庫後建立一個Test WINDOW
在視窗輸入調用SP的代碼,F9開始debug,CTRL+N單步調試
關於oracle預存程序的若干問題備忘
1.在oracle中,資料表別名不能加as,如:
select a.appname from appinfo a;-- 正確
select a.appname from appinfo as a;-- 錯誤
也許,是怕和oracle中的預存程序中的關鍵字as衝突的問題吧
2.在預存程序中,select某一欄位時,後面必須緊跟into,如果select整個記錄,利用遊標的話就另當別論了。
select af.keynode into kn from APPFOUNDATION af where af.appid=aid and af.foundationid=fid;-- 有into,正確編譯
select af.keynode from APPFOUNDATION af where af.appid=aid and af.foundationid=fid;-- 沒有into,編譯報錯,提示:Compilation
Error: PLS-00428: an INTO clause is expected in this SELECT statement
3.在利用select...into...文法時,必須先確保資料庫中有該條記錄,否則會報出"no data found"異常。
可以在該文法之前,先利用select count(*) from 查看資料庫中是否存在該記錄,如果存在,再利用select...into...
4.在預存程序中,別名不能和欄位名稱相同,否則雖然編譯可以通過,但在運行階段會報錯
select keynode into kn from APPFOUNDATION where appid=aid and foundationid=fid;-- 正確運行
select af.keynode into kn from APPFOUNDATION af where af.appid=appid and af.foundationid=foundationid;-- 運行階段報錯,提示
ORA-01422:exact fetch returns more than requested number of rows
5.在預存程序中,關於出現null的問題
假設有一個表A,定義如下:
create table A(
id varchar2(50) primary key not null,
vcount number(8) not null,
bid varchar2(50) not null -- 外鍵
);
如果在預存程序中,使用如下語句:
select sum(vcount) into fcount from A where bid=‘xxxxxx‘;
如果A表中不存在bid="xxxxxx"的記錄,則fcount=null(即使fcount定義時設定了預設值,如:fcount number(8):=0依然無效,fcount還是會變成null),這樣以後使用fcount時就可能有問題,所以在這裡最好先判斷一下:
if fcount is null then
fcount:=0;
end if;
這樣就一切ok了。
6.Hibernate調用oracle預存程序
this.pnumberManager.getHibernateTemplate().execute(
new HibernateCallback() ...{
public Object doInHibernate(Session session)
throws HibernateException, SQLException ...{
CallableStatement cs = session
.connection()
.prepareCall("{call modifyapppnumber_remain(?)}");
cs.setString(1, foundationid);
cs.execute();
return null;
}
});