Click here to Skip to main content
15,895,779 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
i need to create nested SP, i used previous SP to execute inside my Own SP. But it throws error...

Please give me some Productive input to fix this mess....

create procedure ALLinone
(
@Sname varchar(50),
@Srollno varchar(50),
@Pname varchar(50),
@Saddr varchar(50)
)
As
Begin
Exec SP_Testone @Sname, @Srollno
Go
Exec SP_Testtwo @Sname, @Pname
Go
Exec SP_Testthree @Srollno, @Saddr
End

It throws error like...

Msg 102, Level 15, State 1, Procedure ALLinone, Line 11<br />
Incorrect syntax near '@Srollno'.<br />
Msg 137, Level 15, State 2, Line 2<br />
Must declare the scalar variable "@Sname".<br />
Msg 137, Level 15, State 2, Line 2<br />
Must declare the scalar variable "@Srollno".



Even i tried run each and every SP seperately its works Perfect....But problem is while create this nested SP
Posted

1 solution

GO isn't a TSQL command. It marks the end of a batch in Query Analyzer and
therefore signals the end of a stored procedure definition in that batch, so
it should not be part of an SP.

NOTE : I assumed that three calls are independent of each other and that is why error handling is seperate...

CREATE PROCEDURE ALLinone
(
	@Sname varchar(50),
	@Srollno varchar(50),
	@Pname varchar(50),
	@Saddr varchar(50)
)
AS
BEGIN
	CREATE TABLE #Errors (ErrAt NVARCHAR(20), ErrNumber INT, ErrorMessage NVARCHAR(MAX))
	
	BEGIN TRY
		EXEC SP_Testone @Sname, @Srollno
	END TRY
	BEGIN CATCH
		INSERT #Errors
		SELECT 'SP_Testone', ERROR_NUMBER(), ERROR_MESSAGE()
	END CATCH;
	
	BEGIN TRY
		EXEC SP_Testtwo @Sname, @Pname
	END TRY
	BEGIN CATCH
		INSERT #Errors
		SELECT 'SP_Testtwo', ERROR_NUMBER(), ERROR_MESSAGE()
	END CATCH;
	
	BEGIN TRY
		EXEC SP_Testthree @Sname, @Pname
	END TRY
	BEGIN CATCH
		INSERT #Errors
		SELECT 'SP_Testthree', ERROR_NUMBER(), ERROR_MESSAGE()
	END CATCH;
	
	SELECT * FROM #Errors
END
 
Share this answer
 
v3
Comments
J.Karthick 22-Jul-11 10:24am    
Wooow....Thanks :)
You fix this.....
J.Karthick 22-Jul-11 10:25am    
One more question...Is it possible to use try...catch for my nested SP?
Manas Bhardwaj 22-Jul-11 10:32am    
modified my answer
J.Karthick 22-Jul-11 10:35am    
Is this the method to Handle the error??

But it will create an table for each and every execution??
Manas Bhardwaj 22-Jul-11 10:37am    
Table is created once. Error results are inserted into table (if any). This may or may not be the best way to handle error in your SP.

As, I said in answer, I have assumed 3 SPs to be independdent of each other and thats why three different try catch blocks.

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