如何设置列别名作为SQL查询的结果? [英] How to set a column alias as the result of a SQL query?
本文介绍了如何设置列别名作为SQL查询的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用SQL查询的结果来设置列别名。请参见下面的脚本以及脚本的结果,我需要将其用作列别名。
I need to use result of a SQL query to set column aliases. Please see below script and the result of the script I need to use it as column aliases.
select
convert(varchar,DATEADD(month, -12, dateadd(d,-day(convert(date,dateadd(d,-(day(getdate())),getdate()))),convert(date,dateadd(d,+1-(day(getdate())),getdate())))),107),
convert(varchar,convert(date,dateadd(d,-day(convert(date,dateadd(d,-(day(getdate())),getdate()))),convert(date,dateadd(d,+1-(day(getdate())),getdate())))),107)
我需要尽快回答我的问题。
I need the answer for my question as soon as possible.
推荐答案
以下链接描述了两种解决方案:基于变量的列别名
Two solutions are described in the following link: Column alias based on variable
第一个解决方案:
- 在变量中设置别名
- 将查询定义为包含对变量的引用的nvarchar。
-
执行查询u唱歌sp_executesql
- Set the alias in a variable
- Define the query as a nvarchar containing a reference to the variable.
Execute the query using sp_executesql
SET @column_alias = 'new_title'
SET @sql = 'SELECT keycol, datacol AS ' + @column_alias + ' FROM Foo'
EXEC sp_executesql @sql
第二个解决方案:在执行查询后重命名该列
Second solution: Rename the column after the execution of the query
INSERT INTO Results
SELECT keycol, datacol
FROM Foo
EXEC sp_rename 'Results.datacol', @column_alias, 'COLUMN'
这篇关于如何设置列别名作为SQL查询的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文