我的右括号是抛出错误

时间:2013-04-27 04:38:12

标签: sql sql-server

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

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)
)

我不知道我包括或排除错误。请帮忙

2 个答案:

答案 0 :(得分:3)

如果表达式为true,看起来你有if,但没有声明要做。

IF EXISTS(...)

但你需要

IF EXISTS(...)
   DO_SOMETHING

答案 1 :(得分: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