SQL Server遍历表一般都要用到游标,SQL Server中可以很容易的用游标实现循环,实现SQL Server遍历表中记录。
但游标在实际的开发中都不推荐使用。
我们知道还可以借助临时表或表变量等来实现SQL Server遍历表
下例为用表变量来实现简单的循环:
(直接复制到查询分析器中运行即可)
     declare @temp table   
  (   
 [id] int IDENTITY(1,1),   
 [Name] varchar(10)   
    )   
  declare @tempId int,@tempName varchar(10)         
insert into @temp values('a')   
insert into @temp values('b')   
insert into @temp values('c')   
insert into @temp values('d')   
insert into @temp values('e')   
select * from @temp   
  WHILE EXISTS(select [id] from @temp)   
   begin   
SET ROWCOUNT 1    
  select @tempId = [id],@tempName=[Name] from @temp   
SET ROWCOUNT 0   
delete from @temp where [id] = @tempId   
print 'Name:----'+@tempName   
  end  

附上另外一种sql 测试代码:

declare @temp table        ( 
        [id] int IDENTITY(1,1),  
    [Name] varchar(10) ) 
declare @tempId int,@tempName varchar(10) ,@tempcount int
  insert into @temp values('a') 
  insert into @temp values('b') 
  insert into @temp values('c')
  insert into @temp values('d')
  insert into @temp values('e')
 
 set @tempcount=(select COUNT(*) from @temp);
 set @tempId=0;


while (@tempId<=@tempcount )
 begin
  set  @tempName=(select name from @temp where id=@tempId);
 
 print 'the id is :'+str(@tempId) +' the name is :  '+@tempName;
 set @tempId=@tempId+1;
 
 end

测试结果如下:

the id is :         1 the name is :  a

the id is :         2 the name is :  b

the id is :         3 the name is :  c

the id is :         4 the name is :  d

the id is :         5 the name is :  e

SQL Server中如何实现遍历表的记录的更多相关文章

  1. 从SQL Server中清除msdb备份和恢复记录

    正如我在前面的技巧“您的数据库上次恢复是什么时候呢?”中提到的,SQL Server使msdb数据库内系统表中的备份和恢复记录保持激活状态.没有正常的维护,这些系统表将变得很大,从而导致对于msdb数 ...

  2. 在SQL SERVER中根据某字段分隔符将记录分成多条记录

    XT_RSGL_KQSZ_LS表结构如下图: CREATE TABLE  XT_RSGL_KQSZ_LS( KQFW VARCHAR(400) ) 其中KQFW字段以分割符 , 隔开 INSERT I ...

  3. 转载: SQL Server中的索引

    http://www.blogjava.net/wangdetian168/archive/2011/03/07/347192.html 1 SQL Server中的索引 索引是与表或视图关联的磁盘上 ...

  4. Sql Server中的表访问方式Table Scan, Index Scan, Index Seek

    1.oracle中的表访问方式 在oracle中有表访问方式的说法,访问表中的数据主要通过三种方式进行访问: 全表扫描(full table scan),直接访问数据页,查找满足条件的数据 通过row ...

  5. 转:Sql Server中的表访问方式Table Scan, Index Scan, Index Seek

    0.参考文献 Table Scan, Index Scan, Index Seek SQL SERVER – Index Seek vs. Index Scan – Diffefence and Us ...

  6. SQL SERVER中的两种常见死锁及解决思路

    在sql server中,死锁都与一种锁有关,那就是排它锁(x锁).由于在同一时间对同一个数据库资源只能有一个数据库进程可以拥有排它锁.因此,一旦多个进程都需要获取某个或者同一个数据库资源的排它访问权 ...

  7. SQL Server中SCAN 和SEEK的区别

    SQL Server中SCAN 和SEEK的区别 SQL SERVER使用扫描(scan)和查找(seek)这两种算法从数据表和索引中读取数据.这两种算法构成了查询的基础,几乎无处不在.Scan会扫描 ...

  8. SQL Server中的三种Join方式

      1.测试数据准备 参考:Sql Server中的表访问方式Table Scan, Index Scan, Index Seek 这篇博客中的实验数据准备.这两篇博客使用了相同的实验数据. 2.SQ ...

  9. SQL Server中的执行引擎入门

      简介 当查询优化器(Query Optimizer)将T-SQL语句解析后并从执行计划中选择最低消耗的执行计划后,具体的执行就会交由执行引擎(Execution Engine)来进行执行.本文旨在 ...

随机推荐

  1. CentOS 配置Tomcat服务脚本

    #!/bin/bash # description: Tomcat7 Start Stop Restart # processname: tomcat7 # chkconfig: JAVA_HOME= ...

  2. MySQL更改了my.ini的#Path to the database root后,数据还写到原来的文件夹

    重新启动数据库 http://www.cnblogs.com/argenbarbie/p/4090494.html

  3. oracle12c新特点之可插拔数据库(Pluggable Database,PDB)

    1.    12c PDB新特点的优势 1)    可以把多个PDB集成进一个平台. 2)    可以快速提供一个新的PDB或一个已有PDB的克隆. 3)    通过拔插技术,可以快速把存在的数据库重 ...

  4. superobject数组添加json对象,用的是引用

    procedure TForm1.Button1Click(Sender: TObject); var ja,jo: ISuperObject; I: Integer; begin ja := SA( ...

  5. Java——多线程小例子

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  6. 怎么从sqlserver的存储过程获得返回的数据

    1.返回一个数值 declare @count int exec @count = testReturn \'111\',\'222\' select @count @count就是返回的数值是int ...

  7. L1-035 情人节

    以上是朋友圈中一奇葩贴:“2月14情人节了,我决定造福大家.第2个赞和第14个赞的,我介绍你俩认识…………咱三吃饭…你俩请…”.现给出此贴下点赞的朋友名单,请你找出那两位要请客的倒霉蛋. 输入格式: ...

  8. 给tabBarItem加点击效果动画

    获取到tabBarItem,添加喜欢的动画 .h文件 @interface JGTabBarController () //记录上一次点击tabbar @property (nonatomic, as ...

  9. Flappy Bird背后的故事

    更多有价值的互联网文章:晓煦分享(http://www.ihuxu.com/share) 由越南游戏工程师阮哈东(Nguyen Ha Dong)开发的Flappy Bird这款游戏,画面不算精致且看起 ...

  10. oracle数据类型及其隐式转换

    oracle有三种最基本的数据类型,即字符型.数值型.日期型. oracle提供的单行函数中,针对不同的数据类型,提供大量实用的函数,同时提供一系列数据类型转换函数,如下: 1)to_char     ...