如何在 SQL 服务器存储过程中检索整数列表?
How to retrieve list of integers in SQL Server stored procedure?
我将 ID 列表发送到存储过程,并希望将它们 INSERT/UPDATE
放入 table 并收到不是 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;
您的意思是该列表中的 ID 在 table 中不存在?假设你的字段在参数中也被称为 IDS,这样做:
select IDS from @IdsList L
where not exists (select 1
from [dbo].[Int_PriorityAccountsForXMLFeeds] P
where P.IDS = L.IDS)
创建将 return table 缺少 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
我将 ID 列表发送到存储过程,并希望将它们 INSERT/UPDATE
放入 table 并收到不是 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;
您的意思是该列表中的 ID 在 table 中不存在?假设你的字段在参数中也被称为 IDS,这样做:
select IDS from @IdsList L
where not exists (select 1
from [dbo].[Int_PriorityAccountsForXMLFeeds] P
where P.IDS = L.IDS)
创建将 return table 缺少 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