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. Python解析Wav文件并绘制波形的方法

    资源下载 #本文PDF版下载 Python解析Wav文件并绘制波形的方法 #本文代码下载 Wav波形绘图代码 #本文实例音频文件night.wav下载 音频文件下载 (石进-夜的钢琴曲) 前言 在现在 ...

  2. 新项目中使用的linux命令

    要通过跳板机进入内网之后,访问内网域名 mysql -h xxxxxxx -u u_caojiangjiang -p -P 3306 上传文件: scp -r /Users/qudian/Deskto ...

  3. c++中的new和delete

    对于计算机程序设计而言,变量和对象在内存中的分配都是编译器在编译程序时安排好的,这带来了极大的不便,如数组必须大开小用,指针必须指向一个已经存在的变量或对象.对于不能确定需要占用多少内存的情况,动态内 ...

  4. Coconuts, Revisited(递推+枚举+模拟)

    Description The short story titled Coconuts, by Ben Ames Williams, appeared in the Saturday Evening ...

  5. POJ-3009 Curling 2.0 (DFS)

    Description On Planet MM-21, after their Olympic games this year, curling is getting popular. But th ...

  6. keras-anomaly-detection 代码分析——本质上就是SAE、LSTM时间序列预测

    keras-anomaly-detection Anomaly detection implemented in Keras The source codes of the recurrent, co ...

  7. javascript--- document.write()和 innerHTML的区别

    document.write是直接写入到页面的内容流,如果在写之前没有调用document.open, 浏览器会自动调用open.每次写完关闭之后重新调用该函数,会导致页面被重写. innerHTML ...

  8. PHP:第一章——PHP中的关键字

    <?php //PHP关键词 /* and //php中的逻辑与运算符.(和) or //php中的逻辑或运算符.(或) xor //php中的逻辑异或.(异或) __FILE__ //php中 ...

  9. write file to stroage trigger kernel warning

    when you write large files  to extern stroage, the kernel may have as follow context: [ 4560.236385] ...

  10. POJ 2309 BST(树状数组Lowbit)

    题意是给你一个满二叉树,给一个数字,求以这个数为根的树中最大值和最小值. 理解树状数组中的lowbit的用法. 说这个之前我先说个叫lowbit的东西,lowbit(k)就是把k的二进制的高位1全部清 ...