程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> Oracle數據庫 >> 關於Oracle數據庫 >> plsql與tsql的語法不同

plsql與tsql的語法不同

編輯:關於Oracle數據庫
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;
一個標准的一維數組

定義多維表類型變量
定義了名為 tabletype1 的多維表類型,相當於多維數組,table1 是多維表類型變量,將數據表 tempuser.testtable 中
recordnumber為 60 的記錄提取出來存放在 table1 中並顯示。

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;

|| 這個符號是連接語句
to_char(result) dbms_output.put_line函數輸出只能是字符串,因此利用 to_char函數將數值型結果轉換為字符型。
To_char:將其他類型數據轉換為字符型。 To_date:將其他類型數據轉換為日期型。 To_number:將其他類型數據轉換為數值型。

再說下plsql中的控制語句組合有哪幾種

1. if..then..end if條件控制
if 條件 then
語句段;
end if;

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;

游標的屬性
1.%isopen屬性
就是判斷游標是否打開,如果沒有打開就是用fetch語句提示錯誤
2.%found屬性
就是測試前一個fetch語句是否有值,有就返回true 沒有就返回false
3.%notfound屬性 和上面的相反
4.%rowcount屬性 判斷游標的數據行數就是有多少數據

下面說下過程的概念 sql裡沒有
完整的過程的結構如下
create or replace 過程名 as
聲明語句段;
begin
執行語句段;
exception
異常處理語句段;
end;
過程是有名稱的程序塊,as關鍵詞代替了無名塊的declare

創建實例的過程
創建一個名為tempprocdeure的過程,create是創建過程的標識符,replace表示如果又同名的過程將覆蓋原過程。定義了一個變量,其類型何testtable數據表中的currentdate字段的類型相同,都是日期型,將數據表中的recordnumber字段為88的 currentdate字段內容送入變量中,然後輸出結果。

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;

begin
myno:=10;
mydname:=”;
myloc:=”;
scott.tempprocedure(myno,mydname,myloc);
dbms_output.put_line(myno);
dbms_output.put_line(mydname);
dbms_output.put_line(myloc);
end;
搞定了
就是說用重新定義的三個變量當參數傳遞給上面定義的過程過程裡帶參數的變量可以接受這三個變量的值
用java語言來解釋就是那個過程就是類 帶三個參數
這三個變量就是數據 當然沒有對象了哈哈畢竟不是java麼哈哈

今天寫到這裡了 我要下班了 7.3

異常處理
就是程序中要處理特殊情況的辦法

1. 定義異常處理
定義異常處理的語法如下:
declare
異常名 exception;
2. 觸發異常處理
觸發異常處理的語法如下:
raise 異常名;
3. 處理異常
觸發異常處理後,可以定義異常處理部分,語法如下:
Exception
When 異常名 1 then
異常處理語句段 1;
When 異常名 2 then
異常處理語句段 2;

下面的 PL/SQL 程序包含了完整的異常處理定義、觸發、處理的過程。定義名為 salaryerror
的異常,在 scott.emp 數據表中查找 empno=7566 的記錄,將其值放入變量 tempsal 中,判斷
tempsal 值若不在 900 和2600 之間,說明該員工的薪水有問題,將激活異常處理,提示信息。

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;
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved