Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
How to retrieve all record from every table (Ex: table1, table2, table3, ... tableN) where id = 1 from single database (EX: database1) in SQL Server 2008 R2

Suppose,I have 1 database and in that have infinite tables (EX. table1,table2, ....,tableN). So is this possible to get all the record from entire database where id=1 on each table.I thing it is possible with SQL information_schema.table or information_schema.column but i don't know how to use this.

Any help appreciated

Thanks in Advanced

What I have tried:

I have try using information_schema.table and information_schema.column
Posted
Updated 27-Dec-16 22:04pm
v2
Comments
Maciej Los 28-Dec-16 2:15am    
Yes, it's possible!
Salman126 28-Dec-16 3:43am    
can you please tell how to do?
Afzaal Ahmad Zeeshan 28-Dec-16 3:32am    
You are aware of the fact, that one query can return one table (with multiple tables JOINed to it)?

1 solution

It's possible - but the query would be different each time a table is added or removed, and it's a very unusual thing to try and do - it almost certainly means that your whole DB design is wrong: SELECT commands are designed to return information from one base table, with addition information from other related tabled which share common information (normally via a FOREIGN KEY relationship).
Trying to return unrelated info from a variable number of tables is not really the same thing.

But if the ID is a common accessor, then you'd need something like:
C#
SELECT a.*, b.*, c.*, d.*, ...
FROM Table1 a
JOIN Table2 b ON a.ID = b.ID
JOIN Table3 c ON a.ID = c.ID
JOIN Table4 d ON a.ID = d.ID
...
WHERE a.ID = 1

But as I say - if that's the case, it's normally your DB that is badly designed.
 
Share this answer
 
Comments
Maciej Los 28-Dec-16 4:35am    
Seems that OP wants to get data from all tables based on information_schema table. I don't think so that we can join that data, due to data type of each ID.
OriginalGriff 28-Dec-16 4:52am    
Which implies that his DB design is *really* bad! :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900