SQL SERVER 2008 , Nested procedures issue
Considering the following example
Procedure1
..........
IF(@errorcode<>0) ROLLBACK TRANSACTION
ELSE COMMIT TRANSACTION
SELECT @errorcode
Procedure2
..........
WHILE [condition] BEGIN
EXEC @proc1resu开发者_StackOverflow社区lt = Procedure1 [parameters]
IF(@proc1result=0) SET @totalresult=@totalresult+1
END
SELECT @totalresult
The problem is that @totalresult is incremented correctly but the value returned by Procedure2 is 0. How to get it right?
I am using sql server 2008 and Entity Framework 4. Procedure1 works well.
(1) For the first stored procedure, you should use RETURN @errorcode
and not SELECT @errorcode
. This is also my recommendation.
OR (NOT AND)
(2) For the second stored procedure, you should use INSERT ... EXEC Procedure1
like this:
WHILE [condition]
BEGIN
DECLARE @Results TABLE (RetValue INT PRIMARY KEY);
INSERT @Results
EXEC Procedure1 [parameters];
--IF 0=ALL(SELECT a.RetValue FROM @Results a)
IF NOT EXISTS(SELECT * FROM @Results a WHERE a.RetValue <> 0)
SET @totalresult=@totalresult+1;
END
SELECT @totalresult
"but the value returned by Procedure2 is 0"
You do a SELECT @totalresult
. Should it be return @totalresult
?
Answer to Upendra...
CREATE PROC dbo.TestReturn (@InValue int)
AS
Return @Invalue+1
GO
declare @value int
exec @value = TestReturn 100
select @value
精彩评论