KingbaseES 匿名塊如何傳遞引數

2022-09-09 11:30:13 字數 1448 閱讀 6716

匿名塊的基本語法結構包括宣告和執行兩部分。匿名塊每次提交都被重新編譯和執行。因為匿名塊沒有名稱並不在資料庫中儲存,所以匿名塊不能直接從其他pl/sql 塊中呼叫。

定義語法:

[ declare ]

[ variable_declaration ]

[ cursor_declaration ]

begin

sequence_of_statement

end

說明:a. 宣告部分 :類似於儲存過程中的宣告,預先定義塊中要用到的變數。包含pl/sql 變數,異常,游標等。如果沒有變數需要定義,宣告部分可以省略。

b. 執行部分: 以begin 開始,以end 結束,不可省略。類似於儲存過程中的執行塊。

在使用過程中oracle資料庫的匿名塊可以通過 &var形式來動態接收引數輸入, kingbasees也能實現動態輸入引數,但是相對麻煩一些,引數需要通過ksql命令來傳入。

sql> set serveroutput on;

sql> declare

num1 number;

num2 number;

calresult number;

begin

num1:=&n1;

num2:=&n2;

calresult:=num1+num2;

dbms_output.put_line(num1||'+'||num2||'='||calresult);

end;

/enter value for n1: 3

old 6: num1:=&n1;

new 6: num1:=3;

enter value for n2: 4

old 7: num2:=&n2;

new 7: num2:=4;

3+4=7

pl/sql procedure successfully completed.

需要將匿名塊儲存為指令碼,通過ksql命令來呼叫。

[kingbase@singlekbdb ~]$ vi 1.sql

\set sqlterm /

declare

v_date date;

begin

select nd into v_date from t2 where id = :id;

raise notice 'time is %',v_date;

end;

/[kingbase@singlekbdb ~]$ ksql -usystem -p54321 test -v id=2 -c '\i 1.sql;'

ksql:1.sql:9: 注意: time is 2020-09-18 00:00:00

anonymous block

KingbaseES函式如何返回結果集

函式返回值一般是某一型別值,如int,varchar,date等,返回結果集時就需要用到setof語法。test set sqlterm test create or replace function f get class test returns setof class test as test...

ORACLE 匿名塊 變數 if

pl sql匿名塊 匿名塊 宣告 執行體 異常處理 declare v a integer v b integer begin v a 10 v b 請輸入第二個數 dbms output.put line 商 v a v b dbms output.put line 積 v a v b dbms ...

建構函式,靜態塊,匿名構造塊

1,靜態塊 隨著jvm載入類的時候載入而執行 不管new多少次,只執行一次 如果乙個類中有多個static修飾的靜態 塊,按照先後順序執行。例 public class b public b static public static b t3 new b public static void mai...