Click here to Skip to main content
15,903,831 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want a query according case when Table2.Col2=1 then select X column name from table3 inside col3 of table 1 and we also have condition to select column name from table3,table4,table5.....

Please suggest . thanks for you support.

C#
Table1			
Col1	Col2	Col3
1	     12	
2	     20	
3	     30	





C#
Table2		
Col1	Col2	Col3
1	     X	    Table3
2	     Y 	    Table4
3	     Z	    Table4


What I have tried:

I have tried this query but it return only col2 name from table2 but i need select X column from table3

Select MC.Min,MC.Max,
MC.DataType,
MC.ScriptFieldId,
t2.Col1,
CASE WHEN Isnull(MC.ScriptFieldId,0) <> 0 THEN
CASE WHEN t2.Col1 ='1' THEN (SELECT t2.Col2 FROM Table3 CTP where TravellerId =@TravelerId) -- select X column from table3
WHEN t2.Col1 ='2' THEN (SELECT t2.Col2 FROM Table4 CCP1 where CCP1.CompnayId =CCP.CompnayId) -- select Y column from table4
ELSE SFM.ColumnName END ELSE '' END AS 'MappingValue'
FROM ManageScript MC
LEFT OUTER JOIN Table2 t2 on t2.Col1 =MC.Col1
WHERE MC.ClientId =@ClientId and MC.CompanyId =@CompanyId
Posted
Updated 28-Aug-16 1:57am
v3
Comments
Maciej Los 28-Aug-16 7:40am    
There's no relationship between tables!
suneel kumar gupta 28-Aug-16 7:52am    
sorry I have forget to mention it Col1 in table1 as reference in table 2 as col1

1 solution

First of all, please read my comment to the question.
Secondly, please, read this: Visual Representation of SQL Joins[^]
Retrieving Data from Multiple Tables: Selecting Data from More Than One Table by Using Joins[^]
 
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