SQL server中常用sql语句

--循环执行插入10000条数据

declare @ID int
begin
set @ID=1

while @ID<=10000
begin
insert into table_name values('','','')--要循环的sql语句
set @ID=@ID+1
end
end

--获取随机数

select rand()--获取0~1之间的float型的数字

DECLARE @NumBegin Int=60 --随机数的最小值
DECLARE @NumEnd Int=100 --随机数的最大值
DECLARE @Decimal Int=2 --保留小数点几位
SELECT @NumBegin+round((@NumEnd-@NumBegin)*rand(),@Decimal)

--删除表中数据,不可恢复

truncate table table_name

--查找重复数据

select * from tablename
where user_id in(select  user_id from tablename group by  user_id having COUNT(*)>1)
order by  user_id 

 --删去重复的,只留下重复记录中编码最大的一条

delete from 表名 where 

编码 in(select 编码 from 表名 group by 编码 having count(1) >= 2) 

and 编码 not in (select max(编码)from 表名 group by 编码 having count(1) >=2)
--查找数据(排除重复数据)

select * from [表名] where 编码 not in (
select 编码 from [表名] where
编码 in(select 编码 from [表名] group by 编码 having count(1) >= 2)
and 编码 not in (select max(编码) from [表名] group by 编码 having count(1) >=2))

 或

select * from [表名] where 自增主键 in (
select max(自增主键) from [表名] group by 编码 )

 
原文地址:https://www.cnblogs.com/xianyv/p/11556271.html