佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > plsql與tsql的語(yǔ)法不同

plsql與tsql的語(yǔ)法不同

熱門標(biāo)簽:AI電銷 網(wǎng)站排名優(yōu)化 百度競(jìng)價(jià)排名 地方門戶網(wǎng)站 服務(wù)外包 Linux服務(wù)器 呼叫中心市場(chǎng)需求 鐵路電話系統(tǒng)
insert into testtable(recordnumber,currentdate) values (i,sysdate);
print ‘';
select @i=@i+1;
end;

比較一下就可以看出來到底那里不一樣了

plsql里面命令的結(jié)構(gòu)為
delacre
定義語(yǔ)句段
begin
執(zhí)行語(yǔ)句段
exception
異常處理語(yǔ)句段
end
這就是plsql程序總體結(jié)構(gòu)圖

定義變量與mssql的不同
基本方法
變量名 類型標(biāo)識(shí)符【notnull】:=值
例 age number(8):=26
多了定義復(fù)合數(shù)據(jù)類型變量的功能
1.多了%type 變量
declare
mydate user。testtable.currentdate%type;
還有 %rowtype類型的變量可以識(shí)變量獲得字段的數(shù)據(jù)類型,使用%rowtype可以識(shí)變量獲得整個(gè)記錄的數(shù)據(jù)類型。
變量名 數(shù)據(jù)表.列名%type
變量名 數(shù)據(jù)表%rowtype
declare
mytable testtbale%rowtype 包含了testtable 所有字段 只不過在輸出時(shí)候可以選擇輸出那個(gè)
begin
shelect * into mytable
from temuuser.tedttbale
where recordnumber=88
dbms_output.put_line(mytable.currentdate);
end;
還有就是有了定義符合變量
格式
type 復(fù)合變量名 is record(
變量 類型, 可以有好幾個(gè));
變量名 復(fù)合變量名 這個(gè)變量名就好像java中類的對(duì)象一樣而復(fù)合變量名就是類名可以這樣理解 個(gè)人觀點(diǎn)
begin
select * into 變量名 from 表名 where 條件
dbms_output.put_line(變量名.表中的值)
end

另外還可以定義一維數(shù)組
type 表類型 is table of 類型 index by binary_integer
表變量名 表類型
index by binary_integer子句代表以符號(hào)整數(shù)為索引,
這樣訪問表類型變量中的數(shù)據(jù)方法就是“表變量名(索引符號(hào)整數(shù))”

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):='大學(xué)';
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;
一個(gè)標(biāo)準(zhǔn)的一維數(shù)組

定義多維表類型變量
定義了名為 tabletype1 的多維表類型,相當(dāng)于多維數(shù)組,table1 是多維表類型變量,將數(shù)據(jù)表 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;

在來看下面的這個(gè)程序
set serveroutput on
Declare
result integer;
begin
result:=10+3*4-20+5**2;
dbms_output.put_line('運(yùn)算結(jié)果是:'||to_char(result));
end;

|| 這個(gè)符號(hào)是連接語(yǔ)句
to_char(result) dbms_output.put_line函數(shù)輸出只能是字符串,因此利用 to_char函數(shù)將數(shù)值型結(jié)果轉(zhuǎn)換為字符型。
To_char:將其他類型數(shù)據(jù)轉(zhuǎn)換為字符型。 To_date:將其他類型數(shù)據(jù)轉(zhuǎn)換為日期型。 To_number:將其他類型數(shù)據(jù)轉(zhuǎn)換為數(shù)值型。

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

1. if..then..end if條件控制
if 條件 then
語(yǔ)句段;
end if;

2. if..then..else..end if條件控制
if 條件 then
語(yǔ)句段1;
else
語(yǔ)句段2;
end if;

3. if 嵌套條件控制
if 條件1 then
if 條件2 then
語(yǔ)句段1;
else
語(yǔ)句段2;
end if;
else
語(yǔ)句段3;
end if;

4.loop..exit..end loop 循環(huán)控制
loop
循環(huán)語(yǔ)句段;
if 條件語(yǔ)句 then
exit;
else
退出循環(huán)的處理語(yǔ)句段
end if;
end loop;

5. loop..exit..when..end loop 循環(huán)控制
采用 loop..exit..when..end loop 循環(huán)控制的語(yǔ)法結(jié)構(gòu)與loop..exit..end loop 循環(huán)控制類似
exit when 實(shí)際上就相當(dāng)于
if 條件 then
exit;
end if;

6.while..loop..end loop 循環(huán)控制
while 條件 loop
執(zhí)行語(yǔ)句段
end loop;

7.for..in..loop..end 循環(huán)控制
for 循環(huán)變量 in [reverse] 循環(huán)下界..循環(huán)上界 loop
循環(huán)處理語(yǔ)句段;
end loop;
最后一個(gè)出個(gè)例子
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;
本人學(xué)java 的 對(duì)plsql一看覺的很簡(jiǎn)單 和java比起來簡(jiǎn)單多了但是oracle 命令只是一部分更多的東西需要我去學(xué)習(xí) 自夸一下 哈哈

在plsql 多了事務(wù)處理命令

commit命令
commit事務(wù)提交命令。在oracle中為了保證數(shù)據(jù)的一致性在內(nèi)存中將為每個(gè)客戶機(jī)建立工作區(qū),就是說在用commit命令之前的操作都在這個(gè)工作群里完成,只有在用commit命令之后才會(huì)把你寫的命令寫入到數(shù)據(jù)庫(kù)中。
有個(gè)自動(dòng)進(jìn)行事務(wù)提交的命令
set auto on
關(guān)閉為 set auto off

rollback命令
rollback是事務(wù)回滾命令,在沒有提交commit命令千,如果發(fā)現(xiàn)delete insert update等操需要恢復(fù)的話,可以用rollback命令會(huì)滾到上次commit時(shí)的狀態(tài)。
set auto off 要先關(guān)閉自動(dòng)提交
select * from scott.emp;
delete form scott.emp;
rollback
這個(gè)時(shí)候就可以看到 scott.emp還是以前的沒有變化

savepoint命令
這個(gè)命令時(shí)保存點(diǎn)命令。事務(wù)通常由多個(gè)命令組成,可以將每個(gè)事務(wù)劃分成若干個(gè)部分進(jìn)行保存,這樣回滾每個(gè)保存點(diǎn),就不必回滾整個(gè)事務(wù)。
創(chuàng)建保存點(diǎn) savepoint 保存點(diǎn)名
回滾保存點(diǎn) rollback to 保存點(diǎn)名
來個(gè)例子
insert into scott.emp(empno,ename,sal) values(9000,'wang',2500); 先插入一個(gè)值
savepoint insertpoint; 創(chuàng)建一個(gè)還原點(diǎn),名字叫insertpoint
rollback to insertpoint; 還原到那個(gè)還原點(diǎn)

下面開始說游標(biāo)
這個(gè)東西在mssql里沒有吧 我沒有印象
游標(biāo)不是c里面的指針,我一看到這個(gè)詞就想到了指針可惜何c里面的指針大不一樣 不要弄混了 估計(jì)沒人會(huì)弄混。
游標(biāo)可以說是一個(gè)臨時(shí)的數(shù)據(jù)存放的地方
要用游標(biāo)先要定義
cursor 游標(biāo)名 is select 語(yǔ)句
cursor這是游標(biāo)的關(guān)鍵字 selcet建立游標(biāo)的查詢命令
看個(gè)例子
set serveroutput on
declare
tempsal scott.emp.sal%type 定義了一個(gè)變量他是scott.emp.sal同一個(gè)類型
cursor mycursor is 定義一個(gè)游標(biāo)mycursor
select * from scott.emp
where sal>tempsal;
begin
tempsal:=800;
open mycursor; 打開這個(gè)游標(biāo)
end;
暈忘了 只是打開游標(biāo)沒有用 還要提取游標(biāo)的數(shù)據(jù)
用fetch命令
fetch 游標(biāo)名 into 變量1,變量2,。。。。;
或者
fetch 游標(biāo)名 into 記錄型變量名;
上面那個(gè)程序要改一下

set serveroutput on
declare
tempsal scott.emp.sal%type 定義了一個(gè)變量他是scott.emp.sal同一個(gè)類型
cursor mycursor is 定義一個(gè)游標(biāo)mycursor
select * from scott.emp
where sal>tempsal
new scott.emp%rowtype; 有定義了一個(gè)新的變量
begin
tempsal:=800;
open mycursor; 打開這個(gè)游標(biāo)
fetch mycursor into new; 讀取游標(biāo)數(shù)據(jù)把他添加到new中
dbms_output._line(to_char(new.sal)); 顯示結(jié)果
close mysursor; close關(guān)閉這個(gè)游標(biāo)
end;

游標(biāo)的屬性
1.%isopen屬性
就是判斷游標(biāo)是否打開,如果沒有打開就是用fetch語(yǔ)句提示錯(cuò)誤
2.%found屬性
就是測(cè)試前一個(gè)fetch語(yǔ)句是否有值,有就返回true 沒有就返回false
3.%notfound屬性 和上面的相反
4.%rowcount屬性 判斷游標(biāo)的數(shù)據(jù)行數(shù)就是有多少數(shù)據(jù)

下面說下過程的概念 sql里沒有
完整的過程的結(jié)構(gòu)如下
create or replace 過程名 as
聲明語(yǔ)句段;
begin
執(zhí)行語(yǔ)句段;
exception
異常處理語(yǔ)句段;
end;
過程是有名稱的程序塊,as關(guān)鍵詞代替了無(wú)名塊的declare

創(chuàng)建實(shí)例的過程
創(chuàng)建一個(gè)名為tempprocdeure的過程,create是創(chuàng)建過程的標(biāo)識(shí)符,replace表示如果又同名的過程將覆蓋原過程。定義了一個(gè)變量,其類型何testtable數(shù)據(jù)表中的currentdate字段的類型相同,都是日期型,將數(shù)據(jù)表中的recordnumber字段為88的 currentdate字段內(nèi)容送入變量中,然后輸出結(jié)果。

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;
下面說下帶參數(shù)的過程
1.參數(shù)類型
in 讀入?yún)?shù) 程序向過程傳遞數(shù)值
out 讀出參數(shù) 過程向程序傳遞數(shù)值
in out 雙向參數(shù) 程序過程互相傳遞數(shù)值
定義帶參數(shù)的過程
set serveroutput on
creat or replace procedure scott.tempprocedure(
tempdeptno in scott.dept.deptno%type,/*定義了一個(gè)in類型的變量*/
tempdname out scott.dept.danme%type,/*定義了一個(gè)out類型的變量*/
temploc in out scott.dept.loc%type)as /*定義了一個(gè)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;
搞定了
就是說用重新定義的三個(gè)變量當(dāng)參數(shù)傳遞給上面定義的過程過程里帶參數(shù)的變量可以接受這三個(gè)變量的值
用java語(yǔ)言來解釋就是那個(gè)過程就是類 帶三個(gè)參數(shù)
這三個(gè)變量就是數(shù)據(jù) 當(dāng)然沒有對(duì)象了哈哈畢竟不是java么哈哈

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

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

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

下面的 PL/SQL 程序包含了完整的異常處理定義、觸發(fā)、處理的過程。定義名為 salaryerror
的異常,在 scott.emp 數(shù)據(jù)表中查找 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;
您可能感興趣的文章:
  • PLSQL Developer登錄的默認(rèn)密碼介紹
  • Plsql Developer連接Oracle時(shí)出現(xiàn)Could not initialize oci.dll解決方案
  • Windows 64位下裝安裝Oracle 11g,PLSQL Developer的配置問題,數(shù)據(jù)庫(kù)顯示空白的完美解決方案(圖文教程)
  • Oracle基本PLSQL的使用實(shí)例詳解
  • win7 64位操作系統(tǒng)中Oracle 11g + plsql安裝教程詳解(圖解)
  • Oracle客戶端與plsql查詢數(shù)據(jù)亂碼修改成中文的快速解決方法
  • oracle(plsql)生成流水號(hào)
  • 解決plsql遇到亂碼的問題
  • plsql連接oracle數(shù)據(jù)庫(kù)報(bào)ora 12154錯(cuò)誤解決方法
  • oracle客戶端PLSQL連接失敗解決方法
  • PLSQL developer12漢化過程

標(biāo)簽:衡水 蘭州 銅川 湘潭 仙桃 黃山 崇左 湖南

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《plsql與tsql的語(yǔ)法不同》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    安阳县| 江口县| 郸城县| 延庆县| 太仓市| 久治县| 石嘴山市| 宁远县| 武清区| 石棉县| 托里县| 庆元县| 宽城| 铅山县| 乌兰浩特市| 六枝特区| 定陶县| 扎囊县| 获嘉县| 静乐县| 手游| 文山县| 临澧县| 崇阳县| 许昌市| 大化| 化州市| 安义县| 瑞昌市| 吴桥县| 耒阳市| 宁津县| 凤台县| 重庆市| 湘西| 扶绥县| 泾阳县| 绥阳县| 静乐县| 韶山市| 石柱|