简体   繁体   中英

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 @proc1result = 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.

"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

(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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM