sql写语句如何循环执行10000次

insert into julicef(id,GPSLng,GPSLat)select id,GPSLng,GPSLat from julice where id=(select min(id)from julice)

insert into juliceg(id,GPSLng,GPSLat,store_id,Lng,Lat,distance)SELECT *, ( 6378.140* acos( cos( radians(GPSLat) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(GPSLng) ) + sin( radians(GPSLat) ) * sin( radians( lat ) ) ) ) AS distance FROM julicef,juli order by id,GPSLng,GPSLat,store_id,Lng,Lat asc

insert into juliceI(id,GPSLng,GPSLat,store_id,Lng,Lat,distance)select top 3 * from juliceg order by id,GPSLng,GPSLat,distance,store_id,Lng,Lat asc

delete from julicef

delete from julice where id=(select min(id)from julice)

delete from juliceg

这个问题涉及到sql语句的循环执行语句的用法。sql语句中的循环和其他编程语言的原理是类似的,只不过写法上有点区别。
1.定义循环时需要用到的变量并赋值:
declare @i int
set @uId=1
2.sql语句的循环是需要嵌套在begin,end语句之内的:
begin
#需要执行的语句。
end
3.while语句的语法如下(需要注意,每次循环完成要给变量加1):
while @uId<=10000
select * from test where id=10
set @uId=@uId+1
4.完整语句示例如下:
declare @i int
set @uId=1
begin
while @uId<=10000
select * from test where id=10
set @uId=@uId+1
end
温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-08-15

调用循环执行,例如:

declare @n int
set @n=0
begin
while @n<10000
set @n=@n + 1
-- 这里运行您要执行的1万次操作
-- 例如您提问中的那些动作查询
 
end

   

追答

只要是能够执行的操作,一定能够执行的,记得每条语句都要加英文分号";" 进行隔离

对了,还要关闭提示信息:
declare @n int
set nocount on
set @n=0
begin
while @n<10000
set @n=@n + 1
-- 这里运行您要执行的1万次操作
-- 例如您提问中的那些动作查询

end
set nocount off

追问

试了不行

追答

循环肯定没问题,请逐条检查你的操作语句,找出出错的操作语句,然后修正出错的语句

先单独运行每个insert语句,排除语法上的错误,然后再排除插入的记录是否会在循环里出现违反约束的情形,可以在循环里逐个增加insert来测试排除

追问

好的,我试试

操作语句中是不是必须包含循环变量@n?

追答

操作语句里不必须有循环变量,但是@n必须在循环里不断增长,否则会死循环,跳不出来。

本回答被网友采纳