sql - Script -> Query for multiple databases -


I have the following problem: I want to execute a query on more than one database on my SQL server. Each customer has a separate database, all of them have the same table and their names are the same. So a database is kde_01_Miller , then a kde_02_Mueller and so on ...

I want to execute every query from that database.

Here's what I have tried:

  Announcement name @ VARCHAR (100) - Database name DECLARE @dothis nvarchar (200) DECLARE db_cursor Name for Master Name Cursor for Dbo.sysdatabases where name like 'kde_0%', name OPEN db_cursor ahead of FETCH Db_cursor INTO @name WHILE @@ FETCH_STATUS = 0 BEGIN set @dothis = '[' + @name + ']' exec sp_executesql @usis / * Turn off the query * / dbo.basicdata / * end query * / FETCH in the next character db_cursor to close @name. Db_cursor DEALLOCATE db_cursor  

The problem is that the query does not work properly. The Usage statement is not working. I get a result for every database I have, but the result is always the same, based on the database I am currently doing a query.

I have tried the following and have done this: I did this instead of my time-loop:

  WHILE @@ FETCH_STATUS = 0 BEGIN set @ Ethics = 'Select' + QUOTENAME (@name) + '. Db_cursor @name END  

before Dbo.basicdata 'exec sp_executesql @usis FETCH, but I do not like this method because you have quotename (@name) is required.

How do I work out the first example?

This is not possible, since sp_executesql has its own vested batch Is implemented in the form, it means that you have actually "used" other databases, but only those batches that I mentioned earlier

I will try to be more clear, you have it The code is a batch , because none of the " GO " commands I nside (read my SQL comments): DECLARE @name VARCHAR (100) - Database name DECLARE @usis nvarchar (200) DECLARE db_cursor cursor for selection name from master.dbo.sysdatabases where name like 'kde_0%' is named OPEN Db_cursor FETCH Db_cursor INTO @name WHILE @@ FETCH_STATUS = 0 BEGIN set @dothis = '[' + @name + ']' - this will make another batch and @ it anything with your current execution batch Do not have - that starts sp_executesql exec sp_executesql @dothis / * Ask questions * / dbo.basicdata / * end query * / FETCH next to db_cursor INTO @ name end db_cursor DEALLOCATE db_cursor

Therefore, only one way left Whatever you want to do with the database inside @Datiis, type:

  declare @dothis newcomer (max) set @dothis = '[+ + + +' ' Name + '] - Select the query Start Discript but from dbo.basicdata - Query and' exec sp_executesql @dothis  

Comments

Popular posts from this blog

Member with no value in F# -

java - Joda Time Interval Not returning what I expect -

c# - Showing a SelectedItem's Property -