insert into testtable(recordnumber,currentdate) values (i,sysdate); print ‘'; select @i=@i+1; end;
比較一下就可以看出來到底那里不一樣了
plsql里面命令的結構為 delacre 定義語句段 begin 執行語句段 exception 異常處理語句段 end 這就是plsql程序總體結構圖
定義變量與mssql的不同 基本方法 變量名 類型標識符【notnull】:=值 例 age number(8):=26 多了定義復合數據類型變量的功能 1.多了%type 變量 declare mydate user。testtable.currentdate%type; 還有 %rowtype類型的變量可以識變量獲得字段的數據類型,使用%rowtype可以識變量獲得整個記錄的數據類型。 變量名 數據表.列名%type 變量名 數據表%rowtype declare mytable testtbale%rowtype 包含了testtable 所有字段 只不過在輸出時候可以選擇輸出那個 begin shelect * into mytable from temuuser.tedttbale where recordnumber=88 dbms_output.put_line(mytable.currentdate); end; 還有就是有了定義符合變量 格式 type 復合變量名 is record( 變量 類型, 可以有好幾個); 變量名 復合變量名 這個變量名就好像java中類的對象一樣而復合變量名就是類名可以這樣理解 個人觀點 begin select * into 變量名 from 表名 where 條件 dbms_output.put_line(變量名.表中的值) end
另外還可以定義一維數組 type 表類型 is table of 類型 index by binary_integer 表變量名 表類型 index by binary_integer子句代表以符號整數為索引, 這樣訪問表類型變量中的數據方法就是“表變量名(索引符號整數)”
Declare type tabletype1 is table of varchar2(4) index by binary_integer; type tabletype2 is table of tempuser.testtable.recordnumber%type index by binary_integer; table1 tabletype1; table2 tabletype2; begin table1(1):='大學'; table1(2):='大專'; table2(1):=88; table2(2):=55; dbms_output.put_line(table1(1)||table2(1)); dbms_output.put_line(table1(2)||table2(2)); end; 一個標準的一維數組
type tabletype1 is table of testtable%rowtype index by binary_integer; table1 tabletype1; begin select * into table1(60) from tempuser.testtable where recordnumber=60; dbms_output.put_line(table1(60).recordnumber||table1(60).currentdate); end;
在來看下面的這個程序 set serveroutput on Declare result integer; begin result:=10+3*4-20+5**2; dbms_output.put_line('運算結果是:'||to_char(result)); end;
2. if..then..else..end if條件控制 if 條件 then 語句段1; else 語句段2; end if;
3. if 嵌套條件控制 if 條件1 then if 條件2 then 語句段1; else 語句段2; end if; else 語句段3; end if;
4.loop..exit..end loop 循環控制 loop 循環語句段; if 條件語句 then exit; else 退出循環的處理語句段 end if; end loop;
5. loop..exit..when..end loop 循環控制 采用 loop..exit..when..end loop 循環控制的語法結構與loop..exit..end loop 循環控制類似 exit when 實際上就相當于 if 條件 then exit; end if;
6.while..loop..end loop 循環控制 while 條件 loop 執行語句段 end loop;
7.for..in..loop..end 循環控制 for 循環變量 in [reverse] 循環下界..循環上界 loop 循環處理語句段; end loop; 最后一個出個例子 set serveroutput on declare number1 integer:=80; number2 integer:=90; i integer:=0; begin for i in 1..10 loop number1:=number1+1; 在mssql里是 sclect @number=@number+1 end loop; dbms_output.put_line('number1的值:'||to_char(number1)); end; 本人學java 的 對plsql一看覺的很簡單 和java比起來簡單多了但是oracle 命令只是一部分更多的東西需要我去學習 自夸一下 哈哈
在plsql 多了事務處理命令
commit命令 commit事務提交命令。在oracle中為了保證數據的一致性在內存中將為每個客戶機建立工作區,就是說在用commit命令之前的操作都在這個工作群里完成,只有在用commit命令之后才會把你寫的命令寫入到數據庫中。 有個自動進行事務提交的命令 set auto on 關閉為 set auto off
rollback命令 rollback是事務回滾命令,在沒有提交commit命令千,如果發現delete insert update等操需要恢復的話,可以用rollback命令會滾到上次commit時的狀態。 set auto off 要先關閉自動提交 select * from scott.emp; delete form scott.emp; rollback 這個時候就可以看到 scott.emp還是以前的沒有變化
savepoint命令 這個命令時保存點命令。事務通常由多個命令組成,可以將每個事務劃分成若干個部分進行保存,這樣回滾每個保存點,就不必回滾整個事務。 創建保存點 savepoint 保存點名 回滾保存點 rollback to 保存點名 來個例子 insert into scott.emp(empno,ename,sal) values(9000,'wang',2500); 先插入一個值 savepoint insertpoint; 創建一個還原點,名字叫insertpoint rollback to insertpoint; 還原到那個還原點
下面開始說游標 這個東西在mssql里沒有吧 我沒有印象 游標不是c里面的指針,我一看到這個詞就想到了指針可惜何c里面的指針大不一樣 不要弄混了 估計沒人會弄混。 游標可以說是一個臨時的數據存放的地方 要用游標先要定義 cursor 游標名 is select 語句 cursor這是游標的關鍵字 selcet建立游標的查詢命令 看個例子 set serveroutput on declare tempsal scott.emp.sal%type 定義了一個變量他是scott.emp.sal同一個類型 cursor mycursor is 定義一個游標mycursor select * from scott.emp where sal>tempsal; begin tempsal:=800; open mycursor; 打開這個游標 end; 暈忘了 只是打開游標沒有用 還要提取游標的數據 用fetch命令 fetch 游標名 into 變量1,變量2,。。。。; 或者 fetch 游標名 into 記錄型變量名; 上面那個程序要改一下
set serveroutput on declare tempsal scott.emp.sal%type 定義了一個變量他是scott.emp.sal同一個類型 cursor mycursor is 定義一個游標mycursor select * from scott.emp where sal>tempsal new scott.emp%rowtype; 有定義了一個新的變量 begin tempsal:=800; open mycursor; 打開這個游標 fetch mycursor into new; 讀取游標數據把他添加到new中 dbms_output._line(to_char(new.sal)); 顯示結果 close mysursor; close關閉這個游標 end;
set serveroutput on creat or replace procedure tempuser.tempprocedure as tempdate tempuser.testtable.currentdate%type;
begin select currentdate into tempdate from testtable where recordnumber=88; dbms_output.put_line(to_char(tempdate)); end; 使用過程 set serveroutput on begin tempprocedure; end; 下面說下帶參數的過程 1.參數類型 in 讀入參數 程序向過程傳遞數值 out 讀出參數 過程向程序傳遞數值 in out 雙向參數 程序過程互相傳遞數值 定義帶參數的過程 set serveroutput on creat or replace procedure scott.tempprocedure( tempdeptno in scott.dept.deptno%type,/*定義了一個in類型的變量*/ tempdname out scott.dept.danme%type,/*定義了一個out類型的變量*/ temploc in out scott.dept.loc%type)as /*定義了一個inout型的變量*/ loc1 scott.dept.doc%type; dname1 scott.dept.dname%type; begin select loc into loc1 from scott.dept where deptno=tempdeptno; select danme into danme1 from scott.dept where deptno=tempdeptno; temploc:='地址'||loc1; tempdname:='姓名'||dname1;
end;
定義好了 下面開始用了 set serveroutput on declare myno scott.dept.deptno%type; mydname scott.dept.dname%type; myloc scott.dept.loc%type;
set serveroutput on declare salaryerror exception; tempsal scott.emp.sal%type; begin select sal into tempsal from scott.emp where empno=7566; if tempsal 900 or tempsal>2600 then raise salaryerror; end if; exception when salaryerror then dbms_output.put_line('薪水超出范圍'); end;
您可能感興趣的文章:
PLSQL Developer登錄的默認密碼介紹
Plsql Developer連接Oracle時出現Could not initialize oci.dll解決方案
Windows 64位下裝安裝Oracle 11g,PLSQL Developer的配置問題,數據庫顯示空白的完美解決方案(圖文教程)