发现')'附近的语法错误SQL服务器

时间:2016-08-05 04:31:51

标签: sql-server

当我运行此查询时,不知道为什么会出现此错误

  

')'

附近的语法不正确

感谢您的帮助

Select 
    Sum(DateDiff(s, Toc1.OutTime, (Select Top 1 Intime 
                                   From tblTokenLogs 
                                   Where TokenId = Toc1.TokenId 
                                     And LogId != Toc1.LogId 
                                     And LogId > Toc1.LogId))) as Sec 
From 
    tblTokenLogs As Toc1 
Where 
    Toc1.TokenId = 1 
    And Toc1.OutTime != '') as SecTotal

2 个答案:

答案 0 :(得分:0)

将此查询与您的查询进行比较,您就会知道出了什么问题!

SELECT SUM(
           DATEDIFF(
               s,
               Toc1.OutTime,
               (
                   SELECT TOP 1 Intime
                   FROM   tblTokenLogs
                   WHERE  TokenId = Toc1.TokenId
                          AND LogId != Toc1.LogId
                          AND LogId > Toc1.LogId
               )
           )
       )             AS Sec
FROM   tblTokenLogs  AS Toc1
WHERE  Toc1.TokenId = 1
       AND Toc1.OutTime != '' 

不需要的别名) as SecTotal

答案 1 :(得分:0)

试试这个,

Select 
    Sum(DateDiff(s, Toc1.OutTime, Toc2.Intime )) as Sec 
From 
    tblTokenLogs As Toc1
OUTER APPLY (   Select Top 1 Intime 
                From tblTokenLogs 
                Where TokenId = Toc1.TokenId 
                    And LogId != Toc1.LogId 
                    And LogId > Toc1.LogId
                ) AS Toc2
Where 
    Toc1.TokenId = 1 
    And Toc1.OutTime != ''