使用不同的值遍历sql脚本
以下是我脚本的一部分:使用不同的值遍历sql脚本,sql,sql-server-2008,Sql,Sql Server 2008,以下是我脚本的一部分: 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个值,然后按顺序遍历脚
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
?好吧,根据您的评论,那么我想游标是最好的方法。这样做应该可以:
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
是的,您可以使用游标来完成此操作。但是,真的有必要在循环中执行吗?也许有另一种方法可以使用连接@Lamak-我想执行一个很大的脚本,我不确定连接是否有效。@nearome-SQL可以处理比您想象的更大的工作负载,很可能。发布您的整个问题,我们可能会更好地帮助您。