以下以SQL Server 2000中的NorthWind数据库中的Customers表为例,

用 临时表 + While循环 的方法, 对Customers表中的CompanyName列进行遍历

create table #temp
(
  id int identity(1,1),
  customer nvarchar(50)
)

declare @customer nvarchar(50)
declare @n        int
declare @rows     int

select @n=1

insert #temp(customer) select distinct companyname from customers

select @rows = @@rowcount

while @n <= @rows
begin

select @customer = companyname 
from customers
     where companyname=(select customer from #temp where id = @n)
order by companyname desc

print(@customer)

select @n = @n + 1

end

运行后, 输出结果如下:

(所影响的行数为 91 行)

Alfreds Futterkiste
Ana Trujillo Emparedados y helados
Antonio Moreno Taquería
Around the Horn
Berglunds snabbköp
Blauer See Delikatessen
Blondesddsl père et fils
Bólido Comidas preparadas
Bon app'
Bottom-Dollar Markets
B's Beverages
Cactus Comidas para llevar
Centro comercial Moctezuma
Chop-suey Chinese
Comércio Mineiro
Consolidated Holdings
Die Wandernde Kuh
Drachenblut Delikatessen
Du monde entier
Eastern Connection
Ernst Handel
Familia Arquibaldo
FISSA Fabrica Inter. Salchichas S.A.
Folies gourmandes
Folk och fä HB
France restauration
Franchi S.p.A.
Frankenversand
Furia Bacalhau e Frutos do Mar
Galería del gastrónomo
Godos Cocina Típica
Gourmet Lanchonetes
Great Lakes Food Market
GROSELLA-Restaurante
...... (以下略) ..................................

SQL查询遍历数据方法一 [ 临时表 + While循环]的更多相关文章

  1. SQL常见优化Sql查询性能的方法有哪些?

    常见优化Sql查询性能的方法有哪些? 1.查询条件减少使用函数,避免全表扫描 2.减少不必要的表连接 3.有些数据操作的业务逻辑可以放到应用层进行实现 4.可以使用with as 5.使用“临时表”暂 ...

  2. MySQL查询关键数据方法

    MySQL查询关键数据方法 操作表的SQL语句补充 1.修改表名 alter table 表名 reame 新表名: 2.新增字段名 alter table 表名 add 字段名 字段类型(数字) 约 ...

  3. 视图查询的数据和sql查询的数据不一样

    视图查询的数据和sql查询的数据不一样. 手动刷新视图 exec sp_refreshview  视图名称

  4. Mysql常用30种SQL查询语句优化方法

    出处:http://www.antscode.com/article/12deee70111da0c4.html 1.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使 ...

  5. MySQL 常用30种SQL查询语句优化方法

    1.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索引而进行全表扫描. 2.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉 ...

  6. sql 查询重复数据,删除重复数据,过滤重复数据

    select * from (SELECT titleid,count(titleid) c FROM [DragonGuoShi].[dbo].[ArticleInfo] group by titl ...

  7. 关于SQL查询某年数据 和DATEPART 函数的使用

    数据库查询某年数据(sql server)select *from 表名 where YEAR(存时间的字段名) =某年select *from News where YEAR(addDate) =2 ...

  8. ORACLE和SQL查询库数据量

    ORACLE根据账号查询每张表数据量: select t.table_name,t.num_rows from user_tables t ORDER BY NUM_ROWS DESC; SQL SE ...

  9. springDataJPQL实现增删改查及分页,原生sql查询,根据方法命名规则实现查询以及Specification查询

    一.使用方法 1.在dao中定义开一个方法,使用方法的参数设置jpql,并且使用方法的返回值接受查询结果,在方法上添加@query注解,在注解中写jpql语句进行增删改查,测试 2.使用原生的sql语 ...

随机推荐

  1. [原]rpm安装rpm-package报错:Header signature NOKEY 和 error: Failed dependencies:

    以前经常遇到这个问题,一直未有记录,今天记录下来: 在安装rpm包的时候报错误如下: Question 1: warning: *.rpm: Header V3 DSA signature: NOKE ...

  2. Egret IDE中搜索,过滤文件,只搜索.ts

    刚开始忘了这个搜索条件在哪里打开了,后来找着了,记录一下 = =!

  3. File类使用

    简介 File类的实例代表了一个文件或者一个目录,通过API可以获取这个对象的相关信息. File类代表的文件或者目录可以真实存在,也可以是不存在的,可以使用File.exists()来判断. 在Wi ...

  4. jenkins使用Publish Over SSH中遇到的问题

    在jenkins中想使用publish over ssh来在构建后发送XML或PNG文件至服务器,以便做其它的操作,安装完publish over ssh后,填加构建,发现在构建失败时,不传送文件,老 ...

  5. RabbitMQ服务端配置详解(转自:http://www.cnblogs.com/zhen-rh/p/6884297.html)

    RabbitMQ支持三种配置方式: 1) 读取环境变量中配置, 这包括shell中环境变量和rabbitmq-env.conf/rabbitmq-env-conf.bat文件中配置的环境变量 可配置如 ...

  6. Apache服务器301重定向去掉.html和.php

    在做优化网站的时候,会考虑到网站整站的集权: 考虑到网站可以生成静态,首先,让网站优先访问 index.html 之后考虑:去掉 .html 和 .php. 利用 .htaccess <IfMo ...

  7. SpringBoot应用配置常用相关视图解析器

    目录 SpringBoot的自动装配装配了视图解析器了吗? SpringBoot使用JSP SpringBoot中使用Thymeleaf SpringBoot中使用Freemark SpringBoo ...

  8. Oracle备份恢复之逻辑备份

    exp 交互模式:导出scott用户下的emp表. [oracle@localhost ~]$ exp Export: Release 10.2.0.1.0 - Production on Thu N ...

  9. iOS多线程编程之线程的状态(转载)

    一.简单介绍 线程的创建: self.thread=[[NSThread alloc]initWithTarget:self selector:@selector(test) object:nil]; ...

  10. PHP程序员应该知道的15个库

    最几年,PHP已经成为最受欢迎的一种有效服务器端编程语言.据2013年发布的一份调查报告显示,PHP语言已经被安装在全球超过2.4亿个网站以及210万台Web服务器之上.PHP代表超文本预处理器,它主 ...