LAG is not a recognized built in function name

一世执手 提交于 2021-02-07 17:24:11

问题


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 :

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

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 :

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

I am running

SQL Server 2014 Management Studio, the express version

However when I enter the following SELECT @@version

I get :

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)

回答1:


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.

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




回答2:


You need SQL Server 2012 database with compatability level at 110 to use LAG function.

ALTER DATABASE DBName
SET COMPATIBILITY_LEVEL = 110


来源:https://stackoverflow.com/questions/38338569/lag-is-not-a-recognized-built-in-function-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!