【问题标题】:Incorrect syntax near the keyword 'rowcount'关键字“rowcount”附近的语法不正确
【发布时间】:2012-08-13 18:04:14
【问题描述】:

我有这个代码:

merge dbo.tblProblems as target 
using ( 
    select max(problemID), StationName, problemCode, max(ProblemCreateDate), count(*) 
    from dbo.tblProblems 
    group by StationName, problemCode 
) as source(id, StationName, problemCode, maxdate, rowcount) 
on ( 
        target.problemID = source.problemID 
    and target.StationName = target.StationName 
    and target.problemCode = target.problemCode 
) 
when matched then 
    update set ProblemCreateDate = maxdate, probCount = rowcount 
when not matched then delete ;    

但它会产生这个错误:

消息 156,级别 15,状态 1,第 6 行
关键字“行计数”附近的语法不正确。

这个版本的查询:

;WITH n AS  
( 
  SELECT problemID, StationName, problemCode, ProblemCreateDate, probCount 
    c = COUNT(*) OVER (PARTITION BY StationName, problemCode), 
    rn = ROW_NUMBER() OVER  
    ( 
      PARTITION BY StationName, problemCode ORDER BY ProblemCreateDate DESC, problemID DESC 
    ) 
  FROM dbo.tblProblems
) 
 --SELECT problemID, StationName, problemCode, ProblemCreateDate, c 
 -- FROM n WHERE rn = 1; 
UPDATE n SET probCount = c 
  WHERE rn = 1; 

产生此错误:

消息 102,第 15 级,状态 1,第 4 行
'=' 附近的语法不正确。

【问题讨论】:

    标签: sql sql-server-2008 tsql


    【解决方案1】:

    对于第一个查询,rowcountreserved keyword。使用不同的词(例如row_count)或用方括号括起来:

    [rowcount]
    

    对于第二个查询,probCount后面需要一个逗号:

    SELECT problemID, StationName, problemCode, ProblemCreateDate, probCount,
    ------------------------------------------------------------------------^
      c = COUNT(*) OVER (PARTITION BY StationName, problemCode), 
    

    你没有完全正确地适应my code sample(结尾的逗号在那里)。

    【讨论】:

    • when matched then update set ProblemCreateDate = maxdate, probCount = row_count when not matched then delete ; Msg 10710, Level 15, State 1, Line 14 在 MERGE 语句的“WHEN NOT MATCHED”子句中不允许使用“DELETE”类型的操作.
    • 请不要将代码示例添加为 cmets。我不知道你的评论想说什么,而且代码很难以这种方式阅读。
    • 我应该坚持你的解决方案.. Aron 这个很短......我试试
    • 好的...下次会做.. aron,看看页面顶部代码中“when”的最后几行会给出错误
    • 好吧,我确实尝试过(现在看 - 重新编辑)你的代码我做过,我忘了抱歉,它很干净,选择部分正在工作,看看我添加了什么 plz
    猜你喜欢
    • 2016-06-08
    • 2013-12-16
    • 2017-11-22
    • 2018-06-16
    • 2013-10-29
    • 2014-06-22
    • 1970-01-01
    • 2013-05-04
    • 2013-05-21
    相关资源
    最近更新 更多