Is it possible to concatenate query results to form a new query?
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
I have now:
SELECT TABLE_SCHEMA ,
TABLE_NAME ,
COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = 'NNN';
This gives me several hundreds of rows, because several tables contain field with that name. I want to search some value from these tables and columns by using the previous data in new query like:
SELECT <COLUMN_NAME> FROM <TABLE_SCHEMA>.<TABLE_NAME>
Could I include the results of the first query to the tags in second one in one query and how? I would like to avoid calling the every schema, table and column by hand to check only minor things in every table.
Source:
I took the first query from:
https://stackoverflow.com/questions/420741/getting-list-of-tables-and-fields-in-each-in-a-database
sql-server sql-server-2012 information-schema
add a comment |Â
up vote
1
down vote
favorite
I have now:
SELECT TABLE_SCHEMA ,
TABLE_NAME ,
COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = 'NNN';
This gives me several hundreds of rows, because several tables contain field with that name. I want to search some value from these tables and columns by using the previous data in new query like:
SELECT <COLUMN_NAME> FROM <TABLE_SCHEMA>.<TABLE_NAME>
Could I include the results of the first query to the tags in second one in one query and how? I would like to avoid calling the every schema, table and column by hand to check only minor things in every table.
Source:
I took the first query from:
https://stackoverflow.com/questions/420741/getting-list-of-tables-and-fields-in-each-in-a-database
sql-server sql-server-2012 information-schema
SET @sql='SELECT ' + <COLUMN_NAME> + ' FROM ' + <TABLE_SCHEMA> + '.' + <TABLE_NAME>; EXEC (@sql);
. Iterate by recordset (or concate in a query, then execute).
– Akina
1 hour ago
@Akina Could you elaborate that as an answer, seems meaningful approach?
– mico
1 hour ago
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have now:
SELECT TABLE_SCHEMA ,
TABLE_NAME ,
COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = 'NNN';
This gives me several hundreds of rows, because several tables contain field with that name. I want to search some value from these tables and columns by using the previous data in new query like:
SELECT <COLUMN_NAME> FROM <TABLE_SCHEMA>.<TABLE_NAME>
Could I include the results of the first query to the tags in second one in one query and how? I would like to avoid calling the every schema, table and column by hand to check only minor things in every table.
Source:
I took the first query from:
https://stackoverflow.com/questions/420741/getting-list-of-tables-and-fields-in-each-in-a-database
sql-server sql-server-2012 information-schema
I have now:
SELECT TABLE_SCHEMA ,
TABLE_NAME ,
COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = 'NNN';
This gives me several hundreds of rows, because several tables contain field with that name. I want to search some value from these tables and columns by using the previous data in new query like:
SELECT <COLUMN_NAME> FROM <TABLE_SCHEMA>.<TABLE_NAME>
Could I include the results of the first query to the tags in second one in one query and how? I would like to avoid calling the every schema, table and column by hand to check only minor things in every table.
Source:
I took the first query from:
https://stackoverflow.com/questions/420741/getting-list-of-tables-and-fields-in-each-in-a-database
sql-server sql-server-2012 information-schema
sql-server sql-server-2012 information-schema
edited 1 hour ago
asked 1 hour ago


mico
2861516
2861516
SET @sql='SELECT ' + <COLUMN_NAME> + ' FROM ' + <TABLE_SCHEMA> + '.' + <TABLE_NAME>; EXEC (@sql);
. Iterate by recordset (or concate in a query, then execute).
– Akina
1 hour ago
@Akina Could you elaborate that as an answer, seems meaningful approach?
– mico
1 hour ago
add a comment |Â
SET @sql='SELECT ' + <COLUMN_NAME> + ' FROM ' + <TABLE_SCHEMA> + '.' + <TABLE_NAME>; EXEC (@sql);
. Iterate by recordset (or concate in a query, then execute).
– Akina
1 hour ago
@Akina Could you elaborate that as an answer, seems meaningful approach?
– mico
1 hour ago
SET @sql='SELECT ' + <COLUMN_NAME> + ' FROM ' + <TABLE_SCHEMA> + '.' + <TABLE_NAME>; EXEC (@sql);
. Iterate by recordset (or concate in a query, then execute).– Akina
1 hour ago
SET @sql='SELECT ' + <COLUMN_NAME> + ' FROM ' + <TABLE_SCHEMA> + '.' + <TABLE_NAME>; EXEC (@sql);
. Iterate by recordset (or concate in a query, then execute).– Akina
1 hour ago
@Akina Could you elaborate that as an answer, seems meaningful approach?
– mico
1 hour ago
@Akina Could you elaborate that as an answer, seems meaningful approach?
– mico
1 hour ago
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
Unless you are explicitly trying to write queries that work in multiple engines, I recommend sticking to the catalog views in the sys
schema rather than INFORMATION_SCHEMA
(see this and this). You'll need dynamic SQL for this anyway, and in other engines the syntax for this will vary greatly:
DECLARE @sql nvarchar(max) = N'';
SELECT @sql += N'SELECT ' + QUOTENAME(c.name) + N' FROM '
+ QUOTENAME(s.name) + N'.' + QUOTENAME(t.name) + N';'
FROM sys.schemas AS s
INNER JOIN sys.tables AS t
ON s.[schema_id] = t.[schema_id]
INNER JOIN sys.columns AS c
ON t.[object_id] = c.[object_id]
WHERE c.name = N'NNN';
EXEC sys.sp_executesql @sql;
If you want the column to be listed as the name of the table instead of the name of the column:
...
SELECT @sql += N'SELECT ' + QUOTENAME(t.name) + N' = '
+ QUOTENAME(s.name + N'.' + t.name) + N' FROM '
+ QUOTENAME(s.name) + N'.' + QUOTENAME(t.name) + N';'
FROM
...
And if you want them all in a single result set, you could add the table name as a column, and use a UNION
(and coerce all the columns into a compatible type, just in case):
...
SELECT @sql += N'UNION ALL SELECT [table] = '''
+ QUOTENAME(s.name + N'.' + t.name) + N''', '
+ N' value = CONVERT(nvarchar(4000),'
+ QUOTENAME(c.name) + N') FROM '
+ QUOTENAME(s.name) + N'.' + QUOTENAME(t.name) + N';'
FROM
...
SET @sql = STUFF(@sql, 1, 10, N'');
...
Thanks for quick reply. My @sql declaration ended up in form:SELECT @sql += N'SELECT distinct' + QUOTENAME(c.name) + N' as ' + QUOTENAME(t.name) + N' FROM ' + QUOTENAME(s.name) + N'.' + QUOTENAME(t.name) + N';'
to be able to name data I found. Without that change, the results were with same title and thus useless for my purposes.
– mico
30 mins ago
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Unless you are explicitly trying to write queries that work in multiple engines, I recommend sticking to the catalog views in the sys
schema rather than INFORMATION_SCHEMA
(see this and this). You'll need dynamic SQL for this anyway, and in other engines the syntax for this will vary greatly:
DECLARE @sql nvarchar(max) = N'';
SELECT @sql += N'SELECT ' + QUOTENAME(c.name) + N' FROM '
+ QUOTENAME(s.name) + N'.' + QUOTENAME(t.name) + N';'
FROM sys.schemas AS s
INNER JOIN sys.tables AS t
ON s.[schema_id] = t.[schema_id]
INNER JOIN sys.columns AS c
ON t.[object_id] = c.[object_id]
WHERE c.name = N'NNN';
EXEC sys.sp_executesql @sql;
If you want the column to be listed as the name of the table instead of the name of the column:
...
SELECT @sql += N'SELECT ' + QUOTENAME(t.name) + N' = '
+ QUOTENAME(s.name + N'.' + t.name) + N' FROM '
+ QUOTENAME(s.name) + N'.' + QUOTENAME(t.name) + N';'
FROM
...
And if you want them all in a single result set, you could add the table name as a column, and use a UNION
(and coerce all the columns into a compatible type, just in case):
...
SELECT @sql += N'UNION ALL SELECT [table] = '''
+ QUOTENAME(s.name + N'.' + t.name) + N''', '
+ N' value = CONVERT(nvarchar(4000),'
+ QUOTENAME(c.name) + N') FROM '
+ QUOTENAME(s.name) + N'.' + QUOTENAME(t.name) + N';'
FROM
...
SET @sql = STUFF(@sql, 1, 10, N'');
...
Thanks for quick reply. My @sql declaration ended up in form:SELECT @sql += N'SELECT distinct' + QUOTENAME(c.name) + N' as ' + QUOTENAME(t.name) + N' FROM ' + QUOTENAME(s.name) + N'.' + QUOTENAME(t.name) + N';'
to be able to name data I found. Without that change, the results were with same title and thus useless for my purposes.
– mico
30 mins ago
add a comment |Â
up vote
3
down vote
accepted
Unless you are explicitly trying to write queries that work in multiple engines, I recommend sticking to the catalog views in the sys
schema rather than INFORMATION_SCHEMA
(see this and this). You'll need dynamic SQL for this anyway, and in other engines the syntax for this will vary greatly:
DECLARE @sql nvarchar(max) = N'';
SELECT @sql += N'SELECT ' + QUOTENAME(c.name) + N' FROM '
+ QUOTENAME(s.name) + N'.' + QUOTENAME(t.name) + N';'
FROM sys.schemas AS s
INNER JOIN sys.tables AS t
ON s.[schema_id] = t.[schema_id]
INNER JOIN sys.columns AS c
ON t.[object_id] = c.[object_id]
WHERE c.name = N'NNN';
EXEC sys.sp_executesql @sql;
If you want the column to be listed as the name of the table instead of the name of the column:
...
SELECT @sql += N'SELECT ' + QUOTENAME(t.name) + N' = '
+ QUOTENAME(s.name + N'.' + t.name) + N' FROM '
+ QUOTENAME(s.name) + N'.' + QUOTENAME(t.name) + N';'
FROM
...
And if you want them all in a single result set, you could add the table name as a column, and use a UNION
(and coerce all the columns into a compatible type, just in case):
...
SELECT @sql += N'UNION ALL SELECT [table] = '''
+ QUOTENAME(s.name + N'.' + t.name) + N''', '
+ N' value = CONVERT(nvarchar(4000),'
+ QUOTENAME(c.name) + N') FROM '
+ QUOTENAME(s.name) + N'.' + QUOTENAME(t.name) + N';'
FROM
...
SET @sql = STUFF(@sql, 1, 10, N'');
...
Thanks for quick reply. My @sql declaration ended up in form:SELECT @sql += N'SELECT distinct' + QUOTENAME(c.name) + N' as ' + QUOTENAME(t.name) + N' FROM ' + QUOTENAME(s.name) + N'.' + QUOTENAME(t.name) + N';'
to be able to name data I found. Without that change, the results were with same title and thus useless for my purposes.
– mico
30 mins ago
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Unless you are explicitly trying to write queries that work in multiple engines, I recommend sticking to the catalog views in the sys
schema rather than INFORMATION_SCHEMA
(see this and this). You'll need dynamic SQL for this anyway, and in other engines the syntax for this will vary greatly:
DECLARE @sql nvarchar(max) = N'';
SELECT @sql += N'SELECT ' + QUOTENAME(c.name) + N' FROM '
+ QUOTENAME(s.name) + N'.' + QUOTENAME(t.name) + N';'
FROM sys.schemas AS s
INNER JOIN sys.tables AS t
ON s.[schema_id] = t.[schema_id]
INNER JOIN sys.columns AS c
ON t.[object_id] = c.[object_id]
WHERE c.name = N'NNN';
EXEC sys.sp_executesql @sql;
If you want the column to be listed as the name of the table instead of the name of the column:
...
SELECT @sql += N'SELECT ' + QUOTENAME(t.name) + N' = '
+ QUOTENAME(s.name + N'.' + t.name) + N' FROM '
+ QUOTENAME(s.name) + N'.' + QUOTENAME(t.name) + N';'
FROM
...
And if you want them all in a single result set, you could add the table name as a column, and use a UNION
(and coerce all the columns into a compatible type, just in case):
...
SELECT @sql += N'UNION ALL SELECT [table] = '''
+ QUOTENAME(s.name + N'.' + t.name) + N''', '
+ N' value = CONVERT(nvarchar(4000),'
+ QUOTENAME(c.name) + N') FROM '
+ QUOTENAME(s.name) + N'.' + QUOTENAME(t.name) + N';'
FROM
...
SET @sql = STUFF(@sql, 1, 10, N'');
...
Unless you are explicitly trying to write queries that work in multiple engines, I recommend sticking to the catalog views in the sys
schema rather than INFORMATION_SCHEMA
(see this and this). You'll need dynamic SQL for this anyway, and in other engines the syntax for this will vary greatly:
DECLARE @sql nvarchar(max) = N'';
SELECT @sql += N'SELECT ' + QUOTENAME(c.name) + N' FROM '
+ QUOTENAME(s.name) + N'.' + QUOTENAME(t.name) + N';'
FROM sys.schemas AS s
INNER JOIN sys.tables AS t
ON s.[schema_id] = t.[schema_id]
INNER JOIN sys.columns AS c
ON t.[object_id] = c.[object_id]
WHERE c.name = N'NNN';
EXEC sys.sp_executesql @sql;
If you want the column to be listed as the name of the table instead of the name of the column:
...
SELECT @sql += N'SELECT ' + QUOTENAME(t.name) + N' = '
+ QUOTENAME(s.name + N'.' + t.name) + N' FROM '
+ QUOTENAME(s.name) + N'.' + QUOTENAME(t.name) + N';'
FROM
...
And if you want them all in a single result set, you could add the table name as a column, and use a UNION
(and coerce all the columns into a compatible type, just in case):
...
SELECT @sql += N'UNION ALL SELECT [table] = '''
+ QUOTENAME(s.name + N'.' + t.name) + N''', '
+ N' value = CONVERT(nvarchar(4000),'
+ QUOTENAME(c.name) + N') FROM '
+ QUOTENAME(s.name) + N'.' + QUOTENAME(t.name) + N';'
FROM
...
SET @sql = STUFF(@sql, 1, 10, N'');
...
edited 12 mins ago
answered 1 hour ago
Aaron Bertrand♦
146k19280468
146k19280468
Thanks for quick reply. My @sql declaration ended up in form:SELECT @sql += N'SELECT distinct' + QUOTENAME(c.name) + N' as ' + QUOTENAME(t.name) + N' FROM ' + QUOTENAME(s.name) + N'.' + QUOTENAME(t.name) + N';'
to be able to name data I found. Without that change, the results were with same title and thus useless for my purposes.
– mico
30 mins ago
add a comment |Â
Thanks for quick reply. My @sql declaration ended up in form:SELECT @sql += N'SELECT distinct' + QUOTENAME(c.name) + N' as ' + QUOTENAME(t.name) + N' FROM ' + QUOTENAME(s.name) + N'.' + QUOTENAME(t.name) + N';'
to be able to name data I found. Without that change, the results were with same title and thus useless for my purposes.
– mico
30 mins ago
Thanks for quick reply. My @sql declaration ended up in form:
SELECT @sql += N'SELECT distinct' + QUOTENAME(c.name) + N' as ' + QUOTENAME(t.name) + N' FROM ' + QUOTENAME(s.name) + N'.' + QUOTENAME(t.name) + N';'
to be able to name data I found. Without that change, the results were with same title and thus useless for my purposes.– mico
30 mins ago
Thanks for quick reply. My @sql declaration ended up in form:
SELECT @sql += N'SELECT distinct' + QUOTENAME(c.name) + N' as ' + QUOTENAME(t.name) + N' FROM ' + QUOTENAME(s.name) + N'.' + QUOTENAME(t.name) + N';'
to be able to name data I found. Without that change, the results were with same title and thus useless for my purposes.– mico
30 mins ago
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f219627%2fis-it-possible-to-concatenate-query-results-to-form-a-new-query%23new-answer', 'question_page');
);
Post as a guest
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
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
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
SET @sql='SELECT ' + <COLUMN_NAME> + ' FROM ' + <TABLE_SCHEMA> + '.' + <TABLE_NAME>; EXEC (@sql);
. Iterate by recordset (or concate in a query, then execute).– Akina
1 hour ago
@Akina Could you elaborate that as an answer, seems meaningful approach?
– mico
1 hour ago