Select from in multi-tables and multi-database
up vote
0
down vote
favorite
I need to do something like:
SELECT data, hour, user,
FROM [dbx].dbo._datamodel AS db0
UNION
SELECT data, hour, user,
FROM [dby].dbo._datamodel AS db1
UNION
SELECT data, hour, user,
FROM [dbz].dbo._datamodel AS db02
UNION
...
We have to do it for a lot of DDBB and we are looking the way to do it automatically for each DB with a condition.
We don't want to add the UNION and select for each DB every time.
Any one have an advice or idea how to do it?
sql tsql
New contributor
Bernat Moragas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
0
down vote
favorite
I need to do something like:
SELECT data, hour, user,
FROM [dbx].dbo._datamodel AS db0
UNION
SELECT data, hour, user,
FROM [dby].dbo._datamodel AS db1
UNION
SELECT data, hour, user,
FROM [dbz].dbo._datamodel AS db02
UNION
...
We have to do it for a lot of DDBB and we are looking the way to do it automatically for each DB with a condition.
We don't want to add the UNION and select for each DB every time.
Any one have an advice or idea how to do it?
sql tsql
New contributor
Bernat Moragas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
create a procedure with a parameter db_name run it as many times as you want with giving the db name you need
– Himanshu Ahuja
2 days ago
What you mean by DDBB??
– Sami
2 days ago
Hi, DDBB means DataBase,
– Bernat Moragas
17 hours ago
Which DBMS product are you using? "SQL" is just a query language, not the name of a specific database product. Please add a tag for the database product you are usingpostgresql,oracle,sql-server,db2, ...
– a_horse_with_no_name
14 hours ago
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I need to do something like:
SELECT data, hour, user,
FROM [dbx].dbo._datamodel AS db0
UNION
SELECT data, hour, user,
FROM [dby].dbo._datamodel AS db1
UNION
SELECT data, hour, user,
FROM [dbz].dbo._datamodel AS db02
UNION
...
We have to do it for a lot of DDBB and we are looking the way to do it automatically for each DB with a condition.
We don't want to add the UNION and select for each DB every time.
Any one have an advice or idea how to do it?
sql tsql
New contributor
Bernat Moragas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I need to do something like:
SELECT data, hour, user,
FROM [dbx].dbo._datamodel AS db0
UNION
SELECT data, hour, user,
FROM [dby].dbo._datamodel AS db1
UNION
SELECT data, hour, user,
FROM [dbz].dbo._datamodel AS db02
UNION
...
We have to do it for a lot of DDBB and we are looking the way to do it automatically for each DB with a condition.
We don't want to add the UNION and select for each DB every time.
Any one have an advice or idea how to do it?
sql tsql
sql tsql
New contributor
Bernat Moragas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Bernat Moragas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 14 hours ago
a_horse_with_no_name
286k45429526
286k45429526
New contributor
Bernat Moragas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 2 days ago
Bernat Moragas
11
11
New contributor
Bernat Moragas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Bernat Moragas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Bernat Moragas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
create a procedure with a parameter db_name run it as many times as you want with giving the db name you need
– Himanshu Ahuja
2 days ago
What you mean by DDBB??
– Sami
2 days ago
Hi, DDBB means DataBase,
– Bernat Moragas
17 hours ago
Which DBMS product are you using? "SQL" is just a query language, not the name of a specific database product. Please add a tag for the database product you are usingpostgresql,oracle,sql-server,db2, ...
– a_horse_with_no_name
14 hours ago
add a comment |
create a procedure with a parameter db_name run it as many times as you want with giving the db name you need
– Himanshu Ahuja
2 days ago
What you mean by DDBB??
– Sami
2 days ago
Hi, DDBB means DataBase,
– Bernat Moragas
17 hours ago
Which DBMS product are you using? "SQL" is just a query language, not the name of a specific database product. Please add a tag for the database product you are usingpostgresql,oracle,sql-server,db2, ...
– a_horse_with_no_name
14 hours ago
create a procedure with a parameter db_name run it as many times as you want with giving the db name you need
– Himanshu Ahuja
2 days ago
create a procedure with a parameter db_name run it as many times as you want with giving the db name you need
– Himanshu Ahuja
2 days ago
What you mean by DDBB??
– Sami
2 days ago
What you mean by DDBB??
– Sami
2 days ago
Hi, DDBB means DataBase,
– Bernat Moragas
17 hours ago
Hi, DDBB means DataBase,
– Bernat Moragas
17 hours ago
Which DBMS product are you using? "SQL" is just a query language, not the name of a specific database product. Please add a tag for the database product you are using
postgresql, oracle, sql-server, db2, ...– a_horse_with_no_name
14 hours ago
Which DBMS product are you using? "SQL" is just a query language, not the name of a specific database product. Please add a tag for the database product you are using
postgresql, oracle, sql-server, db2, ...– a_horse_with_no_name
14 hours ago
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
I think you are looking for DynamicSQL
DECLARE @SQL NVARCHAR(MAX) = N'SELECT data, hour, user FROM ';
SELECT @SQL = @SQL + [name] + '.dbo._datamodel UNION SELECT * FROM '
FROM master.dbo.sysdatabases
WHERE dbid > 4 ; --ReportServer and ReportServerTempDB with dbid 5 and 6
SET @SQL = LEFT(@SQL, LEN(@SQL) - 21);
SELECT @SQL;
--EXECUTE sp_executesql @SQL;
add a comment |
up vote
0
down vote
I would simply create a view in a standard database that has the union:
create view v_datamodel as
select . . .
union
. . .;
(Side note: If you really just want union all, then use it. union incurs lots of extra overhead.)
If your set of databases is fixed, then you can create the view once -- set it and forget it.
If databases are being added and removed, then you have a process for doing so. This process can update the view, perhaps by using dynamic SQL as suggested in another answer. Or, you might have the list of appropriate databases somewhere other than sysdatabases.
If the database are created "ad hoc", then you could use DDL triggers. I'm not sure if you can define these on create database, but within each database, you could check for the creation of the "data model" table.
add a comment |
up vote
0
down vote
I was trying and still don't working.
I've been trying with :
Declare @sql AS VARCHAR(8000)
SET @sql = ' IF ''?'' like ''dat%''
EXECUTE ('' USE [?]
Select process, processVers
from dbo._datamodel
'')'
EXEC sp_MSforeachdb @sql
It works for the SQL query, but i doesn't for the view. :(
Any advice?
New contributor
Bernat Moragas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Please don't add additional information as comments. EDIT your question instead
– a_horse_with_no_name
14 hours ago
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I think you are looking for DynamicSQL
DECLARE @SQL NVARCHAR(MAX) = N'SELECT data, hour, user FROM ';
SELECT @SQL = @SQL + [name] + '.dbo._datamodel UNION SELECT * FROM '
FROM master.dbo.sysdatabases
WHERE dbid > 4 ; --ReportServer and ReportServerTempDB with dbid 5 and 6
SET @SQL = LEFT(@SQL, LEN(@SQL) - 21);
SELECT @SQL;
--EXECUTE sp_executesql @SQL;
add a comment |
up vote
0
down vote
I think you are looking for DynamicSQL
DECLARE @SQL NVARCHAR(MAX) = N'SELECT data, hour, user FROM ';
SELECT @SQL = @SQL + [name] + '.dbo._datamodel UNION SELECT * FROM '
FROM master.dbo.sysdatabases
WHERE dbid > 4 ; --ReportServer and ReportServerTempDB with dbid 5 and 6
SET @SQL = LEFT(@SQL, LEN(@SQL) - 21);
SELECT @SQL;
--EXECUTE sp_executesql @SQL;
add a comment |
up vote
0
down vote
up vote
0
down vote
I think you are looking for DynamicSQL
DECLARE @SQL NVARCHAR(MAX) = N'SELECT data, hour, user FROM ';
SELECT @SQL = @SQL + [name] + '.dbo._datamodel UNION SELECT * FROM '
FROM master.dbo.sysdatabases
WHERE dbid > 4 ; --ReportServer and ReportServerTempDB with dbid 5 and 6
SET @SQL = LEFT(@SQL, LEN(@SQL) - 21);
SELECT @SQL;
--EXECUTE sp_executesql @SQL;
I think you are looking for DynamicSQL
DECLARE @SQL NVARCHAR(MAX) = N'SELECT data, hour, user FROM ';
SELECT @SQL = @SQL + [name] + '.dbo._datamodel UNION SELECT * FROM '
FROM master.dbo.sysdatabases
WHERE dbid > 4 ; --ReportServer and ReportServerTempDB with dbid 5 and 6
SET @SQL = LEFT(@SQL, LEN(@SQL) - 21);
SELECT @SQL;
--EXECUTE sp_executesql @SQL;
edited 2 days ago
answered 2 days ago
Sami
6,24031038
6,24031038
add a comment |
add a comment |
up vote
0
down vote
I would simply create a view in a standard database that has the union:
create view v_datamodel as
select . . .
union
. . .;
(Side note: If you really just want union all, then use it. union incurs lots of extra overhead.)
If your set of databases is fixed, then you can create the view once -- set it and forget it.
If databases are being added and removed, then you have a process for doing so. This process can update the view, perhaps by using dynamic SQL as suggested in another answer. Or, you might have the list of appropriate databases somewhere other than sysdatabases.
If the database are created "ad hoc", then you could use DDL triggers. I'm not sure if you can define these on create database, but within each database, you could check for the creation of the "data model" table.
add a comment |
up vote
0
down vote
I would simply create a view in a standard database that has the union:
create view v_datamodel as
select . . .
union
. . .;
(Side note: If you really just want union all, then use it. union incurs lots of extra overhead.)
If your set of databases is fixed, then you can create the view once -- set it and forget it.
If databases are being added and removed, then you have a process for doing so. This process can update the view, perhaps by using dynamic SQL as suggested in another answer. Or, you might have the list of appropriate databases somewhere other than sysdatabases.
If the database are created "ad hoc", then you could use DDL triggers. I'm not sure if you can define these on create database, but within each database, you could check for the creation of the "data model" table.
add a comment |
up vote
0
down vote
up vote
0
down vote
I would simply create a view in a standard database that has the union:
create view v_datamodel as
select . . .
union
. . .;
(Side note: If you really just want union all, then use it. union incurs lots of extra overhead.)
If your set of databases is fixed, then you can create the view once -- set it and forget it.
If databases are being added and removed, then you have a process for doing so. This process can update the view, perhaps by using dynamic SQL as suggested in another answer. Or, you might have the list of appropriate databases somewhere other than sysdatabases.
If the database are created "ad hoc", then you could use DDL triggers. I'm not sure if you can define these on create database, but within each database, you could check for the creation of the "data model" table.
I would simply create a view in a standard database that has the union:
create view v_datamodel as
select . . .
union
. . .;
(Side note: If you really just want union all, then use it. union incurs lots of extra overhead.)
If your set of databases is fixed, then you can create the view once -- set it and forget it.
If databases are being added and removed, then you have a process for doing so. This process can update the view, perhaps by using dynamic SQL as suggested in another answer. Or, you might have the list of appropriate databases somewhere other than sysdatabases.
If the database are created "ad hoc", then you could use DDL triggers. I'm not sure if you can define these on create database, but within each database, you could check for the creation of the "data model" table.
answered 2 days ago
Gordon Linoff
744k32285390
744k32285390
add a comment |
add a comment |
up vote
0
down vote
I was trying and still don't working.
I've been trying with :
Declare @sql AS VARCHAR(8000)
SET @sql = ' IF ''?'' like ''dat%''
EXECUTE ('' USE [?]
Select process, processVers
from dbo._datamodel
'')'
EXEC sp_MSforeachdb @sql
It works for the SQL query, but i doesn't for the view. :(
Any advice?
New contributor
Bernat Moragas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Please don't add additional information as comments. EDIT your question instead
– a_horse_with_no_name
14 hours ago
add a comment |
up vote
0
down vote
I was trying and still don't working.
I've been trying with :
Declare @sql AS VARCHAR(8000)
SET @sql = ' IF ''?'' like ''dat%''
EXECUTE ('' USE [?]
Select process, processVers
from dbo._datamodel
'')'
EXEC sp_MSforeachdb @sql
It works for the SQL query, but i doesn't for the view. :(
Any advice?
New contributor
Bernat Moragas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Please don't add additional information as comments. EDIT your question instead
– a_horse_with_no_name
14 hours ago
add a comment |
up vote
0
down vote
up vote
0
down vote
I was trying and still don't working.
I've been trying with :
Declare @sql AS VARCHAR(8000)
SET @sql = ' IF ''?'' like ''dat%''
EXECUTE ('' USE [?]
Select process, processVers
from dbo._datamodel
'')'
EXEC sp_MSforeachdb @sql
It works for the SQL query, but i doesn't for the view. :(
Any advice?
New contributor
Bernat Moragas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I was trying and still don't working.
I've been trying with :
Declare @sql AS VARCHAR(8000)
SET @sql = ' IF ''?'' like ''dat%''
EXECUTE ('' USE [?]
Select process, processVers
from dbo._datamodel
'')'
EXEC sp_MSforeachdb @sql
It works for the SQL query, but i doesn't for the view. :(
Any advice?
New contributor
Bernat Moragas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 14 hours ago
New contributor
Bernat Moragas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered 14 hours ago
Bernat Moragas
11
11
New contributor
Bernat Moragas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Bernat Moragas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Bernat Moragas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Please don't add additional information as comments. EDIT your question instead
– a_horse_with_no_name
14 hours ago
add a comment |
Please don't add additional information as comments. EDIT your question instead
– a_horse_with_no_name
14 hours ago
Please don't add additional information as comments. EDIT your question instead
– a_horse_with_no_name
14 hours ago
Please don't add additional information as comments. EDIT your question instead
– a_horse_with_no_name
14 hours ago
add a comment |
Bernat Moragas is a new contributor. Be nice, and check out our Code of Conduct.
Bernat Moragas is a new contributor. Be nice, and check out our Code of Conduct.
Bernat Moragas is a new contributor. Be nice, and check out our Code of Conduct.
Bernat Moragas is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53350006%2fselect-from-in-multi-tables-and-multi-database%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
create a procedure with a parameter db_name run it as many times as you want with giving the db name you need
– Himanshu Ahuja
2 days ago
What you mean by DDBB??
– Sami
2 days ago
Hi, DDBB means DataBase,
– Bernat Moragas
17 hours ago
Which DBMS product are you using? "SQL" is just a query language, not the name of a specific database product. Please add a tag for the database product you are using
postgresql,oracle,sql-server,db2, ...– a_horse_with_no_name
14 hours ago