设为首页  
联系我们  
加入收藏  
网页制作 冲浪宝典 图形图像 操作系统 软件教学 编程开发 认证考试 安全技术 站长专区 文学驿站 娱乐天地 游戏天地 办公软件
文章搜索
您的位置: 首页 >> 文章首页 >> 编程开发 >> 数据库开发 >> Processing Sequentially Through a Set of Records
精品推荐
数据库开发点击TOP10
·MYSQL 新版出现 Client does not support authentication protocol requested by server; consider upgrading MySQL client解决办法
·生成助记码(取汉字的第一个字母)
·ORA-01034错误的解决办法
·ORACLE数据库管理员的职责
·将DW数据窗口导出为EXCEL文件的方法(整理)
·Oracle中分区表的使用
·SQL Server 2000+ MS WIN2003群集服务配置
·SQL中UNION 与 UNION ALL 的区别
·ORA-00257: archiver error. Connect internal only, until freed.
·Oracle备份与恢复案例
编程开发点击TOP10
·数字小键盘指法练习
·ASP.NET 程序中常用的三十三种代码
·用C语言编通讯录程序(初学者级别的)
·我写的Java学生成绩管理系统源代码
·CHK文件恢复工具
·Modem 常用AT指令集
·java笔试题
·异常java.sql.SQLException: Io exception:The Network Adapter could not establish connection
·单片机模拟I2C总线及24C02(I2C EEPROM)读写实例(源代码)
·C++经典电子书下载
精选专题

Processing Sequentially Through a Set of Records

作者: 来源:网络文章 时间:2005-12-17 22:30:12

Processing Sequentially Through a Set of Records(3) You can also use a SELECT statement to process through a set of records one record at a time. To do this I will issue an initial SELECT statement that will return the first row, then a series of follow on SELECT statements where each SELECT statement retrieves the next row. This is done by using the "TOP 1" clause of the SELECT statement, and a WHERE statement.

I will use the same example as above and only return the top 5 CustomerID's from the Northwind database Customers table. In this code I will use two different "SELECT TOP 1" statements and a WHILE loop to return all 5 records. Each record will be processed one at a time.

declare @CustId nchar(5)declare @RowNum intselect top 1 @CustId=CustomerID from Northwind.dbo.Customersset @RowNum = 0 WHILE @RowNum < 5BEGINset @RowNum = @RowNum + 1print cast(@RowNum as char(1)) + ' ' + @CustIdselect top 1 @CustId=CustomerID from Northwind.dbo.Customerswhere CustomerId > @CustIDEND

Here you can see the first SELECT statement selects only the first CustomerID. This ID is placed in the local variable @CustID. The WHILE loop is controled by the local variable @RowNum. Each time through the WHILE loop, the Row Number and CustomerID are printed out. Prior to returning to the top of the WHILE loop I used another "SELECT TOP 1" statement to select the next CustomerID. This SELECT statement uses a WHERE clause on the SELECT statement to select the first CustomerID that is greater than the CustomerID that was just printed. The WHILE loop is process 5 times, allowing the SELECT TOP 1 method to retrieve the top 5 CustomerID's one records at a time. This example produces the same printed output as my prior CURSOR example.

Conclusion

Hopefully this article has given you some ideas on how to use a CURSOR, and a SELECT statement to process through a set of records. I use both of these methods, although I find using a SELECT statement to be a little simpler to code. You will need to decide which solution makes the most sense in your environment.

共4页 9 7 [1] [2] [3] [48 :>

Processing Sequentially Through a Set of Records 相关文章:
Processing Sequentially Through a Set of Records 相关软件:
特别声明:本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。
转载请注明来源:http://www.xgdown.com