我试图得到result5 =1,如果运营现金流的平均值是<=0,或者前3名的平均运营现金流是<=0。错误消息在关键字'if‘附近显示不正确的语法。
begin
declare @OperatingCashflow5 int
declare @OperatingCashflow3 int
declare @result5 int
select @OperatingCashflow5 = AVG(hpd.TotalCashFromOperations)
from HistoricalFinPerformanceDataStagingGlobal hpd
where hpd.companyid = @companyid
select @OperatingCashflow3 = AVG(hpd.TotalCashFromOperations)
from (
select top (3) hpd.TotalCashFromOperations
from HistoricalFinPerformanceDataStagingGlobal hpd
where hpd.companyid = @companyid
)
if @OperatingCashflow5 <= 0 OR @OperatingCashflow3 <= 0
begin
select @result5 = 1
end
else
begin
select @result5 = 0
end
end发布于 2018-01-29 16:05:12
当您花时间格式化代码时,发现问题要容易得多。
在这种情况下,您有一个嵌套SELECT语句,嵌套select必须有一个别名。您所要做的就是在结束括号后添加一个字母(任何字母):
begin
declare @OperatingCashflow5 int
declare @OperatingCashflow3 int
declare @result5 int
select @OperatingCashflow5 = AVG(hpd.TotalCashFromOperations)
from HistoricalFinPerformanceDataStagingGlobal hpd
where hpd.companyid = @companyid
select @OperatingCashflow3 = AVG(hpd.TotalCashFromOperations)
from (
select top (3) hpd.TotalCashFromOperations
from HistoricalFinPerformanceDataStagingGlobal hpd
where hpd.companyid = @companyid
) hpd -- <<<< Need a name here
if @OperatingCashflow5 <= 0 OR @OperatingCashflow3 <= 0
begin
select @result5 = 1
end
else
begin
select @result5 = 0
end
end我怀疑这个特定的select查询还有其他问题。
https://stackoverflow.com/questions/48505243
复制相似问题