4

我正在尝试执行此查询,但我不断收到错误消息

Msg 102, Level 15, State1
Incorrect syntax near ')'. 

你能看看我的查询吗?我相信每个 SQL 关键字我都添加了适当的括号。

USE [db]
GO

SET NOCOUNT ON

DECLARE @AlertDateTime date
      , @HostName  varchar(50)
      , @Program varchar(100)
      , @Event varchar(100)
      , @Entity varchar(50)
      , @Class tinyint
      , @Severity tinyint
      , @AlertMessage varchar(1024)
      , @SkipFlag char(1) 

select cast(getdate() as date)

IF EXISTS 

(SELECT * FROM SendAlertLog 

WHERE EXISTS
(
    SELECT SendAlertLog.Event  
    FROM SendAlertLog
    GROUP BY SendAlertLog.Event 
    HAVING COUNT (1) >= 2 
)
AND SendAlertLog.SkipFlag != 'Y' 
AND AlertDateTime <= DATEADD (mi,-30,@AlertDateTime)
)

我不知道我包含或排除了什么错误。请帮忙

4

2 回答 2

3

看起来您有一个 if,但如果表达式为真,则无需执行任何语句。你有

IF EXISTS(...)

但是你需要

IF EXISTS(...)
   DO_SOMETHING
于 2013-04-27T04:41:00.640 回答
2

试试这个(没有双重使用 SendAlertLog) -

DECLARE @AlertDateTime DATE

IF EXISTS (
    SELECT 1 
    FROM (
        SELECT 
              cnt = COUNT(1)
            , SkipFlag = MAX(l.SkipFlag)
            , AlertDateTime = MAX(l.AlertDateTime)
        FROM dbo.SendAlertLog l
        GROUP BY l.[Event] 
    ) l
    WHERE cnt >= 2
        AND l.SkipFlag != 'Y' 
        AND l.AlertDateTime <= DATEADD(mi, -30, @AlertDateTime)
) BEGIN

    PRINT 'Do something'

END
于 2013-04-27T05:50:26.183 回答