最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Oracle存儲(chǔ)過(guò)程案例詳解

 更新時(shí)間:2021年08月13日 14:35:50   作者:weixin_41768626  
這篇文章主要介紹了Oracle存儲(chǔ)過(guò)程案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下

創(chuàng)建簡(jiǎn)單存儲(chǔ)過(guò)程(Hello World)

為了方便讀者簡(jiǎn)單易懂,我將下面使用到的表復(fù)制給大家。
具體表中的數(shù)據(jù),請(qǐng)大家自己填寫(xiě)

-- Create table
create table EMP
(
  empno    NUMBER(4) not null,
  ename    VARCHAR2(10),
  job      VARCHAR2(9),
  mgr      NUMBER(4),
  hiredate DATE,
  sal      NUMBER(7,2),
  comm     NUMBER(7,2),
  deptno   NUMBER(2)
)

在這里插入圖片描述

在這里插入圖片描述

create or replace procedure firstP(name in varchar2) is
/*這里name為的參數(shù),in為輸入,varchar2為類(lèi)型*/
begin
 /* dbms_output.put_line(); 相當(dāng)輸出到控制臺(tái)上,這樣我們一個(gè)簡(jiǎn)單的存儲(chǔ)過(guò)程就完成啦
 記住一句話(huà)的結(jié)束使用分號(hào)結(jié)束,存儲(chǔ)過(guò)程寫(xiě)完一定要執(zhí)行
 將它保存到數(shù)據(jù)庫(kù)中 (F8)快捷鍵,或者點(diǎn)擊左上角執(zhí)行*/
  dbms_output.put_line('我的名字叫'||name);/*dbms_output.put_line相當(dāng)于JAVA中的System.out.println("我的名字叫"+name);*/
end firstP;

下面我們要對(duì)剛剛寫(xiě)過(guò)的存儲(chǔ)過(guò)程進(jìn)行測(cè)試,我們開(kāi)啟Test Window這個(gè)窗口

在這里插入圖片描述

-- Created on 2018/12/30 星期日 by ADMINISTRATOR 
declare 
  -- Local variables here
  /*測(cè)試名稱(chēng) 名稱(chēng)類(lèi)型 使用 := 給參數(shù)賦值,在多說(shuō)一句,分號(hào)結(jié)束本句*/
  name2 varchar2(64):='數(shù)據(jù)庫(kù)';
begin
  -- Test statements here
  firstp(name2);
end;

我們打開(kāi)DBMS Output就可以看到執(zhí)行的存儲(chǔ)過(guò)程啦。

在這里插入圖片描述

在這里插入圖片描述

存儲(chǔ)過(guò)程IF判斷

create or replace procedure isifp(age in number) is
/*存儲(chǔ)過(guò)程if判斷以then開(kāi)始,以end if; 結(jié)束*/
begin
  if (age > 30) then
    dbms_output.put_line('我已經(jīng)超過(guò)30歲了');
  else
    if (age < 10) then
      dbms_output.put_line('我還是個(gè)兒童');
    else
      dbms_output.put_line('我正在奮斗時(shí)期');
    end if;
  end if;

end;

存儲(chǔ)過(guò)程輸出

create or replace procedure inandout(name in varchar2, age in number,outp out varchar2) is
/*in 代表輸入,out 代表輸出*/
begin
  outp:='my name is '|| name ||',my age is '||age;/*相當(dāng)于JAVA中的return outp,但是請(qǐng)注意,存儲(chǔ)過(guò)程中可以return多個(gè)值*/
end inandout;

測(cè)試輸出代碼

-- Created on 2018/12/30 星期日 by ADMINISTRATOR 
declare 
  -- Local variables here
  name varchar2(64):='數(shù)據(jù)庫(kù)';
  age number:=06;
  out_p varchar2(64);
begin
  -- Test statements here
  inandout(name,age,outp=>:out_p);
  /*這里的outp是存儲(chǔ)過(guò)程中的輸出參數(shù),out_p是在測(cè)試中使用的別名*/
end;

在這里插入圖片描述

返回游標(biāo)

create or replace procedure sysrefcursor(id in number, columnss out sys_refcursor) as
/*columnss out sys_refcursor  為輸出游標(biāo)*/
begin
  open columnss for
  select * from emp where empno=id;
end;

測(cè)試游標(biāo)

第一種測(cè)試方法

-- Created on 2018/12/30 星期日 by ADMINISTRATOR 
declare 
  -- Local variables here
 cursor ee is select * from emp where empno=7934;
begin
  -- Test statements here
  for e in ee loop
  dbms_output.put_line('deptno:'||e.deptno);
  end loop;
end;

輸出結(jié)果如下:

在這里插入圖片描述

第二種測(cè)試方法

-- Created on 2018/12/30 星期日 by ADMINISTRATOR 
declare 
  -- Local variables here
 cursor ee is select * from emp where empno=7934;
 cur ee % rowtype;
begin
  -- Test statements here
  open ee;
  loop
  fetch ee into cur;
  exit when ee%notfound;
  dbms_output.put_line('name:'||cur.ename);
  end loop;
  close ee;
end;

在這里插入圖片描述

上面測(cè)試結(jié)果僅僅返回一條數(shù)據(jù)。下面我來(lái)演示返回多條數(shù)據(jù)的情況。
首先請(qǐng)看我表中的數(shù)據(jù)

在這里插入圖片描述

有兩個(gè)job中內(nèi)容為CLERK的數(shù)據(jù)。

-- Created on 2018/12/30 星期日 by ADMINISTRATOR 
declare 
  -- Local variables here
 cursor ee is select * from emp where job='CLERK';
begin
  -- Test statements here
  for e in ee loop
  dbms_output.put_line('deptno:'||e.deptno);
  end loop;
end;

在這里插入圖片描述

游標(biāo)返回多條數(shù)據(jù)。

由于對(duì)于初學(xué)者來(lái)說(shuō),游標(biāo)可能不是很容易理解,下面我用JAVA語(yǔ)言來(lái)描述一下。
我們?cè)趈ava程序中寫(xiě)條件查詢(xún)的時(shí)候,返回出來(lái)的數(shù)據(jù)是List<泛型>。這個(gè)操作相當(dāng)于游標(biāo),說(shuō)白了就是個(gè)查詢(xún)而已(大家不要誤認(rèn)為就這么一句簡(jiǎn)單的SQL為什么要用游標(biāo),因?yàn)橹皇欠奖阕x者學(xué)習(xí)游標(biāo)罷了,具體業(yè)務(wù)具體分析,請(qǐng)不要抬杠哦)
當(dāng)我們要使用list中的數(shù)據(jù)時(shí),我們使用循環(huán)調(diào)用某一條數(shù)據(jù)時(shí),是不是就要用實(shí)體類(lèi)對(duì)象點(diǎn)get字段??梢岳斫鉃閒or e in ee loop dbms_output.put_line('deptno:'||e.deptno); end loop;
這里面的e.deptno。

獲取table中的column

create or replace procedure intop(id in number, print2 out varchar2) as
  e_name varchar2(64);
begin
  select ename into e_name from emp where empno = id;
  if e_name ='ALLEN' then 
   dbms_output.put_line(e_name);
   print2:='my name is '||e_name;
   else if e_name ='SMITH' then 
      print2:='打印sql'||e_name;
      else
        print2:='打印其他';
      end if;
   end if;
end intop;

稍微復(fù)雜一點(diǎn)存儲(chǔ)過(guò)程

由于朋友這里有個(gè)需求需要用存儲(chǔ)過(guò)程,進(jìn)而更新一下博客。
首先我們先創(chuàng)建一張表

-- Create table
create table CLASSES
(
  id       NUMBER not null,
  name     VARCHAR2(14),
  classesc VARCHAR2(10),
  seq      NUMBER(5)
)
tablespace USERS
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );
-- Create/Recreate primary, unique and foreign key constraints 
alter table CLASSES
  add constraint PK_CLASSES primary key (ID)
  using index 
  tablespace USERS
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );

下面我們創(chuàng)建一個(gè)序列

-- Create sequence 
create sequence SEQ_CLASSES
minvalue 1
maxvalue 9999999999999999999999999999
start with 2
increment by 1
cache 20;

下面創(chuàng)建存儲(chǔ)過(guò)程,寫(xiě)的亂一些,希望不要介意

create or replace procedure proclasses(Names     in varchar2,
                                       classescs in varchar) as
/*在我們創(chuàng)建存儲(chǔ)過(guò)程的時(shí)候as其實(shí)是is*/
  id  number;/*設(shè)置變量名稱(chēng)*/
  c   number;
  seq number;
begin
  select SEQ_CLASSES.nextval into id from dual;/*獲取下一個(gè)序列,使用into賦值給id這個(gè)變量名稱(chēng)*/
  dbms_output.put_line('classescs=' || classescs);/*打印而已*/
  select count(*) into c from Classes where classesc = classescs;/*條件判斷,classesc=進(jìn)來(lái)的變量*/
  if (c > 0) then/*當(dāng)數(shù)量大于0時(shí)*/
    select max(seq) + 1 into seq from Classes where classesc = classescs;
    dbms_output.put_line('第一個(gè)seq' || seq);
  else
    if (c = 0) then
      seq := 0;/*如果查詢(xún)出來(lái)的數(shù)量為0的時(shí)候,我們賦值seq變量為0*/
      dbms_output.put_line('c=0的時(shí)候seq' || seq);
    end if;
  end if;
  insert into classes
    (id, name, classesc, seq)
  values
    (id, names, classescs, seq);
 /*insert插入這個(gè)不用多說(shuō)了,大家都明白;注意的是我們insert之后一定要提交。
  不然數(shù)據(jù)沒(méi)有持久化到數(shù)據(jù)庫(kù),這個(gè)insert沒(méi)有任何意義了*/
end proclasses;

下面我們來(lái)調(diào)用這個(gè)存儲(chǔ)過(guò)程

-- Created on 2019/1/7 星期一 by ADMINISTRATOR 
declare 
  -- Local variables here
  names varchar2(32):='曉明';
  classescs varchar2(32):='一班';
begin
  -- Test statements here
  proclasses(names,classescs);
end;

到此這篇關(guān)于Oracle存儲(chǔ)過(guò)程案例詳解的文章就介紹到這了,更多相關(guān)Oracle存儲(chǔ)過(guò)程內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • oracle數(shù)據(jù)庫(kù)索引失效的問(wèn)題及解決

    oracle數(shù)據(jù)庫(kù)索引失效的問(wèn)題及解決

    本文總結(jié)了在Oracle數(shù)據(jù)庫(kù)中索引失效的一些常見(jiàn)場(chǎng)景,包括使用isnull、isnotnull、!=、<、>、函數(shù)處理、like前置%查詢(xún)以及范圍索引和等值索引同時(shí)存在等情況,通過(guò)實(shí)際的SQL查詢(xún)驗(yàn)證,展示了索引失效的原因,并給出了相應(yīng)的優(yōu)化建議
    2025-01-01
  • Oracle 查看表空間的大小及使用情況sql語(yǔ)句

    Oracle 查看表空間的大小及使用情況sql語(yǔ)句

    表空間使用情況包括:查看表空間的名稱(chēng)及大小/查看表空間物理文件的名稱(chēng)及大小/查看回滾段名稱(chēng)及大小等等感興趣的你可以參考下本文
    2013-03-03
  • oracle中to_date詳細(xì)用法示例(oracle日期格式轉(zhuǎn)換)

    oracle中to_date詳細(xì)用法示例(oracle日期格式轉(zhuǎn)換)

    這篇文章主要介紹了oracle中to_date詳細(xì)用法示例,包括期和字符轉(zhuǎn)換函數(shù)用法、字符串和時(shí)間互轉(zhuǎn)、求某天是星期幾、兩個(gè)日期間的天數(shù)、月份差等用法
    2014-01-01
  • Oracle數(shù)據(jù)庫(kù)tnsnames.ora文件的作用和配置

    Oracle數(shù)據(jù)庫(kù)tnsnames.ora文件的作用和配置

    這篇文章主要給大家介紹了關(guān)于Oracle數(shù)據(jù)庫(kù)tnsnames.ora文件的作用和配置,tnsnames.ora 是一個(gè)oracle數(shù)據(jù)庫(kù)網(wǎng)絡(luò)配置文件,通過(guò)這個(gè)配置文件才能建立對(duì)數(shù)據(jù)庫(kù)的連接,需要的朋友可以參考下
    2024-06-06
  • Oracle數(shù)據(jù)庫(kù)重啟服務(wù)、監(jiān)聽(tīng)程序命令詳解

    Oracle數(shù)據(jù)庫(kù)重啟服務(wù)、監(jiān)聽(tīng)程序命令詳解

    本文詳細(xì)介紹了如何重啟Oracle數(shù)據(jù)庫(kù)和監(jiān)聽(tīng)器的步驟,包括登錄服務(wù)器、停止和啟動(dòng)數(shù)據(jù)庫(kù)及監(jiān)聽(tīng)器的過(guò)程
    2024-12-12
  • oracle索引總結(jié)

    oracle索引總結(jié)

    這篇文章要給大家介紹的是oracle索引,索引是數(shù)據(jù)庫(kù)對(duì)象之一,用于加快數(shù)據(jù)的檢索,類(lèi)似于書(shū)籍的索引。在數(shù)據(jù)庫(kù)中索引可以減少數(shù)據(jù)庫(kù)程序查詢(xún)結(jié)果時(shí)需要讀取的數(shù)據(jù)量,類(lèi)似于在書(shū)籍中我們利用索引可以不用翻閱整本書(shū)即可找到想要的信息。 感興趣的小伙伴可參考一下
    2021-09-09
  • Oracle安裝卸載圖文教程詳解

    Oracle安裝卸載圖文教程詳解

    這篇文章主要為大家介紹了Oracle安裝卸載的詳細(xì)圖文教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Oracle開(kāi)發(fā)之報(bào)表函數(shù)

    Oracle開(kāi)發(fā)之報(bào)表函數(shù)

    本文主要介紹Oracle報(bào)表函數(shù)RATIO_TO_REPORT的具體使用方法,需要的朋友可以參考下。
    2016-05-05
  • oracle 虛擬專(zhuān)用數(shù)據(jù)庫(kù)詳細(xì)介紹

    oracle 虛擬專(zhuān)用數(shù)據(jù)庫(kù)詳細(xì)介紹

    這篇文章詳細(xì)介紹了oracle 虛擬專(zhuān)用數(shù)據(jù)庫(kù),對(duì)行級(jí)別和列級(jí)別分別舉了代碼實(shí)例并進(jìn)行分析,內(nèi)容比較詳細(xì),需要的朋友可以參考下。
    2017-09-09
  • Zabbix監(jiān)控Oracle歸檔日志空間的全過(guò)程

    Zabbix監(jiān)控Oracle歸檔日志空間的全過(guò)程

    本文將介紹Zabbix監(jiān)控Oracle歸檔日志空間的全過(guò)程,Zabbix是一個(gè)開(kāi)源的監(jiān)控系統(tǒng),它可以監(jiān)控各種不同類(lèi)型的服務(wù)器和服務(wù),如果您想要監(jiān)控Oracle數(shù)據(jù)庫(kù),文中是一些簡(jiǎn)單的步驟,需要的朋友可以參考下
    2024-04-04

最新評(píng)論

辉南县| 余姚市| 三穗县| 深水埗区| 灌南县| 永清县| 万宁市| 淄博市| 邯郸县| 蒙山县| 呼和浩特市| 浪卡子县| 西平县| 穆棱市| 成安县| 潮安县| 阿城市| 永顺县| 丰台区| 泰宁县| 潞西市| 衡水市| 湖口县| 梓潼县| 图片| 汶上县| 五常市| 望城县| 双流县| 满城县| 泗阳县| 东阿县| 和平区| 马公市| 敦化市| 临颍县| 长沙县| 新密市| 东山县| 永泰县| 新昌县|