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. hihoCoder-1087 Hamiltonian Cycle (记忆化搜索)

    描述 Given a directed graph containing n vertice (numbered from 1 to n) and m edges. Can you tell us h ...

  2. UVA-1252 Twenty Questions (状压DP)

    题目大意:有n件物品,每件物品有m个特征,可以对特征进行询问,询问的结果是得知某个物体是否含有该特征,要把所有的物品区分出来(n个物品的特征都互不相同)最小需要多少次询问? 题目分析:定义dp(s,a ...

  3. RedHat Linux 5下不能使用fdisk的问题

    最近在用RedHat Linux5的时候,使用fdisk命令,遇到了下面的错误: bash:fdisk:command not found 在网上找了找,解决方案如下: 首先,这个错误的原因是因为fd ...

  4. Spring Boot的SpringApplication类详解

    相信使用过Spring Boot的开发人员,都对Spring Boot的核心模块中提供的SpringApplication类不陌生.SpringApplication类的run()方法往往在Sprin ...

  5. ansible role[初稿]

    ansible roles role_name/ files/:存储由copy或script等模块调用的文件: tasks/:此目录中至少应该有一个名为main.yml的文件,用于定义各task:其它 ...

  6. maven多环境配置

    我们在开发项目的时候,往往会有好几个环境.比如开发.预发布(测试).产品,每个环境一般用到配置都不一样,最典型的就是数据库,开发的数据库与产品的数据库肯定是不一样的,如果要多个环境的切换就得改配置,这 ...

  7. oracle function用法(本文来自百度文库)

    函数调用限制 1.SQL语句中只能调用存储函数(服务器端),而不能调用客户端的函数 2.SQL只能调用带有输入参数,不能带有输出,输入输出函数 3.SQL不能使用PL/SQL的特有数据类型(boole ...

  8. 关于Oracle的一些基础知识以及注意事项

    一.oracle基础 1.1 DDL(Data Definition Language) 数据定义语言 create drop,desc(注意,此操作只能在PL/SQL Developer的命令窗户执 ...

  9. oracle索引原理

    B-TREE索引(二叉树索引,默认情况下,我们建的索引都是此种类型) 一个B树索引只有一个根节点,它实际就是位于树的最顶端的分支节点.可以用下图一来描述B树索引的结构.其中,B表示分支节点,而L表示叶 ...

  10. CCScrollView

    #ifndef TestCCScrollView_testScene_h #define TestCCScrollView_testScene_h #include "cocos2d.h&q ...