以下以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. github上打包的样式为什么在预览的时候,出现404

    这是资源引用的问题 在这里主要是需要在dist的index.html文件内将"./static/css/style.css"改为"static/css/style.css ...

  2. How to Use Postman to Manage and Execute Your APIs

    How to Use Postman to Manage and Execute Your APIs Postman is convenient for executing APIs because ...

  3. PHP内置安全函数一览

    内置安全函数 filter_var函数 根据参数中的过滤类型进行过滤,如过滤Email类型的,则符合的字符串返回字符串,不符合的返回False. urldecode函数 写这个函数是特别为了提醒注意, ...

  4. jumpserver的安装

    原文地址:http://docs.jumpserver.org/zh/docs/step_by_step.html 为了保证服务器安全,加个堡垒机,所有ssh连接都通过堡垒机来完成,堡垒机也需要有身份 ...

  5. 【转】Windows socket基础

    转自:http://blog.csdn.net/ithzhang/article/details/8448655 Windows socket 基础 Windows socket是一套在Windows ...

  6. css如何设置div中的内容垂直居中?

    <style>.out { position: relative;//相对div的定位 top: 30%;//相对div的border-top的距离,根据元素的高度,50%则为垂直居中:} ...

  7. SQL已存在则更新不存在则插入

    不废话,下代码. replace into T_Life_UMessage(message_id,account,isread,isdelete)values(?,?,1,1) 意思是若不存在则插入要 ...

  8. 9.19Cookie

    2018-9-19 15:02:19 cookie 使用

  9. 170815、redis3.0安装配置

    下载地址http://redis.io/download 安装步骤: 1 首先需要安装gcc,把下载好的redis-3.0.0-rc2.tar.gz 放到linux /usr/local文件夹下 2 ...

  10. HDU-1003 Max Sum(动态规划,最长字段和问题)

    Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...