Loop through all the rows of a temp table and call a stored procedure for each row

后端 未结 5 822
独厮守ぢ
独厮守ぢ 2020-12-08 14:52

I have declared a temp table to hold all the required values as follows:

    DECLARE @temp TABLE
    (
    Password INT,
    IdTran INT,
    Kind VARCHAR(16)
         


        
5条回答
  •  有刺的猬
    2020-12-08 15:10

    you could use a cursor:

    DECLARE @id int
    DECLARE @pass varchar(100)
    
    DECLARE cur CURSOR FOR SELECT Id, Password FROM @temp
    OPEN cur
    
    FETCH NEXT FROM cur INTO @id, @pass
    
    WHILE @@FETCH_STATUS = 0 BEGIN
        EXEC mysp @id, @pass ... -- call your sp here
        FETCH NEXT FROM cur INTO @id, @pass
    END
    
    CLOSE cur    
    DEALLOCATE cur
    

提交回复
热议问题