Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用不同的值遍历sql脚本_Sql_Sql Server 2008 - Fatal编程技术网

使用不同的值遍历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可以处理比您想象的更大的工作负载,很可能。发布您的整个问题,我们可能会更好地帮助您。