while exists(select mobile,count(*) from @表名 group by mobile having count(*)>1 ) --这里的表名怎么处理呢???
--换成这样看看
while exists(exec('select mobile,count(*) from '+@表名+' group by mobile having count(*)>1'))

解决方案 »

  1.   

    leo_lesley(leo) 的方法,我开始试过,不行.我试试rookie_one和weihouyanli() 的方法.先谢三位
      

  2.   

    我也遇到这样的问题,也是用@@rowcount处理的
    select mobile,count(*) from @表名 group by mobile having count(*)>1
    这个还是拼接SQL串
    然后execute 判断@@rowcount
      

  3.   

    查询语句中字段或表名是变量的话只能用动态查询(exec),
    因为exists不能内嵌exec语句,所以也只能用@@rowcount处理了
      

  4.   

    declare @表名 varchar(50)
    declare @SQLstr varchar(3000)
    SET @SQLstr= 'delete from '+ @表名+' where id in (select max(id) from '+ @表名+'group by mobile having count(*)>1)'
    declare @condition varchar(1000)
    set @condition='select mobile,count(*) from ' + @表名 + ' group by mobile having count(*)>1'
    exec(@condition)
    while @@rowcount>0
    begin
    EXEC(@SQLstr)
    set @condition='select mobile,count(*) from ' + @表名 + ' group by mobile having count(*)>1'
    exec(@condition)
    end--楼主试试
      

  5.   

    --这个更好一些
    declare @表名 varchar(50)
    declare @SQLstr varchar(3000)
    SET @SQLstr= 'delete from '+ @表名+' where id in (select max(id) from '+ @表名+'group by mobile having count(*)>1)'
    declare @condition varchar(1000)
    set @condition='select mobile,count(*) from ' + @表名 + ' group by mobile having count(*)>1'
    exec(@condition)
    while @@rowcount>0
    begin
    set nocount on
    EXEC(@SQLstr)
    set nocount off
    exec(@condition)
    end
      

  6.   

    谢谢!是按wanglei8() 解决的.  :)