2012-11-05 8 views
9
CREATE PROCEDURE Pname(in_Tid IN VARCHAR2,in_IP IN VARCHAR2,outstaticip OUT VARCHAR2,outcount OUT NUMBER) 
AS 
BEGIN 
select STATIC_IP into outstaticip from OP_TTER_MAPPING where TERMINAL_ID = in_Tid; 
if in_IP = outstaticip then 
return 1; 
else 
select COUNT(*) into outcount from OP_TTER_MAPPING where DYNAMIC_IP_LOW <= in_IP AND DYNAMIC_IP_HIGH >= in_IP AND TERMINAL_ID = in_Tid; 
if outcount = 1 then 
return 1; 
else 
return 0; 
    end if; 
end if; 
END; 
  1. È possibile utilizzare il ritorno in stored procedure come sopra?
  2. Se possiamo usare il ritorno, come posso ottenere che il valore di ritorno in Executesql("begin Pname(----)END") metodo

EDITÈ possibile utilizzare "ritorno" nella stored procedure?

Ora ho modificato il mio valore di ritorno in stored procedure come questo, sto facendo bene?

CREATE PROCEDURE P_ValidateTIDIP(in_Tid IN VARCHAR2,in_IP IN VARCHAR2,outstaticip OUT VARCHAR2,outcount OUT NUMBER,outretvalue OUT NUMBER) 
AS 
BEGIN 
select STATIC_IP into outstaticip from OP_TTER_MAPPING where TERMINAL_ID = in_Tid; 
if in_IP = outstaticip then 
    outretvalue:=1; 
else 
    select COUNT(*) into outcount from OP_TTER_MAPPING where DYNAMIC_IP_LOW <= in_IP AND DYNAMIC_IP_HIGH >= in_IP AND TERMINAL_ID = in_Tid; 
    if outcount = 1 then 
    outretvalue:=1; 
    else 
    outretvalue:=0; 
    end if; 
end if; 
END; 

risposta

9

Nel stored procedure, si ritorna i valori utilizzando OUT parametro SOLO. Come avete definito due variabili nel tuo esempio:

outstaticip OUT VARCHAR2, outcount OUT NUMBER 

Basta assegnare i valori di ritorno ai parametri fuori cioè outstaticip e outcount e accedervi di nuovo di chiamare posizione. Quello che voglio dire qui è: quando chiamate la stored procedure, passerete anche queste due variabili. Dopo la chiamata alla procedura memorizzata, le variabili verranno popolate con valori di ritorno.

Se si desidera avere RETURN value come ritorno dalla chiamata PL/SQL, quindi utilizzare FUNCTION. Si prega di notare che nel caso, si sarebbe in grado di restituire solo una variabile come variabile di ritorno.

+0

@ user1799114: non è formattato, così difficile da sottostare. Ma sembra OK come sembri assegnare i valori nelle variabili OUT. Qual è la domanda ora? –

+0

Ciao yogendra singh, ho modificato il codice sopra, sto facendo in modo corretto ???? – user1

+0

@ user1799114: Sono qui. qual'è la domanda? –

6

Uso FUNZIONE:

CREATE OR REPLACE FUNCTION test_function 
RETURN VARCHAR2 IS 

BEGIN 
    RETURN 'This is being returned from a function'; 
END test_function; 
1
CREATE PROCEDURE pr_emp(dept_id IN NUMBER,vv_ename out varchar2 ) 
AS 
v_ename emp%rowtype; 
CURSOR c_emp IS 
    SELECT ename 
    FROM emp where deptno=dept_id; 
BEGIN 
    OPEN c; 
    loop 
     FETCH c_emp INTO v_ename; 
     return v_ename; 
     vv_ename := v_ename 
     exit when c_emp%notfound; 
    end loop; 
    CLOSE c_emp; 


END pr_emp; 
3
-- IN arguments : you get them. You can modify them locally but caller won't see it 
-- IN OUT arguments: initialized by caller, already have a value, you can modify them and the caller will see it 
-- OUT arguments: they're reinitialized by the procedure, the caller will see the final value. 
CREATE PROCEDURE f (p IN NUMBER, x IN OUT NUMBER, y OUT NUMBER) 
IS 
BEGIN 
    x:=x * p; 
    y:=4 * p; 
END; 
/

SET SERVEROUTPUT ON 

declare 
    foo number := 30; 
    bar number := 0; 
begin 
    f(5,foo,bar); 
    dbms_output.put_line(foo || ' ' || bar); 
end; 
/

- Procedura uscita può essere raccolto dalle variabili xey (ans1: = x e ans2: = y) sarà: 150 e 20 rispettivamente.

- Risposta presi in prestito da: https://stackoverflow.com/a/9484228/1661078

1

E 'possibile.

Quando si utilizza il ritorno all'interno di una procedura, il controllo viene trasferito al programma chiamante che chiama la procedura. È come un'uscita in loop.

Non restituirà alcun valore.