fetch data from multi tables with out relation ship between these tables
For every week I have excel sheet开发者_Python百科 converted to SQL database this excel sheet gave me 30 deference table with same structure and field definition but there is no relationship between these tables.
So I want to merge the data from these 30 table in one table I'm trying to write T-sql to do this but I fail by using INFORMATION_SCHEMA.Tables
or sys.tables
to get the table names and proceed to fetch data from these tables in one query.
To combine tables together with the exact same schema:
SELECT * FROM Table1
UNION
SELECT * FROM Table2
If your entire DB is made up of these tables you should be able to use the sp_msforeachtable
procedure.
CREATE TABLE #TempTable (Col1 INT, Col2 NVARCHAR(50))
sp_msforeachtable 'INSERT INTO #TempTable (Col1 , Col2) SELECT Col1 , Col2 FROM ?'
SELECT * FROM #TempTable
DROP TABLE #TempTable
精彩评论