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

I would like to get the columns of another table into a SELECT statement, without mentioning those columns directly.

QUERY that I need like as below.

'Select T1.column1, T1.column2, (COLUMNS OF TABLE 2) from TABLE-1 as T1, TABLE-2 as T2'

how to manage the COLUMNS OF TABLE 2 part. Please suggest.

What I have tried:

DECLARE @output nvarchar(max)
SELECT @output = COALESCE(@output+', ', '')+name FROM sys.columns WHERE OBJECT_ID = OBJECT_ID(N'TABLE-2')
'Select T1.column1, T1.column2,(SELECT @output) from TABLE-1 as T1, TABLE-2 as T2'

// But @output is result only. Not as the Columns. :)
Posted
Updated 24-Apr-16 21:20pm
Comments
Karthik_Mahalingam 25-Apr-16 3:29am    
select t1.*, t2.* from Table1 t1 , Table2 t2

1 solution

Try:
SQL
SELECT T1.column1, T1.column2, T2.* FROM TABLE-1 AS T1, TABLE-2 AS T2
But...you probably want to use a JOIN if you have columns which refer to the otehr table.
 
Share this answer
 
Comments
NPSSR 25-Apr-16 3:35am    
It's includes some repeated columns, Transaction ID column, etc. Is there a way to restrict those columns? Or I have to remove those columns in my Business class?
OriginalGriff 25-Apr-16 3:43am    
It repeats them because it doesn't know what else to do with them, and it's trying very hard to give you what you asked for.
As I said, you probably want to use a JOIN - but without a lot more info about your tables and the relationship between them I can't even give you an example!
NPSSR 25-Apr-16 3:57am    
Sure Griff.. Thanks a lot for your suggestions. And, i will move ahead. Thanks Again!
OriginalGriff 25-Apr-16 3:59am    
You're welcome!

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