SQL Server存储过程参数输出

时间:2012-01-06 10:44:14

标签: sql-server stored-procedures

我有一个存储过程,为了这个问题的目的,我已经大大缩小了,但实质上我需要帮助的问题是这个。

如果表xyz中的行被更新,我需要将ID附加到彼此并输出回调用应用程序。更新按预期工作,问题在于我构建输出@IPV_ID_Found的方式(请参阅代码底部的注释部分)。

@IPV_Status varchar (50),
@IPV_ID_Found varchar(500) = 'A' OUTPUT 

IF (@IPV_Status ='closed')
BEGIN
UPDATE TEST_TBL
 SET
    Status  = 'xyz',        
 WHERE                  
    ID      =  @IPV_ID 

  -- this works for one ID
  SELECT @IPV_ID_Found = (CAST(@IPV_ID AS VARCHAR(500)))    

  -- this does not work for multiple IDs
  SELECT @IPV_ID_Found = @IPV_ID_Found + (CAST(@IPV_ID AS VARCHAR(500))) + ','

  -- neither does this
  SET @IPV_ID_Found = @IPV_ID_Found + (CAST(@IPV_ID AS VARCHAR(500))) + ','

  SELECT @IPV_ID_Found                  
END

1 个答案:

答案 0 :(得分:1)

查看更改:

 -- this does not work for multiple IDs
  SELECT @IPV_ID_Found = @IPV_ID_Found + ',' + (CAST(@IPV_ID AS VARCHAR(500))) 

然后您将获得一个连续的值列表,例如1,2,3,4,5

如果需要返回记录集,则不需要OUTPUT参数,而是使用表变量:

declare @IPV_ID_Found table(Item varchar(500))

IF (@IPV_Status ='closed')
BEGIN
UPDATE TEST_TBL
 SET
    Status  = 'xyz',        
 WHERE                  
    ID      =  @IPV_ID 

  -- this works for one ID
  insert @IPV_ID_Found 
  VALUES (CAST(@IPV_ID AS VARCHAR(500)))    

  -- this does not work for multiple IDs
  INSERT @IPV_ID_Found 
  VALUES (CAST(@IPV_ID AS VARCHAR(500)))

  SELECT Item FROM @IPV_ID_Found                  
END