1 declare @result table
2 (
3 custid int,
4 ordermonth datetime,
5 qty int,
6 runqty int,
7 primary key(custid,ordermonth)
8 );
9
10 declare
11 @custid as int,
12 @prvcustid as int,
13 @ordermonth as datetime,
14 @qty as int,
15 @runqty as int;
16 declare c cursor fast_forward for --定义游标
17 select custid,ordermonth,qty
18 from Sales.CustOrders
19 order by custid,ordermonth;
20
21 open c --打开游标
22 fetch next from c into @custid,@ordermonth,@qty;
23 select @prvcustid = @custid,@runqty = 0;
24 while @@fetch_status = 0 --当不是最后一行记录时,函数返回0
25 begin
26 --Do something
27 end
28 close c; --关闭游标
29 deallocate c; --释放游标

2、通过特定ID。

 1 declare @max int
2 declare @i int
3 select ROW_NUMBER() over (order by UnitId) as 'Id',RECID into #temp from UNIT
4 select @max=max(Id) from #temp
5 set @i = 1
6 while (@i <= @max)
7 begin
8 if @i = 10
9 begin
10 select * from UNIT where RECID = (select RECID from #temp where ID = @i) --11125166
11 break
12 end
13 set @i = @i + 1
14 end
15
16 drop table #temp

3.表

create table ta(id int,name datetime)
declare @i int ,@name datetime
set @i=0;
while @i<10
begin
set @i=@i+1
--insert into ta (id,name) values(@i,DATEADD(hh,@i,GETDATE()))
Select @name=name from ta where id=@i
print @name
end
select * from ta

MSSQL中循环的更多相关文章

  1. JavaScript中让Html页面中循环播放文字

    JavaScript中让Html页面中循环播放文字 <html> <head> <meta http-equiv="Content-Type" con ...

  2. Mssql中一些常用数据类型的说明和区别

    Mssql中一些常用数据类型的说明和区别 1.bigint 占用8个字节的存储空间,取值范围在-2^63 (-9,223,372,036,854,775,808) 到 2^63-1 (9,223,37 ...

  3. 说说你所熟知的MSSQL中的substring函数

    说说你所熟知的MSSQL中的substring函数 *:first-child { margin-top: 0 !important; } body>*:last-child { margin- ...

  4. mysql数据表如何导入MSSQL中

    本案例演示所用系统是windows server 2012.其它版本windows操作系统类似. 1,首先需要下载mysql odbc安装包. http://dev.mysql.com/downloa ...

  5. Java中循环删除list中元素的方法总结

    印象中循环删除list中的元素使用for循环的方式是有问题的,但是可以使用增强的for循环,然后在今天使用的时候发现报错了,然后去科普了一下,发现这是一个误区.下面我们来一起看一下. Java中循环遍 ...

  6. [置顶] LOAD语句:利用MSSQL中的xp_cmdshell功能,将指定文件夹下的指定文件,生成mysql的LOAD语句

    LOAD语句:利用MSSQL中的xp_cmdshell功能,将指定文件夹下的指定文件,生成mysql的LOAD语句 declare @sql varchar(4000), @dirpath varch ...

  7. 【JS中循环嵌套常见的六大经典例题+六大图形题,你知道哪几个?】

    首先,了解一下循环嵌套的特点:外层循环转一次,内层循环转一圈. 在上一篇随笔中详细介绍了JS中的分支结构和循环结构,我们来简单的回顾一下For循环结构: 1.for循环有三个表达式,分别为: ①定义循 ...

  8. JAVA中循环删除list中元素的方法总结【转】

    印象中循环删除list中的元素使用for循环的方式是有问题的,但是可以使用增强的for循环,然后今天在使用时发现报错了,然后去科普了一下,再然后发现这是一个误区.下面就来讲一讲..伸手党可直接跳至文末 ...

  9. js中return false; jquery中需要这样写:return false(); Jquery 中循环 each的用法 for循环

    js中return false; jquery中需要这样写:return false(); Jquery 中循环 each的用法 $(".progressName").each(f ...

随机推荐

  1. [Codeforces #190] Tutorial

    Link: Codeforces #190 传送门 A: 明显答案为$n+m-1$且能构造出来 #include <bits/stdc++.h> using namespace std; ...

  2. hdu 1864 最大报销额(背包)

    最大报销额 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  3. centos svnversion安装部署

    第一步: yum install subversion; 第二步: mkdir /data/svn/conf mkdir /data/svn/library 第三步: svnadmin create  ...

  4. 让SignalR客户端回调支持强类型

    几天写一个小程序的时候用到了SignalR,发现现在SingalR Server 支持强类型了,也就是说,我们可以定义一个客户端的通知契约: public interface IClient    { ...

  5. Sys.dm_os_wait_stats Sys.dm_performance_counters

    wait_type waiting_tasks_count wait_time_ms max_wait_time_ms signal_wait_time_ms MISCELLANEOUS 0 0 0 ...

  6. [bug]超时时间已到。超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。

    引言 自己弄了一个小项目——日程管理系统,在初始化日期时,查询了数据库,每个日期就会查询一次数据库,就导致了这个问题. 问题 出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小. Desc ...

  7. laravel的 array 函数

    代码如下:  routes.php文件 // 获⃣取⃣数⃣组⃣的⃣第⃣一⃣个⃣ Route::get('/helper', function () { $arr = [1, 2, 4]; return ...

  8. 配置安全证书的Etcd集群

    不知在哪篇技术文档中看到,kubernetes master和etcd分开部署模式,因为集群的状态都保存在etcd中,这样当kubernetes master挂掉后,通过API Server交互的Sc ...

  9. Linux/Unix C编程之的perror函数,strerror函数,errno

    #include <stdio.h> // void perror(const char *msg); #include <string.h> // char *strerro ...

  10. nginx安装和配置

    一.安装:yum install nginx service nginx restart/start/check/status/... 二.配置:官网文档 http://nginx.org/en/do ...