Get a single row of data from all tables using ID
up vote
0
down vote
favorite
I need to get sample data from all the database tables, there are hundreds of tables and not all of them may be relevant.
Is there a way I can provide an id from the top level table, to select a row, and also select the corresponding row of data in the subtables (if data exists).
i.e. Given an id, select all the data for this id in all the tables, only return data if it exists.
e.g. Instead of doing:
Select * from main_table where id = 1
Select * from next_table where master_id = 1
Select * from another_table where master_id = 1
Select * from sub_table where next_table.id = 5
Select * from this_table where sub_table.id = 9
etc
sql sql-server
add a comment |
up vote
0
down vote
favorite
I need to get sample data from all the database tables, there are hundreds of tables and not all of them may be relevant.
Is there a way I can provide an id from the top level table, to select a row, and also select the corresponding row of data in the subtables (if data exists).
i.e. Given an id, select all the data for this id in all the tables, only return data if it exists.
e.g. Instead of doing:
Select * from main_table where id = 1
Select * from next_table where master_id = 1
Select * from another_table where master_id = 1
Select * from sub_table where next_table.id = 5
Select * from this_table where sub_table.id = 9
etc
sql sql-server
if it is only sample data..., TOP 1 from all tables is not sufficient? Or it must be corresponding data?
– PawelCz
13 hours ago
corresponding data for a given top level table ID - so I give the query an id for the main table and it returns all the corresponding data from each relevant table
– user3437721
13 hours ago
this would be easy if every table would have the same name for column, like master_id and this id would be the same for all tables, but what I can see you need to rely on FKs..., what is smelly
– PawelCz
13 hours ago
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I need to get sample data from all the database tables, there are hundreds of tables and not all of them may be relevant.
Is there a way I can provide an id from the top level table, to select a row, and also select the corresponding row of data in the subtables (if data exists).
i.e. Given an id, select all the data for this id in all the tables, only return data if it exists.
e.g. Instead of doing:
Select * from main_table where id = 1
Select * from next_table where master_id = 1
Select * from another_table where master_id = 1
Select * from sub_table where next_table.id = 5
Select * from this_table where sub_table.id = 9
etc
sql sql-server
I need to get sample data from all the database tables, there are hundreds of tables and not all of them may be relevant.
Is there a way I can provide an id from the top level table, to select a row, and also select the corresponding row of data in the subtables (if data exists).
i.e. Given an id, select all the data for this id in all the tables, only return data if it exists.
e.g. Instead of doing:
Select * from main_table where id = 1
Select * from next_table where master_id = 1
Select * from another_table where master_id = 1
Select * from sub_table where next_table.id = 5
Select * from this_table where sub_table.id = 9
etc
sql sql-server
sql sql-server
asked 13 hours ago
user3437721
86411331
86411331
if it is only sample data..., TOP 1 from all tables is not sufficient? Or it must be corresponding data?
– PawelCz
13 hours ago
corresponding data for a given top level table ID - so I give the query an id for the main table and it returns all the corresponding data from each relevant table
– user3437721
13 hours ago
this would be easy if every table would have the same name for column, like master_id and this id would be the same for all tables, but what I can see you need to rely on FKs..., what is smelly
– PawelCz
13 hours ago
add a comment |
if it is only sample data..., TOP 1 from all tables is not sufficient? Or it must be corresponding data?
– PawelCz
13 hours ago
corresponding data for a given top level table ID - so I give the query an id for the main table and it returns all the corresponding data from each relevant table
– user3437721
13 hours ago
this would be easy if every table would have the same name for column, like master_id and this id would be the same for all tables, but what I can see you need to rely on FKs..., what is smelly
– PawelCz
13 hours ago
if it is only sample data..., TOP 1 from all tables is not sufficient? Or it must be corresponding data?
– PawelCz
13 hours ago
if it is only sample data..., TOP 1 from all tables is not sufficient? Or it must be corresponding data?
– PawelCz
13 hours ago
corresponding data for a given top level table ID - so I give the query an id for the main table and it returns all the corresponding data from each relevant table
– user3437721
13 hours ago
corresponding data for a given top level table ID - so I give the query an id for the main table and it returns all the corresponding data from each relevant table
– user3437721
13 hours ago
this would be easy if every table would have the same name for column, like master_id and this id would be the same for all tables, but what I can see you need to rely on FKs..., what is smelly
– PawelCz
13 hours ago
this would be easy if every table would have the same name for column, like master_id and this id would be the same for all tables, but what I can see you need to rely on FKs..., what is smelly
– PawelCz
13 hours ago
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
There are 2 options, I can see.
Option #1: multiple joins
You will have to join the main_table with other required tables to select the top 1 from each table based on the reference keys.
declare @main_table_id int
set @main_table_id = 1
Select top 1 * from main_table where id = @main_table_id
Select top 1 nt.*
from main_table mt
inner join next_table nt on mt.id = nt.master_id
where mt.id = @main_table_id
Select top 1 at.*
from main_table mt
inner join another_table at on mt.id = at.master_id
where mt.id = @main_table_id
Select top 1 st.*
from main_table mt
inner join next_table nt on mt.id = nt.master_id
inner join sub_table st on st.next_table_id = nt.id
where mt.id = @main_table_id
Select top 1 st.*
from main_table mt
inner join next_table nt on mt.id = nt.master_id
inner join sub_table st on st.next_table_id = nt.id
inner join this_table tt on tt.sub_table_id = st.id
where mt.id = @main_table_id
Option #2: Stored procedure style
you will be selecting the keys and storing in a variable. This could end up in having a lot of SQL variable.
declare @main_table_id int
set @main_table_id = 1
Select top 1 * from main_table where id = @main_table_id
declare @next_table_id int
select top 1 @next_table_id = id from next_table where master_id = @main_table_id
Select top 1 * from next_table where id = @next_table_id
Select top 1 * from another_table where master_id = @main_table_id
declare @sub_table_id int
select top 1 @sub_table_id = id from sub_table where next_table_id = @next_table_id
Select top 1 * from sub_table where id = @sub_table_id
Select top 1 * from this_table where sub_table_id = @sub_table_id
Problem is, since there is dozens of tables - I have no idea which ones are relevant and which ones I should join, so I am looking an automatic way to do it in SQL server or by sql
– user3437721
13 hours ago
There could be lot of complexities including when there are more than one parent columns references either from same table or multiple tables.You might want to look at the table definitions with columns and find the references, given that all the reference columns are named in the expected way.
– Khathiravan Raj
12 hours ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
There are 2 options, I can see.
Option #1: multiple joins
You will have to join the main_table with other required tables to select the top 1 from each table based on the reference keys.
declare @main_table_id int
set @main_table_id = 1
Select top 1 * from main_table where id = @main_table_id
Select top 1 nt.*
from main_table mt
inner join next_table nt on mt.id = nt.master_id
where mt.id = @main_table_id
Select top 1 at.*
from main_table mt
inner join another_table at on mt.id = at.master_id
where mt.id = @main_table_id
Select top 1 st.*
from main_table mt
inner join next_table nt on mt.id = nt.master_id
inner join sub_table st on st.next_table_id = nt.id
where mt.id = @main_table_id
Select top 1 st.*
from main_table mt
inner join next_table nt on mt.id = nt.master_id
inner join sub_table st on st.next_table_id = nt.id
inner join this_table tt on tt.sub_table_id = st.id
where mt.id = @main_table_id
Option #2: Stored procedure style
you will be selecting the keys and storing in a variable. This could end up in having a lot of SQL variable.
declare @main_table_id int
set @main_table_id = 1
Select top 1 * from main_table where id = @main_table_id
declare @next_table_id int
select top 1 @next_table_id = id from next_table where master_id = @main_table_id
Select top 1 * from next_table where id = @next_table_id
Select top 1 * from another_table where master_id = @main_table_id
declare @sub_table_id int
select top 1 @sub_table_id = id from sub_table where next_table_id = @next_table_id
Select top 1 * from sub_table where id = @sub_table_id
Select top 1 * from this_table where sub_table_id = @sub_table_id
Problem is, since there is dozens of tables - I have no idea which ones are relevant and which ones I should join, so I am looking an automatic way to do it in SQL server or by sql
– user3437721
13 hours ago
There could be lot of complexities including when there are more than one parent columns references either from same table or multiple tables.You might want to look at the table definitions with columns and find the references, given that all the reference columns are named in the expected way.
– Khathiravan Raj
12 hours ago
add a comment |
up vote
0
down vote
There are 2 options, I can see.
Option #1: multiple joins
You will have to join the main_table with other required tables to select the top 1 from each table based on the reference keys.
declare @main_table_id int
set @main_table_id = 1
Select top 1 * from main_table where id = @main_table_id
Select top 1 nt.*
from main_table mt
inner join next_table nt on mt.id = nt.master_id
where mt.id = @main_table_id
Select top 1 at.*
from main_table mt
inner join another_table at on mt.id = at.master_id
where mt.id = @main_table_id
Select top 1 st.*
from main_table mt
inner join next_table nt on mt.id = nt.master_id
inner join sub_table st on st.next_table_id = nt.id
where mt.id = @main_table_id
Select top 1 st.*
from main_table mt
inner join next_table nt on mt.id = nt.master_id
inner join sub_table st on st.next_table_id = nt.id
inner join this_table tt on tt.sub_table_id = st.id
where mt.id = @main_table_id
Option #2: Stored procedure style
you will be selecting the keys and storing in a variable. This could end up in having a lot of SQL variable.
declare @main_table_id int
set @main_table_id = 1
Select top 1 * from main_table where id = @main_table_id
declare @next_table_id int
select top 1 @next_table_id = id from next_table where master_id = @main_table_id
Select top 1 * from next_table where id = @next_table_id
Select top 1 * from another_table where master_id = @main_table_id
declare @sub_table_id int
select top 1 @sub_table_id = id from sub_table where next_table_id = @next_table_id
Select top 1 * from sub_table where id = @sub_table_id
Select top 1 * from this_table where sub_table_id = @sub_table_id
Problem is, since there is dozens of tables - I have no idea which ones are relevant and which ones I should join, so I am looking an automatic way to do it in SQL server or by sql
– user3437721
13 hours ago
There could be lot of complexities including when there are more than one parent columns references either from same table or multiple tables.You might want to look at the table definitions with columns and find the references, given that all the reference columns are named in the expected way.
– Khathiravan Raj
12 hours ago
add a comment |
up vote
0
down vote
up vote
0
down vote
There are 2 options, I can see.
Option #1: multiple joins
You will have to join the main_table with other required tables to select the top 1 from each table based on the reference keys.
declare @main_table_id int
set @main_table_id = 1
Select top 1 * from main_table where id = @main_table_id
Select top 1 nt.*
from main_table mt
inner join next_table nt on mt.id = nt.master_id
where mt.id = @main_table_id
Select top 1 at.*
from main_table mt
inner join another_table at on mt.id = at.master_id
where mt.id = @main_table_id
Select top 1 st.*
from main_table mt
inner join next_table nt on mt.id = nt.master_id
inner join sub_table st on st.next_table_id = nt.id
where mt.id = @main_table_id
Select top 1 st.*
from main_table mt
inner join next_table nt on mt.id = nt.master_id
inner join sub_table st on st.next_table_id = nt.id
inner join this_table tt on tt.sub_table_id = st.id
where mt.id = @main_table_id
Option #2: Stored procedure style
you will be selecting the keys and storing in a variable. This could end up in having a lot of SQL variable.
declare @main_table_id int
set @main_table_id = 1
Select top 1 * from main_table where id = @main_table_id
declare @next_table_id int
select top 1 @next_table_id = id from next_table where master_id = @main_table_id
Select top 1 * from next_table where id = @next_table_id
Select top 1 * from another_table where master_id = @main_table_id
declare @sub_table_id int
select top 1 @sub_table_id = id from sub_table where next_table_id = @next_table_id
Select top 1 * from sub_table where id = @sub_table_id
Select top 1 * from this_table where sub_table_id = @sub_table_id
There are 2 options, I can see.
Option #1: multiple joins
You will have to join the main_table with other required tables to select the top 1 from each table based on the reference keys.
declare @main_table_id int
set @main_table_id = 1
Select top 1 * from main_table where id = @main_table_id
Select top 1 nt.*
from main_table mt
inner join next_table nt on mt.id = nt.master_id
where mt.id = @main_table_id
Select top 1 at.*
from main_table mt
inner join another_table at on mt.id = at.master_id
where mt.id = @main_table_id
Select top 1 st.*
from main_table mt
inner join next_table nt on mt.id = nt.master_id
inner join sub_table st on st.next_table_id = nt.id
where mt.id = @main_table_id
Select top 1 st.*
from main_table mt
inner join next_table nt on mt.id = nt.master_id
inner join sub_table st on st.next_table_id = nt.id
inner join this_table tt on tt.sub_table_id = st.id
where mt.id = @main_table_id
Option #2: Stored procedure style
you will be selecting the keys and storing in a variable. This could end up in having a lot of SQL variable.
declare @main_table_id int
set @main_table_id = 1
Select top 1 * from main_table where id = @main_table_id
declare @next_table_id int
select top 1 @next_table_id = id from next_table where master_id = @main_table_id
Select top 1 * from next_table where id = @next_table_id
Select top 1 * from another_table where master_id = @main_table_id
declare @sub_table_id int
select top 1 @sub_table_id = id from sub_table where next_table_id = @next_table_id
Select top 1 * from sub_table where id = @sub_table_id
Select top 1 * from this_table where sub_table_id = @sub_table_id
answered 13 hours ago
Khathiravan Raj
11
11
Problem is, since there is dozens of tables - I have no idea which ones are relevant and which ones I should join, so I am looking an automatic way to do it in SQL server or by sql
– user3437721
13 hours ago
There could be lot of complexities including when there are more than one parent columns references either from same table or multiple tables.You might want to look at the table definitions with columns and find the references, given that all the reference columns are named in the expected way.
– Khathiravan Raj
12 hours ago
add a comment |
Problem is, since there is dozens of tables - I have no idea which ones are relevant and which ones I should join, so I am looking an automatic way to do it in SQL server or by sql
– user3437721
13 hours ago
There could be lot of complexities including when there are more than one parent columns references either from same table or multiple tables.You might want to look at the table definitions with columns and find the references, given that all the reference columns are named in the expected way.
– Khathiravan Raj
12 hours ago
Problem is, since there is dozens of tables - I have no idea which ones are relevant and which ones I should join, so I am looking an automatic way to do it in SQL server or by sql
– user3437721
13 hours ago
Problem is, since there is dozens of tables - I have no idea which ones are relevant and which ones I should join, so I am looking an automatic way to do it in SQL server or by sql
– user3437721
13 hours ago
There could be lot of complexities including when there are more than one parent columns references either from same table or multiple tables.You might want to look at the table definitions with columns and find the references, given that all the reference columns are named in the expected way.
– Khathiravan Raj
12 hours ago
There could be lot of complexities including when there are more than one parent columns references either from same table or multiple tables.You might want to look at the table definitions with columns and find the references, given that all the reference columns are named in the expected way.
– Khathiravan Raj
12 hours 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%2fstackoverflow.com%2fquestions%2f53224048%2fget-a-single-row-of-data-from-all-tables-using-id%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
if it is only sample data..., TOP 1 from all tables is not sufficient? Or it must be corresponding data?
– PawelCz
13 hours ago
corresponding data for a given top level table ID - so I give the query an id for the main table and it returns all the corresponding data from each relevant table
– user3437721
13 hours ago
this would be easy if every table would have the same name for column, like master_id and this id would be the same for all tables, but what I can see you need to rely on FKs..., what is smelly
– PawelCz
13 hours ago