Click here to Skip to main content
15,887,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to call stored procedure from stored procedure in sql 2005.
My code is
SQL
create PROCEDURE dbo.SPonDeptTable
	(
	@Option int,
	@CompName varchar(50)
	)	
AS

	begin
	if @Option=0
	 begin
select DeptName from MstDepartment where CompanyID=(exec SPonCompanyTabl(1,@CompName)); 

 end
	RETURN
	end

and SPonCompanyTabl is

SQL
create PROCEDURE dbo.SPonCompanyTabl
    (
    @Option int,
    @name varchar
    )
AS

    begin
    --declare @CompID varchar(50)
    if @Option=0
     begin
select CompanyName from MstCompany

 end
 else if @Option=1
 begin
 select CompanyID from MstCompany where CompanyName=@name
 end
    RETURN
    end


SPonCompanyTabl is working fine but when I create SPonDeptTable error is

incorrect syntex near the keyword 'exec'
incorrect syntex near the keyword '1'
Posted
Comments
Herman<T>.Instance 9-May-12 10:05am    
exec_sp in stead of exec

1 solution

The procedure doesn't return a value so you could use a function instead. For more information see CREATE FUNCTION[^].

Of course one possibility would be to combine the procedures for example something like:
SQL
create PROCEDURE dbo.SPonDeptTable
	(
	@Option int,
	@CompName varchar(50)
	)	
AS
BEGIN
   IF @Option=0 BEGIN
      SELECT DeptName 
      FROM   MstDepartment 
      WHERE  CompanyID = (SELECT CompanyID 
                          FROM   MstCompany 
                          WHERE  CompanyName=@CompName); 
 
   END
   RETURN
END
 
Share this answer
 
Comments
Member 7909353 10-May-12 0:19am    
It is also not working.
Wendelius 10-May-12 0:30am    
Do you get an error or what isn't working?

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