只从sql查询中选择列名

时间:2020-12-18 23:48:28

As an example I have this query here

作为一个例子,我在这里有这个查询

SELECT DISTINCT convert(varchar(10), PickupDate ,105) AS [Pickup dates]
FROM info.dbo.A_Query_Detail AS D
INNER JOIN info.dbo.A_Query_Header AS ACQ on ACQ.ID = D.Header_ID
WHERE D.HeaderID = @HeaderID 
AND ACQ.PriceType = 'Pickup'
GROUP BY 
Adrid, convert(varchar(10), PickupDate ,105) 

What I want to get here is only the column name without it conflicts with the above query. So my result would be something like: "Column_Date" or Pickup dates as stated above. I have red something about sys.tables but I can't seem to make it work with the above code.

我想在这里得到的只是没有与上面查询冲突的列名。因此,我的结果应该是类似于上面所述的“Column_Date”或拾取日期。我有些关于系统的东西。表,但我似乎不能使它与上面的代码一起工作。

2 个解决方案

#1


3  

If you want a list of columns, sometimes I use a temporary view:

如果你想要列的列表,我有时会使用临时视图:

create view _MyView as <your query here>

Then you can do:

然后你可以做:

select *
from information_schema.columns
where table_name = '_MyView'

You can get the column names and types by doing this.

这样做可以获得列名和类型。

Then you can do:

然后你可以做:

drop view _MyView

(And, of course, the view name should not conflict with anything else.)

(当然,视图名不应该与其他任何东西相冲突。)

#2


1  

try this,

试试这个,

SELECT 'Pickup dates' AS [ColumnName];

this is like @Gordon Linoff's answer but doesn't carry the overhead of creating a view.

这类似于@Gordon Linoff的答案,但不包含创建视图的开销。

#1


3  

If you want a list of columns, sometimes I use a temporary view:

如果你想要列的列表,我有时会使用临时视图:

create view _MyView as <your query here>

Then you can do:

然后你可以做:

select *
from information_schema.columns
where table_name = '_MyView'

You can get the column names and types by doing this.

这样做可以获得列名和类型。

Then you can do:

然后你可以做:

drop view _MyView

(And, of course, the view name should not conflict with anything else.)

(当然,视图名不应该与其他任何东西相冲突。)

#2


1  

try this,

试试这个,

SELECT 'Pickup dates' AS [ColumnName];

this is like @Gordon Linoff's answer but doesn't carry the overhead of creating a view.

这类似于@Gordon Linoff的答案,但不包含创建视图的开销。