我正在while循环中将未按顺序的值传递给存储过程。
我知道如果数字按顺序排列,该怎么做,但是如何传递数组中而不按顺序排列的值?
这很好用,因为我的数字都是从4000到5000(4000、4001、4002 ....)。
DECLARE @FldID INT
SET @FldID = 4000
WHILE @FldID <= 5000
BEGIN
PRINT @FldID;
EXEC [dbo].[usp_delFld] @FldID
SET @FldID = @FldID + 1;
END;
但是如果我的电话号码不是按顺序排列,即:4001、4002、4010、4502、4999、4030、4044,怎么办?
如何将这些值传递给存储过程? 我还能使用while循环吗?
答案 0 :(得分:0)
如果您将值存储在某个表中,则可以这样做:
DECLARE @FldID INT
SET @FldID = 4000
WHILE @FldID <= 5000
BEGIN
if exists(select 1 from SomeTable Where FldID=@FldID)
begin
PRINT @FldID;
EXEC [dbo].[usp_delFld] @FldID
SET @FldID = @FldID + 1
end
END;
您也可以使用游标进行此类操作
答案 1 :(得分:0)
DECLARE @FldID INT DECLARE @COUNT INT,
@SNO INT DECLARE @TBL TABLE (SNO INT IDENTITY(1, 1), FLDID INT)
INSERT INTO
@TBL (FLDID)
SELECT
FLDID
FROM
TABLE
WHERE
FLDID BETWEEN 4000
AND 5000
SELECT
@COUNT = COUNT(*)
FROM
@TBL
SET @SNO=1
WHILE (@COUNT > 0) BEGIN
SELECT
TOP (1) @FldID = FLDID,
@SNO = SNO
FROM
@TBL
WHERE
SNO = @SNO PRINT @FldID;EXEC [dbo].[usp_delFld] @FldID
SET
@COUNT = @COUNT -1
SET
@SNO = @SNO + 1 END;