如何在SELECT语句中添加另一个表的列名(不提及列名) [英] How do I add column names of an another table (without mentioning column names), in a SELECT statement
问题描述
我想将另一个表的列放到SELECT语句中,而不直接提到这些列。
我需要的如下所示。
'选择T1.column1,T1.column2,(表2的列)从TABLE-1作为T1,TABLE-2作为T2'
如何管理 COLUMNS OF TABLE 2 部分。请建议。
我尝试了什么:
DECLARE @output nvarchar(max)
SELECT @output = COALESCE(@ output +',','')+ name FROM sys.columns WHERE OBJECT_ID = OBJECT_ID(N'TABLE-2')
'从TABLE-1中选择T1.column1,T1.column2,(SELECT @output)为T1,将TABLE-2选为T2'
//但是@output只是结果。不作为列。 :)
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. :)
推荐答案
尝试:
Try:
SELECT T1.column1, T1.column2, T2.* FROM TABLE-1 AS T1, TABLE-2 AS T2
但是......如果你有引用otehr表的列,你可能想要使用JOIN。
But...you probably want to use a JOIN if you have columns which refer to the otehr table.
这篇关于如何在SELECT语句中添加另一个表的列名(不提及列名)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!