SQL select结果集和return的区别

时间:2024-01-13 16:47:26

IF EXISTS (SELECT 1 FRIN sys.objects where object_id=OBJECT_ID(N'[dbo].[testReturn_up]')AND type in (N'P'),N'PC')

  DROP PROC [dbo.]testReturn_up

GO

Create PROC [dbo.]testReturn_up

AS

  BEGIN

    SELECT 0;

    RETURN 2;

  END

TEST:

  DECLARE @a int

  exec @a = testReturn_up

  SELECT @

Result is:

0

2

BUT:

   exec testReturn_up

Result is just 0

So the answer is

exec取得的是结果集,而将执行过的Proc赋值给变量时,就是return的结果。所以步骤一的时候,result有2行记录,第一个0至不过是执行proc的响应。但是真正的@a = 2。