February 18, 2014 at 11:01 am
Say I have 100 schemas all with the table "orders" is there an easy way to write one query to query them all?
cust1.orders
cust2.orders
cust3.orders
cust4.orders
etc...
February 18, 2014 at 11:10 am
jiayi345 (2/18/2014)
Say I have 100 schemas all with the table "orders" is there an easy way to write one query to query them all?cust1.orders
cust2.orders
cust3.orders
cust4.orders
etc...
Yes... write a spot of dynamic SQL to create a UNION ALL view and then query the view.
--Jeff Moden
Change is inevitable... Change for the better is not.
February 18, 2014 at 11:12 am
use the metadata to build a query;
probably featuring a UNION ALL if you want them in a single resutl set
SELECT
'SELECT ' + schema_name(schema_id)+ ' AS SchemaName,* FROM '
+ quotename(schema_name(schema_id)) + '.'
+ quotename(name) + ' UNION ALL ;' AS cmd
FROM sys.tables
WHERE name = 'Orders'
Lowell
February 18, 2014 at 11:14 am
Lowell (2/18/2014)
use the metadata to build a query;probably featuring a UNION ALL if you want them in a single resutl set
SELECT
'SELECT ' + schema_name(schema_id)+ ' AS SchemaName,* FROM '
+ quotename(schema_name(schema_id)) + '.'
+ quotename(name) + ' UNION ALL ;' AS cmd
FROM sys.tables
WHERE name = 'Orders'
This is how I'm currently doing it. I didn't know if there was a better way.
February 18, 2014 at 11:39 am
You could also create a view with all the schemas.
I can't imagine why would you have all those schemas.
February 18, 2014 at 12:54 pm
I'm not sure what the original intent was as I didn't create it. Thanks everyone for the suggestions.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply