SQL Server语法,用于通过子查询进行更新

时间:2009-07-09 09:20:01

标签: sql sql-server

如何制定此查询

update forge..dimInteg2
   set duplicates = 
      (select count(*) from (select idCover 
                                 from x90..dimCover
                                 group by idCover
                                 having count(*) > 1)) 
where dimTable = 'dimCover'

避免此错误

Line 6: Incorrect syntax near ')'.

与[SQL Server subquery syntax相似 但我似乎无法使用别名技巧。

我在SQL Server 2000上

3 个答案:

答案 0 :(得分:3)

你错过了一个括号吗?

update forge..dimInteg2
set duplicates = 
  (select count(*) from (select idCover 
                             from x90..dimCover
                             group by idCover
                             having count(*) > 1) ) --HERE
where dimTable = 'dimCover'

然后别名解决方案应该有效。

答案 1 :(得分:0)

尝试将字段dimTable添加到第二个选择语句:

update forge..dimInteg2
   set duplicates = 
      (select count(*) from (select idCover --, dimTable HERE
                                 from x90..dimCover
                                 group by idCover
                                 having count(*) > 1) 
where dimTable = 'dimCover'

答案 2 :(得分:0)

您需要命名派生表。

更新forge..dimInteg2    设置重复=       (从中选择count(dimCover.idCover)(选择idCover                                  来自x90..dimCover                                  idCover分组                                  有计数(*)> 1)dimCover) 其中dimTable ='dimCover'