關於oracle的一點備忘

來源:互聯網
上載者:User

標籤:oracle 預存程序

資料庫無法迴避的就是處理資料


情景:發票資訊,很多公司員工在進行交易的時候,填寫的發票資訊都是一樣的,所以公司員工往往想調用統一的發票資訊,公司會計登入系統可以填寫通用發票資訊,為了產生訂單簡單,發票需要和員工號碼綁定,所以發票的表裡想要複製一份員工號為會計的發票資訊,改動其中的員工號,發票id,建立時間,其餘不變



不知道大家弄懂應用情境沒,敘述能力有限。


解決:可以使用select into,但是感覺不方便和靈活。所以考略利用右邊和預存程序,因為沒有傳回值,就不寫函數了


上代碼:

create or replace procedure myprocd(    uid in varchar2)AS    receipt_autoid number;BEGIN    select SEQ_CHEM_DICT_RECEIPT.NEXTVAL into receipt_autoid from dual;    for rs in (select * from CHEM_DICT_RECEIPT where user_id=‘0‘)    loop        rs.user_id:=uid;        rs.id:=receipt_autoid;        rs.receipt_id:=‘RE0215‘||receipt_autoid;        rs.created_time:=sysdate;        insert into chem_dict_receipt values rs;    end loop;END myprocd;execute myprocd(‘002‘);

提醒:我實在是沒搞清楚序列如何在迴圈中增加,所以我更改了策略,我讓遊標只能讀取一次,也就是不再迴圈,所以更改如下

CREATE OR REPLACE procedure SIT_HXPGL.myproce(    uid_re in varchar2,    type_re in varchar2)AS    receipt_autoid number(12);BEGIN    select SEQ_CHEM_DICT_RECEIPT.NEXTVAL into receipt_autoid from dual;    for rs in (select * from CHEM_DICT_RECEIPT where user_id=‘0‘ and receipt_type=type_re)    loop        rs.user_id:=uid_re;        rs.id:=receipt_autoid;        rs.receipt_id:=‘RE0215‘||receipt_autoid;        rs.created_time:=sysdate;        insert into CHEM_DICT_RECEIPT values rs;            end loop;END myproce;/

無奈:代碼裡沒有辦法標紅。我新加了一個變數

type_re

這樣我的遊標只能每次讀取一組資料。

最後加一句,關於執行預存程序,好像是call和execute都可以,推薦使用call


小思路:好像可以吧擷取自增序列放在自訂函數,然後調用,可能不會有問題

關於函數和預存程序或者遊標,這裡有個連結很好,可以參考下

http://wen866595.iteye.com/blog/1733887       程式點滴

其中有個小問題,我實在實踐中發現的,但是還沒看oracle的官方文檔。

問題:預存程序的結束END;

解決:END myproc;

解釋:應該是要加上預存程序的名字

本文出自 “一站式解決方案” 部落格,請務必保留此出處http://10725691.blog.51cto.com/10715691/1945344

關於oracle的一點備忘

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.