SQL Query fails in ExecuteQuery, but not in SQL Server Management Studio

75 views
Skip to first unread message

Robert Milasan

unread,
Mar 31, 2016, 3:14:05 AM3/31/16
to Execute Query
Hello, I'm trying to run this SQL Query:

SET NOCOUNT ON;
DECLARE
@db_name nvarchar(130);
DECLARE
@cnt int = 1;
DECLARE
@db_cnt int = 0;
DECLARE
@dbname nvarchar(130);
DECLARE
@dbcmd nvarchar(200);

DECLARE databases CURSOR FOR SELECT name FROM sys
.databases
SET
@db_cnt = (SELECT COUNT(name) FROM sys.databases)

OPEN databases
;
WHILE
@cnt < CAST(@db_cnt AS int) BEGIN
    FETCH NEXT FROM databases INTO
@db_name;
    SET
@cnt = @cnt + 1;
      PRINT N
'Using database: ' + @db_name;

      SET
@dbcmd = N'USE ' + CAST(@db_name AS nvarchar(130));
      PRINT
@dbcmd;

END;

CLOSE databases
;
DEALLOCATE databases
;
GO

Now this complex SQL query works if I run it on Windows SQL Server Management Studio, but doesn't work in ExecuteQuery app connected to the same DB server. Is there some settings I'm missing?

Thanks!

Takis Diakoumis

unread,
Mar 31, 2016, 8:28:57 PM3/31/16
to execut...@googlegroups.com

Hello

Your mileage will vary with that one. What I suggest you try is executing the entire thing as a block.

There is an option on right-click that allows you to execute as a single statement. That's your best bet.

Try that and let me know how you go.

Thanks
Takis

--
You received this message because you are subscribed to the Google Groups "Execute Query" group.
To unsubscribe from this group and stop receiving emails from it, send an email to executequery...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages