我有一个游标,它获取数据并将其提取到@data_table 中。然后在 while 循环中,我想将该 @data_table 传递给另一个游标(作为表名)以运行更多处理。我不断收到声明 @data_table 错误。我该如何解决?
DECLARE @var_name varchar(50)
DECLARE @data_table varchar(50)
DECLARE @data_value varchar(50)
DECLARE curDiscreteVars CURSOR LOCAL
FOR SELECT DISTINCT v.var_name, v.data_table
FROM dbo.vars v
GROUP BY v.var_name, v.data_table
-- Loop through cursor, translating variable values as needed, and generate counts for each val_code for a variable
OPEN curDiscreteVars
FETCH NEXT FROM curDiscreteVars
INTO @var_name, @data_table
WHILE @@FETCH_STATUS = 0
BEGIN
--loop through all possible data values
DECLARE curValues CURSOR LOCAL
FOR SELECT DISTINCT @var_name
FROM @data_table
OPEN curValues
FETCH NEXT FROM curValues
INTO @data_value
WHILE @@FETCH_STATUS = 0
BEGIN
print @var_name
FETCH NEXT FROM curValues
INTO @data_value
END
CLOSE curValues
DEALLOCATE curValues
FETCH NEXT FROM curDiscreteVars
INTO @var_name, @data_table
END
CLOSE curDiscreteVars
DEALLOCATE curDiscreteVars