在查询中使用变量时无法查询并将数据放入游标中
Can't query and put data inside a cursor when using variable inside the query
我必须将查询结果(正在提取单列和值)放入变量中。我正在尝试使用游标,但是我选择了基于变量查询的数据库,这里是我的查询
SELECT productName, price FROM @ShopName.dbo.Products WHERE ProductName = @ProductName
首先从数据库中提取@ShopName 变量,然后使用游标将其分配给该变量。 @ProductName 变量由来自 API 的输入参数填充。我必须从特定数据库中获取 ProductName(产品有多个数据库),但上面的查询会引发语法错误。此外,当我尝试分配给变量的临时查询时:
SET @Sql = N'SELECT productName, price FROM ' + QUOTENAME(@ShopName) + '.dbo.Products WHERE ProductName = ' + @ProductName
不允许使用
DECLARE cursorT CURSOR
FOR
@Sql
这会抛出 Incorrect syntax near '@Sql', Expecting '(', SELECT, or WITH
有什么方法可以在游标中使用带有数据库名称的变量时使用该查询吗?
光标应该放在你的技术包的底部,只有在必要时才谨慎使用。我不知道你的情况是否有必要,没有足够的代码知道。但我想在继续之前把它弄出来。
作为纯学术兴趣点,是的,有一些方法可以做到这一点。两种主要方式:
- 按照 Dale 的建议,在动态 SQL 中声明游标。如果游标是全局的,您仍然可以在声明后的静态代码中使用游标。
- 使用动态 SQL 将结果放入动态 sql 范围之外的内容,例如临时文件 table。光标在温度 table.
1 很糟糕。这很可能会导致代码在未来极难理解。我把它包括在内只是出于好奇。 2是合理的。
示例:
-- some dummy schema and data to work with
create table t(i int);
insert t values(1), (2);
-- option 1: declare a cursor dynamically, use it statically (don't do this)
declare @i int;
exec sp_executesql N'declare c cursor global for select i from t';
open c;
fetch next from c into @i;
while (@@fetch_status = 0)
begin
print @i;
fetch next from c into @i;
end
close c;
deallocate c;
-- option 2: dynamically dump data to a table, eg a temp table
create table #u(i int);
exec sp_executesql N'insert #u (i) select i from t';
declare c cursor local for select i from #u;
declare @i int;
open c;
fetch next from c into @i;
while (@@fetch_status = 0)
begin
print @i;
fetch next from c into @i;
end
close c;
deallocate c;
我必须将查询结果(正在提取单列和值)放入变量中。我正在尝试使用游标,但是我选择了基于变量查询的数据库,这里是我的查询
SELECT productName, price FROM @ShopName.dbo.Products WHERE ProductName = @ProductName
首先从数据库中提取@ShopName 变量,然后使用游标将其分配给该变量。 @ProductName 变量由来自 API 的输入参数填充。我必须从特定数据库中获取 ProductName(产品有多个数据库),但上面的查询会引发语法错误。此外,当我尝试分配给变量的临时查询时:
SET @Sql = N'SELECT productName, price FROM ' + QUOTENAME(@ShopName) + '.dbo.Products WHERE ProductName = ' + @ProductName
不允许使用
DECLARE cursorT CURSOR
FOR
@Sql
这会抛出 Incorrect syntax near '@Sql', Expecting '(', SELECT, or WITH
有什么方法可以在游标中使用带有数据库名称的变量时使用该查询吗?
光标应该放在你的技术包的底部,只有在必要时才谨慎使用。我不知道你的情况是否有必要,没有足够的代码知道。但我想在继续之前把它弄出来。
作为纯学术兴趣点,是的,有一些方法可以做到这一点。两种主要方式:
- 按照 Dale 的建议,在动态 SQL 中声明游标。如果游标是全局的,您仍然可以在声明后的静态代码中使用游标。
- 使用动态 SQL 将结果放入动态 sql 范围之外的内容,例如临时文件 table。光标在温度 table.
1 很糟糕。这很可能会导致代码在未来极难理解。我把它包括在内只是出于好奇。 2是合理的。
示例:
-- some dummy schema and data to work with
create table t(i int);
insert t values(1), (2);
-- option 1: declare a cursor dynamically, use it statically (don't do this)
declare @i int;
exec sp_executesql N'declare c cursor global for select i from t';
open c;
fetch next from c into @i;
while (@@fetch_status = 0)
begin
print @i;
fetch next from c into @i;
end
close c;
deallocate c;
-- option 2: dynamically dump data to a table, eg a temp table
create table #u(i int);
exec sp_executesql N'insert #u (i) select i from t';
declare c cursor local for select i from #u;
declare @i int;
open c;
fetch next from c into @i;
while (@@fetch_status = 0)
begin
print @i;
fetch next from c into @i;
end
close c;
deallocate c;