0

这是我的脚本的一部分:

DECLARE @dataId int = 123               

-- Here I run more SQL scripts using @dataId

Select from table A where id = @dataId 

-- So on it goes in other tables and delete and update other tables

我的问题是有一种方法可以输入 100 个值@dataId,它会按顺序遍历脚本吗?在 SQL Server 2008 中是否有更简单的方法来执行此操作,而不是@dataId每次手动输入?

4

1 回答 1

0

好吧,根据您的评论,我想这CURSORS就是要走的路。这样的事情应该做:

DECLARE @dataId int

DECLARE DataIds CURSOR LOCAL STATIC READ_ONLY FORWARD_ONLY FOR
SELECT DataId
FROM YourTableWithDataIdsHere

OPEN DataIds
FETCH NEXT FROM DataIds INTO @dataId
WHILE @@FETCH_STATUS = 0
BEGIN

    -- Here I run more SQL scripts using @dataId

    Select from table A where id = @dataId 

    -- So on it goes in other tables and delete and update other tables

    FETCH NEXT FROM DataIds INTO @dataId
END

CLOSE DataIds
DEALLOCATE DataIds
于 2012-11-07T21:21:29.870 回答