Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

Please can anyone suggest me,how can we call a another procedure in a procedure.

I would be glad if i get a similar example.

Thanks.
Posted

create procedure sp_proc2
as
begin
 declare @retcode int
--  here you can call another stored procedure
 exec @retcode = sp_proc1

end


in this way you can call
 
Share this answer
 
You can call like this.
SQL
EXEC sp_name @param, @output OUTPUT

Check the below example.

http://www.aspfree.com/c/a/ASP.NET-Code/Call-Stored-procedure-from-within-another-stored-procedure-return-values/[^]

For more info, go here[^].
 
Share this answer
 
The scenario is whan user passes an employee_no to be inserted into EMP table i wan that value to be check against the existing record. If the employee_no does not exists the record will be inserted into the table , but if the record alredy exists the employee_no will be incremented by 1 each time and checked against existing record till no more such employee_no exists and finally the record gets inserted into the database table.

So hear we go.

[CODE]
create or replace procedure find(eno number, flag out boolean) is
var1 number(2);
SQL
begin
  select count(*) into var1 from emp where empno = eno;
  if var1 = 0 then
    flag := FALSE;
  else
    flag := TRUE;
  end if;
end;

[/CODE]

The above procedure will check for the existance of the record in the database table. So what i did here is pass the Primay key column as an argument to the procedure. This procedure also has an OUT parameter, that tells what the query output was. Simply i take a count of the record based on the PK column, if the record does not exists (count =0) return FALSE ,TRUE otherwise.

Now this procedure is called inside another procedure, the next one (INSERT_EMP).
I pass a single number parameter to the procedure, which in turn is passed into the first procedure (FIND). So the logic is tested inside FIND and the output of that procedure is returned back to this prodecure. Based on that value the actual transaction is completed.

[CODE]
create or replace procedure INSERT_EMP(emp_num number) is
SQL
 eno emp.empno%type;
  fg  boolean;
begin
  eno := emp_num;
  <>
  find(eno, fg);
  if fg = true then
    dbms_output.put_line(eno || '  Already exists...');
    eno := eno + 1;
    goto abc;
  else
    insert into emp (empno) values (eno);
    COMMIT;
    dbms_output.put_line('New empno inserted is : ' || eno);
  end if;
end;
[/CODE]
 
Share this answer
 
v2
Comments
Dalek Dave 16-Sep-11 3:20am    
Edited for Code Blocks.
Google found this: How to Share Data Between Stored Procedures[^] which covers that as well as making sense of the data when you do...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900