使用in out引數模式的過程
in out模式就是兼備了in與out兩種的模式,則既可以作為輸入值,被過程呼叫,
也可以作為輸出,從過程中呼叫出來作為結果,透過形參傳遞給實參。這裡的過程
例子主要實現兩個事情:一是透過三個引數的輸入值作為記錄插入表dept中,另一個
透過引數傳遞確定部門號的下一個部門號,查詢表中的記錄。當然當中也扦插一些程式
編寫錯誤的解決方法。
---建立一個過程:使用in out引數:
---將使用Scott使用者下的dept表:
--檢視錶dept的表結構:
scott@PROD>desc dept
Name Null? Type
----------------------------------------- -------- ----------------------------
DEPTNO NOT NULL NUMBER(2)
DNAME VARCHAR2(14)
LOC VARCHAR2(13)
--檢視錶dept的記錄:
scott@PROD>select * from dept;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
--建立一個過程:
scott@PROD>create or replace procedure app_dept(
2 v_no in out dept.deptno%type,
3 v_name in out dept.dname%type,
4 v_addr in out dept.loc%type)
5 as
6 begin
7 insert into dept
8 values(v_no,v_name,v_addr);
9 v_no = v_no - 1;
10 select deptno,dname,loc into v_no,v_name,v_addr
11 from dept where deptno = v_no;
12 exception
13 when dup_val_on_index then
14 dbms_output.put_line('There is a deptno,can't appear again!');
15 when no_data_found then
16 dbms_output.put_line('There is no this deptno in this table!');
17 end app_dept;
18 /
Warning: Procedure created with compilation errors.
scott@PROD>
#當編寫程式過程中遇到錯誤時,我們可以呼叫資料庫中的程式檢視出錯的地方,進而修改過來,重新執行過程。
--檢視錯誤
scott@PROD>show error
Errors for PROCEDURE APP_DEPT:
LINE/COL ERROR
-------- -----------------------------------------------------------------
9/6 PLS-00103: Encountered the symbol "=" when expecting one of the
following:
:= . ( @ % ;
The symbol ":= was inserted before "=" to continue.
14/45 PLS-00103: Encountered the symbol "T" when expecting one of the
following:
) , * & = - + < / > at in is mod remainder not rem =>
<an exponent (**)> <> or != or ~= >= <= <> and or like like2
like4 likec as between from using || multiset member
submultiset
LINE/COL ERROR
-------- -----------------------------------------------------------------
--edit重新修改程式的句子:
#修改(略)
scott@PROD>edit
Wrote file afiedt.buf
482
1 create or replace procedure app_dept(
2 v_no in out dept.deptno%type,
3 v_name in out dept.dname%type,
4 v_addr in out dept.loc%type)
5 as
6 begin
7 insert into dept
8 values(v_no,v_name,v_addr);
9 v_no = v_no - 1;
10 select deptno,dname,loc into v_no,v_name,v_addr
11 from dept where deptno = v_no;
12 exception
13 when dup_val_on_index then
14 dbms_output.put_line('There is a deptno,can't appear again!');
15 when no_data_found then
16 dbms_output.put_line('There is no this deptno in this table!');
17* end app_dept;
scott@PROD>
--修改後重新執行:
scott@PROD>create or replace procedure app_dept(
2 v_no in out dept.deptno%type,
3 v_name in out dept.dname%type,
4 v_addr in out dept.loc%type)
5 as
6 begin
7 insert into dept
8 values(v_no,v_name,v_addr);
9 v_no := v_no - 1;
10 select deptno,dname,loc into v_no,v_name,v_addr
11 from dept where deptno = v_no;
12 exception
13 when dup_val_on_index then
14 dbms_output.put_line('There is a deptno,can not appear again!');
15 when no_data_found then
16 dbms_output.put_line('There is no this deptno in this table!');
17 end app_dept;
18 /
Procedure created.
#過程成功執行。
--呼叫程式:
scott@PROD>variable v_no number
scott@PROD>variable v_name varchar2(14)
scott@PROD>variable v_addr varchar2(13)
scott@PROD>execute :v_no :=21
PL/SQL procedure successfully completed.
scott@PROD>execute :v_name :='manager'
PL/SQL procedure successfully completed.
scott@PROD>execute :v_addr :='china'
PL/SQL procedure successfully completed.
scott@PROD>call app_dept(:v_no,:v_name,:v_addr);
Call completed.
scott@PROD>
#過程執行完成。
--檢視插入的資料:
scott@PROD>print :v_no :v_name :v_addr
V_NO
----------
20
V_NAME
--------------------------------------------------------------------------------
RESEARCH
V_ADDR
--------------------------------------------------------------------------------
DALLAS
scott@PROD>
#已經顯示出來:
--使用SQL語句檢視修改後表dept的記錄:
scott@PROD>execute :v_no :=32
PL/SQL procedure successfully completed.
scott@PROD>execute :v_name :='clerk'
PL/SQL procedure successfully completed.
scott@PROD>execute :v_addr :='shanghai'
PL/SQL procedure successfully completed.
scott@PROD>call app_dept(:v_no,:v_name,:v_addr);
There is no this deptno in this table!
Call completed.
scott@PROD>
--列印插入的資料:
scott@PROD>call app_dept(:v_no,:v_name,:v_addr);
There is no this deptno in this table!
Call completed.
scott@PROD>print :v_no :v_name :v_addr
V_NO
----------
31
V_NAME
--------------------------------------------------------------------------------
clerk
V_ADDR
--------------------------------------------------------------------------------
shanghai
scott@PROD>
--使用SQL語言檢視執行過程後的表dept的記錄:
scott@PROD>select * from dept;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
21 manager china
32 clerk shanghai
6 rows selected.
#這裡可以看到,以上插入過程是成功的,就只有檢視的時候,返回查不到記錄的異常。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31392094/viewspace-2128472/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 使用out引數模式的過程模式
- MySQL 儲存過程引數IN OUT INOUT對比MySql儲存過程
- MySQL儲存過程in、out、inout引數示例與總結MySql儲存過程
- 詳解MySQL儲存過程引數有三種型別(in、out、inout)MySql儲存過程型別
- tcbs_批量儲存過程_輸出引數out與異常的關係儲存過程
- 在sqlplus中呼叫in out輸入輸出引數的儲存過程SQL儲存過程
- oracle儲存過程plsql_sp_多個out引數測試語法Oracle儲存過程SQL
- 使用帶有輸出引數的儲存過程儲存過程
- mysql儲存過程的引數MySql儲存過程
- 儲存過程的引數可以使用sql的函式儲存過程SQL函式
- 中介軟體的引數解析過程
- out,ref,params引數傳遞
- 帶輸出引數的儲存過程儲存過程
- 修改pfile引數檔案過程的異常
- 動態呼叫帶引數的儲存過程儲存過程
- Lua中呼叫ref和out修飾引數的函式/過載函式函式
- .NET用使用儲存過程獲取輸出引數的程式碼示例!儲存過程
- c# 方法引數(傳值,傳引用,ref,out,params,可選引數,命名引數)C#
- SQL Server-儲存過程(Procedure),帶入引數和出引數SQLServer儲存過程
- 使用DBMS_RANDOM過程引發的問題random
- ng-template 使用過程中引數傳遞錯誤的單步除錯除錯
- c#之帶有out輸出引數的方法定義及使用示例C#
- 過程需要型別為 'ntext/nchar/nvarchar' 的引數 '@statement'型別
- Oracle帶輸入輸出引數的儲存過程Oracle儲存過程
- SQLSERVER儲存過程如何寫帶引數的遊標SQLServer儲存過程
- dos下呼叫帶輸出引數的儲存過程儲存過程
- Java呼叫儲存過程(帶輸出引數)Java儲存過程
- asp.net中URL引數加密解密過程ASP.NET加密解密
- oracle儲存過程將引數字串分割sqlOracle儲存過程字串SQL
- oracle父儲存過程呼叫子儲存過程procedure與輸出引數Oracle儲存過程
- go 如何呼叫 sqlserver 帶傳出引數的儲存過程GoSQLServer儲存過程
- MySQL Innodb_fast_shutdown引數的內部過程介紹MySqlAST
- C#中ref、out及特殊說明引數的用法C#
- MySQL過程報 Parameter number N is not an OUT parameter錯誤MySql
- SQL Server系統儲存過程和引數示例SQLServer儲存過程
- jdbctemplate呼叫儲存過程傳遞陣列引數JDBC儲存過程陣列
- Windows啟動過程(MBR引導過程分析)Windows
- LINUX的引導過程 (轉)Linux