0

I have to put a result of a query (single column and value is being pulled) into a variable. I'm trying to use a cursor however I choose the database to query based on a variable here is my query

SELECT productName, price FROM @ShopName.dbo.Products WHERE ProductName = @ProductName

@ShopName variable is being pulled from the database first and assigned to the variable using a cursor. @ProductName variable is being populated by an input parameter coming from API. I have to get ProductName from a specific database (there are multiple databases with products), but the query above throws syntax errors. Additionally when I tried ad hoc query assigned to a variable:

SET @Sql = N'SELECT productName, price FROM ' + QUOTENAME(@ShopName) + '.dbo.Products WHERE ProductName = ' + @ProductName

It doesn't allow to use it in

DECLARE cursorT CURSOR
FOR
@Sql

This throws Incorrect syntax near '@Sql', Expecting '(', SELECT, or WITH

Is there any way to make it possible to use that query in cursor while using the variable with database name in it?

3
  • If you're using dynamic SQL, you have to do it all in dynamic SQL, you can't mix it up. So you need to also run your cursor within the dynamic SQL. Commented Jul 10, 2020 at 8:12
  • 1
    What part of this requires use of a cursor? Commented Jul 10, 2020 at 8:14
  • It does not make sense that you are calling ...FROM @ShopName... unless @ShopName is a table variable? Please provide us with the complete code and not partial code? Commented Jul 10, 2020 at 8:20

1 Answer 1

2

Cursors should be right at the bottom of your bag of techniques, used sparingly and with great care, only when necessary. I can't tell if it's necessary in your case, there's not enough code to know. But I wanted to get that out before continuing.

As a point of purely academic interest, yes, there are some ways you can do this. Two main ways:

  1. Declare a cursor in the dynamic SQL, as Dale suggested. You can still use the cursor in static code which follows the declaration if the cursor is global.
  2. Use dynamic SQL to drop the results into something with scope outside of the dynamic sql, like a temp table. The cursor over the temp table.

1 is just bad. It is likely to result in code which is extremely difficult to understand in future. I include it for curiosity only. 2 is reasonable.

Examples:

-- 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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.