【问题标题】:Concat alias of column names and variable inside the select选择内的列名和变量的连接别名
【发布时间】:2021-06-01 10:24:35
【问题描述】:

我想将我在选择中使用的别名与一个变量连接起来。比如:

DECLARE @var1 as Date
set @var1 = '1/06/2021'

select (column1 + column2) AS ('Values at date ' + cast(@var1 as nvarchar)) from MyTable

但没有动态选择,因为我们使用的是 Reporting Services 和 EntityFramework,它需要知道过程将返回的列数

【问题讨论】:

  • 如果您的列名是编码日期,则您的数据模型有问题。
  • 似乎列的名称应该是您的 SSRS tablix/matrix 中的表达式。
  • 是的。这是一个等式。我们希望该等式的列名具有该名称

标签: sql sql-server entity-framework stored-procedures reporting-services


【解决方案1】:

获取命令字符串并用命令“执行”它

DECLARE @var1 as Date
set @var1 = '1/06/2021'

declare @StrSelect nvarchar(4000)='Values at date '+CAST(@var1 as nvarchar)

exec('select  column1 + column2 as ['+@StrSelect+'] from MyTable')

【讨论】:

  • 嗨。谢谢你的帮助,但我不想那样。它是这样的:从 MyTable 中选择 column1 + column2 AS ('Values at date ' + cast(@var1 as nvarchar) ) 我想将变量与别名连接起来
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-26
  • 1970-01-01
  • 2012-03-23
  • 2015-08-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多