如何检索SQL Server存储过程中的整数列表?

时间:2015-02-16 08:00:22

标签: sql-server stored-procedures

我将一个ID列表发送到存储过程,并希望INSERT/UPDATE将它们放入一个表中并接收一个不是INSERTED/UPDATED的ID列表,这样我就可以向它显示一个详细的错误用户。

如何在此存储过程中完成它?

CREATE procedure [dbo].[NET_SET_Int_PriorityAccountsForXMLFeeds_ExpirePriorityByIds]    
   (@IdsList NumericList READONLY)    
AS 
    UPDATE [dbo].[Int_PriorityAccountsForXMLFeeds]    
    SET PromotionExpirationDate = DATEADD(MINUTE,-1,GETDATE())
    WHERE IDS IN (SELECT * FROM @IdsList)

    IF (@@ERROR != 0)
       RETURN -1;

2 个答案:

答案 0 :(得分:0)

你的意思是表中的ID不存在于表中吗?假设您的字段在参数中也称为IDS,则执行以下操作:

select IDS from @IdsList L 
where not exists (select 1 
                  from [dbo].[Int_PriorityAccountsForXMLFeeds] P 
                  where P.IDS = L.IDS)

答案 1 :(得分:0)

创建将返回缺少ID的表的函数 - 类似的东西 -

ALTER FUNCTION [NET_SET_Int_PriorityAccountsForXMLFeeds_ExpirePriorityByIds]    
   (@IdsList NumericList READONLY)    
RETURNS  @rtnTable TABLE 
(
    -- columns returned by the function
   IDS int) 
AS
BEGIN

    UPDATE [dbo].[Int_PriorityAccountsForXMLFeeds]    
    SET PromotionExpirationDate = DATEADD(MINUTE,-1,GETDATE())
    WHERE IDS IN (SELECT * FROM @IdsList)
    GO 
    insert into @rtnTable
    select IDS from @IdsList x
    where not exists (select * 
                  [Int_PriorityAccountsForXMLFeeds]  P 
                  where P.IDS = x.IDS)
END