Click here to Skip to main content
15,917,328 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hallo everybody,

I am dynamically filling out two tables in my stored procedure and wished to have extract out the fields those I want from each table which will become a table as a result set.

Structurally the stored procedure looks like the following pseudocode:

Declare Table_1 Table (F1, ...)
Insert InTo Table_1 Select * From X.Table

Declare Table_2 Table (F1, ...)
Insert InTo Table_2 Select * From Y.Table

Select * From Table_1, Table_2


Now, the big problem here is that the select statement cannot fetch out fields those are found in the second table even if there is a valid value in it this happens only when there is no value in the first table. In other words, if the first table has value then the select statement returns a result set otherwise if the first table doesn't have value but if the second table has then the select statement is not returning a result set.

By the way, I am using SQL2005 server.

If I am not clear, please let me know.

Thanks
Posted
Updated 16-Oct-11 3:35am
v2
Comments
Amir Mahfoozi 16-Oct-11 8:50am    
Does the "AND" keyword really work in the last line ? :o
"LEFT OUTER JOIN" or "," are imaginable but AND is very strange in there !
Yonathan1111 16-Oct-11 9:34am    
Hi Amir,

Not actually, it doesn't work. I just preferred not to use words like Join, or others. However, you are right I will correct it.

Thanks
Amir Mahfoozi 18-Oct-11 13:01pm    
Hi Yonathan,
If the two tables have relation with each other you should join them on their related field(inner join)
If you want to see all rows in first table even if there were not related rows in second table(or even if its completely empty) you should do a LEFT OUTER JOIN.
If you want to see all rows in second table even if there were not related rows in first table(or even if its completely empty) you should do a RIGHT OUTER JOIN.

And in case there was no relation between two tables do a cartesian product between them and put a condition to filter out unwanted ones. (the "," between two tables causes a cartesian product)
Yonathan1111 19-Oct-11 3:16am    
Hi Amir,

I have already tested in those cases you mentioned before my post. If you are suggesting these cases, that means I was doing correctly. Now, this is showing me that there is a problem at my design. As you can see at my post the pseudocode of the stored procedure creates two tables, actually these two tables are built from same table in this case X and Y tables, but both are same tables. My wish is to create Table_1 and Table_2 get some fields from each table and put them on a table which can accommodate those fields. Now, you can help me in designing my SP.

I thank you for your comment.
Amir Mahfoozi 19-Oct-11 3:51am    
With a very high probability you don't need to make two tables because they are from the same table,
So I think that you can do it with a single complicated SQL.

If the table column structures are same (or even similar), you can do a UNION between the two tables and you should be able to return all the results together.

If the table structures are drastically different (or you do not want to do a UNION), you can create a temporary table and insert the values of both the first and the second tables into it.

The select and return all the values from this temporary table.
 
Share this answer
 
Comments
Yonathan1111 16-Oct-11 9:24am    
Hi Abhinav,
The UNION operation is not suitable with my other stuffs, but I liked your second suggestion even-though I didn't know how to do it.

Thanks
You could use a FULL JOIN[^] to also get the records from the second table when no linked record exists with the first.
 
Share this answer
 

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