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. 20170523xlVBA多条件分类求和一例

    Public Sub NextSeven_CodeFrame() Application.ScreenUpdating = False Application.DisplayAlerts = Fals ...

  2. C# 中的时间(DataTime)

    在做报表或查询的时候,常常会预设一些可选的日期范围,如本周.本月.本年等,利用 C# 内置的DateTime基本上都可以实现这些功能. 当前时间: DateTime dt = DateTime.Now ...

  3. VirtualBox + Centos 使用NAT + Host-Only 方式联网

    一.准备工作 1. VirtualBox 2. CentOS镜像 备注:我这里准备好了需要下载的文件,有需要的话可以下载一下,分别是VirtualBox-5.1.24-117012-Win.exe,C ...

  4. 此纳税人登记号已用于同一期间的交易方(交易方类型为 XXX 且交易方名称为 xxxx)。

    When updated Supplier's tax informations , System occurs a error:'This tax registration number is al ...

  5. Hibernate---介绍

    Hibernate---介绍 框架体系结构: 2hibernate入门 1.1 ORM框架 Hibernate是一个数据持久化层的ORM框架. Object:对象,java对象,此处特指JavaBea ...

  6. iOS UI-九宫格

    第一节课: .复习 .运行App应用管理,简单界面分析 .一个应用为一个整体,直接创建一个appView然后计算frame .说明弊端,应该根据数据的个数来for循环创建 第二节课: .加载plist ...

  7. Android轮播图Banner的实现

    从慕课网上学了一门叫做“不一样的自定义实现轮播图效果”的课程,感觉实用性较强,而且循序渐进,很适合初学者.在此对该课程做一个小小的笔记. 实现轮播思路: 1.一般轮播图是由一组图片和底部轮播圆点组成, ...

  8. mysql 索引原理及查询优化 -转载

    转载自 mysql 索引原理及查询优化 https://www.cnblogs.com/panfb/p/8043681.html 潘红伟   mysql 索引原理及查询优化 一 介绍 为何要有索引? ...

  9. ssh的配置,ssh打开密钥登陆,关闭密码登陆。

    刚装玩fedora,那么我们就以fedora为例来说一下怎么配置: 1.先确认是否已安装ssh服务: [root@localhost ~]# rpm -qa | grep openssh-server ...

  10. psycopg2 (python与postgresql)

    #快速导入数据到postgresql import pandas as pd import psycopg2 from io import StringIO def sql_to_df(): con= ...