LAG 不是公认的内置函数名称

2021-09-25 00:00:00 window-functions sql-server

我有一个创建以下存储过程的脚本:

I have a script that creates the following stored procedure :

CREATE PROCEDURE [dbo].[GetDurationFree] 
    @EquipmentName varchar(50)
AS
    UPDATE dbo.EquipmentMessages
    SET UnlockDuration = (SELECT DATEDIFF (SECOND, 
                (SELECT TOP 1 LAG(TimeUnlock) OVER (ORDER BY TimeUnlock) TimeUnlock
                    FROM dbo.EquipmentMessages
                    WHERE EquipmentName = @EquipmentName
                    ORDER BY TimeLock DESC), 
                (SELECT TOP 1 TimeLock FROM dbo.EquipmentMessages
                    WHERE EquipmentName = @EquipmentName
                    ORDER BY TimeLock DESC)))
    WHERE TimeLock = (SELECT MAX(TimeLock) FROM dbo.EquipmentMessages
                    WHERE EquipmentName = @EquipmentName);

唯一的问题是它使用了延迟,当我尝试执行它时出现以下错误:

The only problem is that It uses a Lag, when I try to execute it I get the following errors :

消息 195,级别 15,状态 10,过程 GetDurationFree,第 6 行
LAG"不是可识别的内置函数名称.

Msg 195, Level 15, State 10, Procedure GetDurationFree, Line 6
'LAG' is not a recognized built-in function name.

消息 156,级别 15,状态 1,过程 GetDurationFree,第 12 行
关键字ORDER"附近的语法不正确.

Msg 156, Level 15, State 1, Procedure GetDurationFree, Line 12
Incorrect syntax near the keyword 'ORDER'.

我在网上阅读,有人提出以下建议:

I was reading online and someone suggested the following :

ALTER DATABASE yourDBName
SET COMPATIBILITY_LEVEL = 110

但是,当我运行它时,出现以下错误:

However when I run this, I get the following error :

消息 15048,级别 16,状态 1,第 1 行
数据库兼容级别的有效值为 80、90 或 100.

Msg 15048, Level 16, State 1, Line 1
Valid values of the database compatibility level are 80, 90, or 100.

我在跑步

SQL Server 2014 Management Studio,快速版

SQL Server 2014 Management Studio, the express version

但是当我输入以下 SELECT @@version

我明白了:

Microsoft SQL Server 2008 (SP1) - 10.0.2531.0 (X64) 
Mar 29 2009 10:11:52 
Copyright (c) 1988-2008 Microsoft Corporation
Express Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1)

推荐答案

我最终做的是在卸载程序下进入控制面板并删除所有与 SQL 相关的内容.然后我重新安装了程序,它运行良好.

What I ended up doing is going into the control panel under uninstall a program and deleted everything that has to do with SQL. Then I re installed the program and It worked fine.

我注意到计算机上没有安装 2008 的早期版本.也许问题出在那里.

I noticed that there a early version of 2008 on the computer that wasn't installed. Maybe the problem comes from there.

相关文章