Oracle 過程中變數賦值

2021-09-08 10:14:33 字數 2852 閱讀 9709

create or replace function get_sal1(id employees.employee_id%type)

return number is

sal employees.salary%type;

begin

sal := 0;

select salary into sal from employees where employee_id = id;

return sal;

end;

create or replace function get_sal1(id employees.employee_id%type)

return number is

sal employees.salary%type:= 0;

--sal := 0;

begin

select salary into sal from employees where employee_id = id;

return sal;

end;

下面會報錯:

compilation errors for function hr.get_sal1

error: pls-00103: 出現符號 "select"在需要下列之一時:         * & = - + ; < / > at in           is mod remainder not rem <> or != or ~= >=           <= <> and or like like2 like4 likec between || multiset           member submultiset        符號 ";" 被替換為 "select" 後繼續。 line: 8 text: select salary into sal from employees where employee_id = id;

create or replace function get_sal1(id employees.employee_id%type)

return number is

sal employees.salary%type;

begin

sal := 0

select salary into sal from employees where employee_id = id;

return sal;

end;

這個也會報錯:

compilation errors for function hr.get_sal1

error: pls-00103: 出現符號 "="在需要下列之一時:         constant exception           table           long double ref char time timestamp interval date binary           national character nchar        符號 "" 被替換為 "=" 後繼續。 line: 5 text: sal := 0;

create or replace function get_sal1(id employees.employee_id%type)

return number is

sal employees.salary%type;

sal := 0;

begin

select salary into sal from employees where employee_id = id;

return sal;

end;

**pl/sql

支援sql

中的資料型別,

pl/sql

中正常支援

number,varchar2,date

等oracle sql

資料型別。宣告變數必須指明變數的資料型別,也可以宣告變數時對變數初始化,變數宣告必須在宣告部分。宣告變數的語法是:

變數名資料型別

[ :=

初始值]

語法解析:

資料型別如果需要長度,可以用括號指明長度,比如:

varchar2(20)。

宣告變數

sql> declare ①

② ③pl/sql procedure successfully completed

**解析:

①宣告乙個變數

sname

,初始化值是「

jerry

」。字串用單引號,如果字串**現單引號可以使用兩個單引號(

』』)來表示,即單引號同時也具有轉義的作用。

②對變數

sname

重新賦值,賦值運算子是「

:=」。

③dbms_output.put_line

是輸出語句,可以把乙個變數的值輸出,在

sql*plus

中輸出資料時,可能沒有結果顯示,可以使用命令:

set serveroutput on

設定輸出到

sql*plus

控制台上。

對變數賦值還可以使用

select…into

語句從資料庫中查詢資料對變數進行賦值。但是查詢的結果只能是一行記錄,不能是零行或者多行記錄

字元及數字運算特點

空值加數字仍是空值:null + < 數字》 = null

空值加(連線)字元,結果為字元:null || 《字串》 = < 字串》

儲存過程中SELECT與SET對變數賦值

createproc insert book param1char 10 param2varchar 20 param3money,param4moneyoutput withencryption 加密 as insertintobook 編號,書名,values param1,param2,par...

oracle 變數賦值

在oracle 中使用 select 字段 into 變數 from 表 where 條件 這種方法給變數賦值時和mssql的行為有比較大區別.在mssql中如果在該表中未檢索到任何行,則變數的值保持不變,如果檢索到多條資料,則會將每一條資料的值依次賦給變數 順序不保證永遠相同 但是在oracle中...

儲存過程中的變數定義

as we all know,mysql的儲存過程就類似於指令碼,既然似指令碼就會有使用到變數的時候。mysql儲存過程常見的變數 區域性變數 使用者變數 系統變數 一 區域性變數 在過程體中,可以宣告區域性變數,用來臨時儲存一些值。1 定義區域性變數語法 declare var name var ...