我试图从依赖于某个字段的表中获取结果,并根据其中一个字段填充来自不同字段的变量。这就是我所拥有的
DECLARE @MyText Text;
SELECT
bu4contart,
CASE WHEN bu4contart IN('40HR', '40RF') THEN
'Temp: ' + bu4setpoint
ELSE
'Oversize: ' + bu4ohtop
END AS @MyText
FROM MyTable
答案 0 :(得分:0)
您的问题是您需要来自(可能)行集的标量值。这可能适用于标量情况。
DECLARE @MyText Text =
(SELECT Top 1
CASE WHEN bu4contart IN('40HR', '40RF') THEN
'Temp: ' + bu4setpoint
ELSE
'Oversize: ' + bu4ohtop
END AS MyText
FROM MyTable
Order By FieldThatOrdersForTop1FromMultipleRowsIfNecessary
)
答案 1 :(得分:0)
也许这个?
DECLARE @MyText varchar(max);
DECLARE @sql varchar(max);
SET @MyText = 'ahahahha';
SET @sql='SELECT
bu4contart,
CASE WHEN bu4contart IN(''40HR'', ''40RF'') THEN
''Temp: '' + bu4setpoint
ELSE
''Oversize: '' + bu4ohtop
END AS ' + @MyText
+ ' FROM MyTable';
EXECUTE (@sql)