以下是我的脚本的一部分:
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
我的问题是有一种方法可以在@dataId
中输入100个值,它会按顺序遍历脚本吗?有没有更简单的方法在SQL Server 2008中执行此操作,而不是每次都手动输入@dataId
?
答案 0 :(得分: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