是什么导致SELECT错误附近的语法不正确?

时间:2019-10-03 18:01:23

标签: .net sql-server tsql

此查询中的“选择”附近出现错误的语法错误。此处的目标是批量删除alohaftplog中的行1000。

DECLARE @Batch INT = 1000
DECLARE @DateLimit DATETIME = CONVERT(VARCHAR(15), GETDATE() - 30, 101)
DECLARE @Cnt INT = 0

SET @Cnt = SELECT COUNT(*) FROM AlohaFtpLog WHERE DATE < @DateLimit 

WHILE @Cnt > 0
BEGIN
    DELETE TOP(@Batch) FROM AlohaFtpLog WHERE DATE < @DateLimit
    SET @Cnt = SELECT COUNT(*) FROM AlohaFtpLog WHERE DATE < @DateLimit
END

3 个答案:

答案 0 :(得分:3)

SET @Cnt = SELECT COUNT(*) FROM AlohaFtpLog WHERE DATE < @DateLimit

需要是:

SET @Cnt = (SELECT COUNT(*) FROM AlohaFtpLog WHERE DATE < @DateLimit)

OR:

SELECT @Cnt = COUNT(*) FROM AlohaFtpLog WHERE DATE < @DateLimit

答案 1 :(得分:1)

SELECT @Cnt = COUNT(*) FROM AlohaFtpLog WHERE DATE < @DateLimi

答案 2 :(得分:0)

我很确定您需要一组括号。

SET @Cnt = (SELECT COUNT(*) FROM AlohaFtpLog WHERE DATE < @DateLimit)