Click here to Skip to main content
15,889,858 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I have two table valued functions as T1 and T2
and executing this I get T1 data as

ParameterID Parametername ParameterDoc
1 French French Data

2 German German Data

3 Polish Polish Data

4 English English Data


and T2 as

CountryCode

Docxrelation

63

France

67

Germany
75

Poland

34

United States

65 India
66 China


and my expected result would be like
ParameterIDParameternameParameterDocCountryCode

Docx relation

1FrenchFrench Data63

France

2GermanGerman Data67

Germany
3PolishPolish Data

75

Poland

4EnglishEnglish Data34

United States

NULLNULLNULL65India
NULLNULLNULL66China


I tried to execute
Select * from dbo.T1, dbo.T2

but it does does gives me the expected result and gives more data than the expected result sets . there is no relation between their Id's/ countrycode that's why I'm getting issue to add column in the query result set.

Any suggestion will be highly appreciated.
Posted
Comments
Sreekanth Mothukuru 8-Oct-15 5:58am    
You said "more data than the expected result set" is that because you are doing Select * instead of selective columns?
Abhishek Pant 8-Oct-15 6:38am    
it becomes like data with cartesian product and I need all the columns as columns may vary in different conditions and I'm not known to column names as it is entered by a user from asp.net page
Sreekanth Mothukuru 8-Oct-15 8:03am    
There is a concept called "Outer Apply" in Sql Server where you can join two tables horizontally. Try it in Google. It will fix your req.

1 solution

The above query would simply give a Cartesian product of the 2 tables. That means it would print 20 rows(4*5). To get the expected data, you should have a column matching in both the tables which isn't the case here. Try adding another table which contains the following information:

Table T3
Country Language
Germany German
France French
Poland Polish
United States English
India Hindi
China Mandarin

The new table would contain the country name and language columns. Now a mapping would be there to join the 3 tables and get the expected result.

Select T1.*,T2.* from T1
INNER JOIN T3 ON T1.Parametername= T3.Language
RIGHT OUTER JOIN T2 ON T2.Docxrelation = T3.Language

This would fetch the desired output.
 
Share this answer
 
Comments
Abhishek Pant 8-Oct-15 6:06am    
As per as my current condition the data is saved from a dynamic gridview have columns generated at runtime as according to parameterid for a table that I used in Tabular valued function. and as per as my requirement I have to show the expected result set in SSRS result set with the data I getting from that. So it would not be creating a relation if a user is entering rows and its columns dynamically from a gridview
[no name] 8-Oct-15 6:10am    
Total no of Column and name will be same or it is different in different scenario?
Abhishek Pant 8-Oct-15 6:36am    
yes it may vary in different scenario but at least one column will there as general prohibition.
Ankur_Garg 8-Oct-15 6:15am    
There has to be a relation between both the tables to get the desired output. From where are the rows inserting into table T1 & T2?

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