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'))
--换成这样看看
while exists(exec('select mobile,count(*) from '+@表名+' group by mobile having count(*)>1'))
解决方案 »
- 求VC6,ADO使用conection的Excute方法查询数据表并显示查询结果到列表控件的方法
- sqlserver如何根据日志记录对数据库在某一次操作进行恢复?
- *****求sqlserver第三方客户端软件*****
- 如何写这样的SQL语句,你一定没有见过?
- 如何回滚一个update语句???在线等待!
- 怎样在dts包里实现检验并删除重复的记录?
- 关于合计GROUP BY的一个复杂问题,谢谢各位高手
- 有一个大批量数据筛选的问题,麻烦高手进来帮个忙。。在线等。。解决就给分。。
- 请问:在PB中怎样把本地的access数据库中的数据插入到远程的sqlserver数据库
- SQL SERVER的日期转换问题?(在线等待)
- 请xiequanqin和tntzbzc进本贴!
- 请教select的问题!急!
select mobile,count(*) from @表名 group by mobile having count(*)>1
这个还是拼接SQL串
然后execute 判断@@rowcount
因为exists不能内嵌exec语句,所以也只能用@@rowcount处理了
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--楼主试试
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