我在SQL Server 2012中有一个存储过程,它由一个控制台应用程序(使用MS Visual Studio 2013的VB)通过传入2个参数来执行。存储过程的代码如下:
ALTER procedure [dbo].[OptimiserReads_getMaxValue]
@ReadDate datetime,
@tagname nvarchar(50)
AS
Begin
declare @first float
declare @second float
set @first = '265'
set @second = (select value
from RawHistorianData
where interval = @readdate and tagname = @tagname)
Select
CASE when @first > @second then @first else @second end
end
基本上,它应该导致@first
或@second
的结果。如果我在SQL Server Management Studio中执行该过程,我将获得所需的结果。
在控制台应用程序中,由于CASE
语句,我不知道如何命名结果。如何在SQL中为结果命名,例如
Select CASE (when @first > @second then @first else @second) as value
答案 0 :(得分:3)
Select CASE when @first > @second then @first else @second end as columnname
答案 1 :(得分:3)
Select CASE when @first > @second then @first else @second end as value
答案 2 :(得分:2)
就像您在问题中提到的那样,您需要做的就是为您的案例陈述添加别名。
格式如下。
SELECT CASE WHEN @first > @second THEN @first
ELSE @second
END AS BiggerValue
别名可用于列,表,联接,子查询等。
格式始终为'SomeObject' as 'Alias'
例如:
SELECT t.* from MyTable as t
SELECT FirstName as FN from MyTable
SELECT * FROM (SELECT Name FROM MyTable) as SubQuery