T-SQL: Looping through an array of known values

后端 未结 7 1586
深忆病人
深忆病人 2021-01-30 00:51

Here\'s my scenario:

Let\'s say I have a stored procedure in which I need to call another stored procedure on a set of specific ids; is there a way to do this?

i

7条回答
  •  面向向阳花
    2021-01-30 01:09

    CREATE TABLE #ListOfIDs (IDValue INT)
    
    DECLARE @IDs VARCHAR(50), @ID VARCHAR(5)
    SET @IDs = @OriginalListOfIDs + ','
    
    WHILE LEN(@IDs) > 1
    BEGIN
    SET @ID = SUBSTRING(@IDs, 0, CHARINDEX(',', @IDs));
    INSERT INTO #ListOfIDs (IDValue) VALUES(@ID);
    SET @IDs = REPLACE(',' + @IDs, ',' + @ID + ',', '')
    END
    
    SELECT * 
    FROM #ListOfIDs
    

提交回复
热议问题