简体   繁体   English

SQL SERVER 2008,嵌套过程问题

[英]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? 问题是@totalresult正确增加,但是Procedure2返回的值为0。如何正确设置?

I am using sql server 2008 and Entity Framework 4. Procedure1 works well. 我正在使用sql server 2008和Entity Framework4。Procedure1正常运行。

"but the value returned by Procedure2 is 0" “但是Procedure2返回的值为0”

You do a SELECT @totalresult . 您执行SELECT @totalresult Should it be return @totalresult ? 应该return @totalresult吗?

Answer to Upendra... 回答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 . (1)对于第一个存储过程,应使用RETURN @errorcode而不是SELECT @errorcode This is also my recommendation. 这也是我的建议。

OR (NOT AND) 或(非与)

(2) For the second stored procedure, you should use INSERT ... EXEC Procedure1 like this: (2)对于第二个存储过程,应使用INSERT ... EXEC Procedure1如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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