[FIXED] traverse through database in sql using while loop

Issue

Here I wanted to run the same query on multiple databases using a while loop.

  1. I created a table with the name '@database' and added the names of the database that I want to traverse through in it
  2. used while loop to traverse through databases names to run that particular query in each database

It didn't work.(it's running twice for the database which has been selected on the left dropdown of SQL management studio )

Tables in both databases are the same.

DECLARE @database TABLE (id INT,name varchar(50))

insert into @database values (1, 'Databse_one'), (2, 'Database_two')

DECLARE @cnt INT =2, @int INT=1,@dbname NVARCHAR(MAX)

while(@int <=@cnt)

 begin

select @dbname='use '+name from @database
where id=@int

EXEC sp_sqlexec @dbname
select top 10 db_name() DB_NAME, req_number, record Type,order_date from ORDER_DOCUMENT order by order_date desc

set @int = @int+1

end

Solution

It wouldn't work like that even after you corrected the typos you already have. After use dbName, it would require a GO or be that in a different batch on the same connection. Instead you could write that as:

DECLARE @database TABLE(id INT, name VARCHAR(50));
INSERT INTO @database VALUES(1, 'Databse_one'), (2, 'Database_two');
DECLARE @cnt INT=2, @int INT=1, @dbname NVARCHAR(MAX);
WHILE(@int<=@cnt)BEGIN
    SELECT @dbname=name FROM @database WHERE id=@int;
    DECLARE @sql NVARCHAR(MAX);
    SET @sql=N'select top(10) '''+@dbname+N''',req_number, record Type,order_date from '+QUOTENAME(@dbname)+N'.dbo.ORDER_DOCUMENT order by order_date desc;';
    EXEC sys.sp_executesql @stmt=@sql;
    SET @int=@int+1;
END;


Answered By - Cetin Basoz
Answer Checked By - Willingham (sql.easybugfix Volunteer)

Related Posts

Post a Comment