plsql與tsql的文法不同

來源:互聯網
上載者:User

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;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.