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










share|improve this question





















  • 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














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










share|improve this question





















  • 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












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










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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
















  • 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












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





share|improve this answer




















  • 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











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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






























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





share|improve this answer




















  • 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















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





share|improve this answer




















  • 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













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





share|improve this answer












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






share|improve this answer












share|improve this answer



share|improve this answer










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

















  • 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


















 

draft saved


draft discarded















































 


draft saved


draft discarded














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














































































Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo