2011-12-19 86 views
3

如何從sqlplus調用存儲過程?從sqlplus調用存儲過程

我有一個過程:

Create or replace procedure testproc(parameter1 in varachar2,parameter2 out varchar2) 
begin 

Do something 

end; 

我試圖EXEC testproc(12,89) ::返回錯誤

+2

一般情況下,這是你更好說明你正在得到什麼錯誤,而不是僅僅說它「返回錯誤」 – 2011-12-19 13:10:23

回答

10

您的程序的第二個參數是OUT參數 - 其值將被分配給過程完成時傳遞的變量。所以你不能使用這個參數的文字值。

您可以在SQLPlus提示符下聲明一個綁定變量和使用:

-- Declare bind variable 
VARIABLE x NUMBER 

-- If necessary, initialize the value of x; in your example this should be unnecessary 
-- since the value of the second parameter is never read 
EXEC :x := 1 

-- Call the procedure 
EXEC testproc(12, :x) 

-- Print the value assigned to the bind variable 
PRINT x 

或者,您也可以使用匿名PL/SQL塊:

-- Activate client processing of dbms_output buffer 
SET SERVEROUTPUT ON 

-- In anonymous block, declare variable, call procedure, print resulting value 
DECLARE 
    x NUMBER; 
BEGIN 
    testproc(12, x); 
    dbms_output.put_line(x); 
END; 
/
3
create or replace procedure autogenerate(t1 in int,t2 in int) 
    is 
    jum number; 
    begin 
      if t1 < 10 then 
      dbms_output.put_line('Value too low.'); 
       else if t1 > 20 then 
       dbms_output.put_line('Value too high.'); 
       end if; 
      end if; 
    end; 
/
    show errors; 
    set serveroutput on; 
    execute autogenerate(1,2); 

試試這個,如果你有問題,只是再次發佈它給我:)

+0

創建或替換過程TESTSQLRULEPROC(param1 in varchar2,param2 out varchar2)AS BEGIN \t IF TO_Number(param1)<10 THEN \t \t param2:='Value too low。'; \t ELSIF TO_Number(參數1)> 20 THEN \t \t param2:='Value too high。'; \t \t ELSE \t \t param2:='NULL'; \t END IF; \t例外,當其他人 \t \t param2:='不是數字! END TESTSQLRULEPROC; – Rupesh 2011-12-19 08:29:46

+0

編輯,檢查它 – 2011-12-19 08:43:02