﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>IT博客网-寻梦岁月-随笔分类-DataBase</title><link>http://www.cnitblog.com/ffan/category/476.html</link><description>寻梦的岁月不言辛苦几许,不问收获几多…</description><language>zh-cn</language><lastBuildDate>Tue, 10 Jul 2007 19:02:30 GMT</lastBuildDate><pubDate>Tue, 10 Jul 2007 19:02:30 GMT</pubDate><ttl>60</ttl><item><title>Sql Server 常用的全局变量</title><link>http://www.cnitblog.com/ffan/archive/2007/07/09/29664.html</link><dc:creator>ffan</dc:creator><author>ffan</author><pubDate>Mon, 09 Jul 2007 07:15:00 GMT</pubDate><guid>http://www.cnitblog.com/ffan/archive/2007/07/09/29664.html</guid><wfw:comment>http://www.cnitblog.com/ffan/comments/29664.html</wfw:comment><comments>http://www.cnitblog.com/ffan/archive/2007/07/09/29664.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/ffan/comments/commentRss/29664.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ffan/services/trackbacks/29664.html</trackback:ping><description><![CDATA[<span style="COLOR: red">@@IDENTITY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 返回最后插入行的标识列的列值。</span> <br><span style="COLOR: red">＠＠ERROR&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 返回最后执行的Transact-SQL语句的错误代码。没有错误则为零<br></span><span style="COLOR: red">@@ROWCOUNT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 返回受上一语句影响的行数，任何不返回行的语句将这一变量设置为0。</span> <br><span style="COLOR: red">@@DBTS&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 返回当前数据库的时间戳值必须保证数据库中时间戳的值是惟一的。</span> <br><br><span class=t18>@@SERVERNAME&nbsp;&nbsp;&nbsp; : 返回运行SQL Server 2000本地服务器的名称。 <br>@@REMSERVER&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 返回登录记录中记载的远程SQL Server服务器的名称。 <br>＠＠CONNECTIONS : 返回自上次启动SQL Server以来连接或试图连接的次数，用其可让管理人员方便地了解今天所有试图连接服务器的次数。 <br>@@CURSOR_ROWS : 返回最后连接上并打开的游标中当前存在的合格行的数量。 <br>@@VERSION&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 返回SQL Server当前安装的日期、版本和处理器类型。 <br>@@CPU_BUSY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 返回自SQL Server最近一次启动以来CPU的工作时间其单位为毫秒。 <br>@@DATEFIRST&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 返回使用SET DATEFIRST命令而被赋值的DATAFIRST参数值。SET DATEFIRST命令用来指定每周的第一天是星期几。 <br>@@FETCH_STATUS : 返回上一次FETCH语句的状态值。 <br>@@IDLE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 返回自SQL Server最近一次启动以来CPU处于空闭状态的时间长短，单位为毫秒。 <br>@@IO_BUSY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 返回自SQL Server最后一次启动以来CPU执行输入输出操作所花费的时间(毫秒)。 <br>@@LANGID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 返回当前所使用的语言ID值。 <br>@@LANGUAGE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 返回当前使用的语言名称。 <br>@@LOCK_TIMEOUT: 返回当前会话等待锁的时间长短其单位为毫秒。 <br>@@MAX_CONNECTIONS : 返回允许连接到SQL Server的最大连接数目。 <br>@@MAX_PRECISION : 返回decimal 和 numeric数据类型的精确度。 <br>@@NESTLEVEL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 返回当前执行的存储过程的嵌套级数，初始值为0。 <br>@@OPTIONS&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 返回当前SET选项的信息。 <br>@@PACK_RECEIVED : 返回SQL Server通过网络读取的输入包的数目。 <br>@@PACK_SENT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 返回SQL Server写给网络的输出包的数目。 <br>@@PACKET_ERRORS : 返回网络包的错误数目。 <br>@@PROCID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 返回当前存储过程的ID值。 <br>@@SERVICENAME&nbsp;&nbsp; : 返回SQL Server正运行于哪种服务状态之下：如 MS SQLServer、MSDTC、SQLServerAgent。 <br>@@SPID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 返回当前用户处理的服务器处理ID值。 <br>@@TEXTSIZE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 返回SET语句的TEXTSIZE选项值SET语句定义了SELECT语句中text或image。数据类型的最大长度基本单位为字节。 <br>@@TIMETICKS&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 返回每一时钟的微秒数。 <br>@@TOTAL_ERRORS : 返回磁盘读写错误数目。 <br>@@TOTAL_READ&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 返回磁盘读操作的数目。 <br>@@TOTAL_WRITE&nbsp;&nbsp;&nbsp; : 返回磁盘写操作的数目。 <br>@@TRANCOUNT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 返回当前连接中处于激活状态的事务数目。</span> 
<img src ="http://www.cnitblog.com/ffan/aggbug/29664.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ffan/" target="_blank">ffan</a> 2007-07-09 15:15 <a href="http://www.cnitblog.com/ffan/archive/2007/07/09/29664.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>MySQL5.0中文手册（13.1. 数据定义声明） </title><link>http://www.cnitblog.com/ffan/archive/2006/02/20/6736.html</link><dc:creator>ffan</dc:creator><author>ffan</author><pubDate>Mon, 20 Feb 2006 02:15:00 GMT</pubDate><guid>http://www.cnitblog.com/ffan/archive/2006/02/20/6736.html</guid><wfw:comment>http://www.cnitblog.com/ffan/comments/6736.html</wfw:comment><comments>http://www.cnitblog.com/ffan/archive/2006/02/20/6736.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/ffan/comments/commentRss/6736.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ffan/services/trackbacks/6736.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 13.1. 数据定义声明&nbsp;13.1.1.ALTER DATABASE句法&nbsp;ALTER {DATABASE | SCHEMA} [db_name]&nbsp;&nbsp;&nbsp; alter_specification [, alter_specification] ...&nbsp;alter_specification:&nbsp;&nbs...&nbsp;&nbsp;<a href='http://www.cnitblog.com/ffan/archive/2006/02/20/6736.html'>阅读全文</a><img src ="http://www.cnitblog.com/ffan/aggbug/6736.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ffan/" target="_blank">ffan</a> 2006-02-20 10:15 <a href="http://www.cnitblog.com/ffan/archive/2006/02/20/6736.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>MYSQL 实现TOP 功能</title><link>http://www.cnitblog.com/ffan/archive/2006/01/26/6370.html</link><dc:creator>ffan</dc:creator><author>ffan</author><pubDate>Thu, 26 Jan 2006 07:51:00 GMT</pubDate><guid>http://www.cnitblog.com/ffan/archive/2006/01/26/6370.html</guid><wfw:comment>http://www.cnitblog.com/ffan/comments/6370.html</wfw:comment><comments>http://www.cnitblog.com/ffan/archive/2006/01/26/6370.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/ffan/comments/commentRss/6370.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ffan/services/trackbacks/6370.html</trackback:ping><description><![CDATA[MYSQL 里没有top关键字， 但可以利用 <SPAN lang=EN-US style="FONT-FAMILY: ''; mso-bidi-font-size: 9.0pt">LIMIT来实现top 功能， 并且可以取得第N-M之间的记录, 语法如下<BR><BR>select * from table limit n, m<BR><BR></SPAN><img src ="http://www.cnitblog.com/ffan/aggbug/6370.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ffan/" target="_blank">ffan</a> 2006-01-26 15:51 <a href="http://www.cnitblog.com/ffan/archive/2006/01/26/6370.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>最牛X的SQL语句</title><link>http://www.cnitblog.com/ffan/archive/2006/01/17/6213.html</link><dc:creator>ffan</dc:creator><author>ffan</author><pubDate>Tue, 17 Jan 2006 03:25:00 GMT</pubDate><guid>http://www.cnitblog.com/ffan/archive/2006/01/17/6213.html</guid><wfw:comment>http://www.cnitblog.com/ffan/comments/6213.html</wfw:comment><comments>http://www.cnitblog.com/ffan/archive/2006/01/17/6213.html#Feedback</comments><slash:comments>10</slash:comments><wfw:commentRss>http://www.cnitblog.com/ffan/comments/commentRss/6213.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ffan/services/trackbacks/6213.html</trackback:ping><description><![CDATA[SELECT top 1 ooo.ooo_title,cv.apply_id,cv.apply_date,xxx_basic_info1.name,isnull((SELECT top 1 code_cn_value FROM code_value WHERE code_id=40818 and value=xxx_basic_info1.gender),''),xxx_basic_info1.birthday,isnull((SELECT top 1 code_cn_value FROM code_value WHERE code_id=40820 and value=xxx_basic_info1.marital),''),isnull((SELECT top 1 a.code_cn_value FROM code_value a INNER JOIN xxx_value_cust b ON a.value=b.value WHERE a.code_id=40899 and b.cust_id=5569 and b.repeat_id=1 and b.xxx_id=143011050509001379),''),xxx_basic_info1.id_card,xxx_basic_info1.passport,isnull((SELECT top 1 code_cn_value FROM code_value WHERE code_id=40824 and value=xxx_basic_info1.graduate_date),''),isnull((SELECT top 1 a.code_cn_value FROM code_value a INNER JOIN xxx_value_cust b ON a.value=b.value WHERE a.code_id=40900 and b.cust_id=5573 and b.repeat_id=1 and b.xxx_id=143011050509001379),''),xxx_basic_info1.work_date,isnull((SELECT top 1 code_cn_value FROM code_value WHERE code_id=40830 and value=xxx_basic_info1.pr_city_id),''),xxx_basic_info1.pr_oth_city,isnull((SELECT top 1 code_cn_value FROM code_value WHERE code_id=40832 and value=xxx_basic_info1.cur_city_id),''),xxx_basic_info1.cur_oth_city,xxx_basic_info1.exp_city,isnull((SELECT top 1 a.code_cn_value FROM code_value a INNER JOIN xxx_value_cust b ON a.value=b.value WHERE a.code_id=40901 and b.cust_id=5580 and b.repeat_id=1 and b.xxx_id=143011050509001379),''),xxx_basic_info1.dorm_tel,xxx_basic_info1.tel,xxx_basic_info1.mobile,xxx_basic_info1.pager,xxx_basic_info1.email,xxx_basic_info1.address,xxx_basic_info1.postcode,isnull((SELECT top 1 a.code_cn_value FROM code_value a INNER JOIN xxx_value_cust b ON a.value=b.value WHERE a.code_id=40902 and b.cust_id=5588 and b.repeat_id=1 and b.xxx_id=143011050509001379),''),xxx_basic_info1.xxx_intro,isnull((SELECT top 1 value FROM xxx_value_cust WHERE cust_id=5590 and repeat_id=1 and xxx_id=143011050509001379),''),xxx_basic_info1.upload_cv,xxx_degree_info1.start_date,xxx_degree_info1.end_date,isnull((SELECT top 1 code_cn_value FROM code_value WHERE code_id=40847 and value=xxx_degree_info1.school_id),''),xxx_degree_info1.oth_school,isnull((SELECT top 1 code_cn_value FROM code_value WHERE code_id=40849 and value=xxx_degree_info1.faculty),''),xxx_degree_info1.major,isnull((SELECT top 1 code_cn_value FROM code_value WHERE code_id=40851 and value=xxx_degree_info1.degree),''),isnull((SELECT top 1 code_cn_value FROM code_value WHERE code_id=40852 and value=xxx_degree_info1.gpa),''),xxx_degree_info1.courses,xxx_degree_info2.start_date,xxx_degree_info2.end_date,isnull((SELECT top 1 code_cn_value FROM code_value WHERE code_id=40847 and value=xxx_degree_info2.school_id),''),xxx_degree_info2.oth_school,isnull((SELECT top 1 code_cn_value FROM code_value WHERE code_id=40849 and value=xxx_degree_info2.faculty),''),xxx_degree_info2.major,isnull((SELECT top 1 code_cn_value FROM code_value WHERE code_id=40851 and value=xxx_degree_info2.degree),''),isnull((SELECT top 1 code_cn_value FROM code_value WHERE code_id=40852 and value=xxx_degree_info2.gpa),''),xxx_degree_info2.courses,xxx_degree_info3.start_date,xxx_degree_info3.end_date,isnull((SELECT top 1 code_cn_value FROM code_value WHERE code_id=40847 and value=xxx_degree_info3.school_id),''),xxx_degree_info3.oth_school,isnull((SELECT top 1 code_cn_value FROM code_value WHERE code_id=40849 and value=xxx_degree_info3.faculty),''),xxx_degree_info3.major,isnull((SELECT top 1 code_cn_value FROM code_value WHERE code_id=40851 and value=xxx_degree_info3.degree),''),isnull((SELECT top 1 code_cn_value FROM code_value WHERE code_id=40852 and value=xxx_degree_info3.gpa),''),xxx_degree_info3.courses,isnull((SELECT top 1 a.code_cn_value FROM code_value a INNER JOIN xxx_value_cust b ON a.value=b.value WHERE a.code_id=40903 and b.cust_id=5130 and b.repeat_id=1 and b.xxx_id=143011050509001379),''),xxx_edu_info1.award,xxx_edu_info1.activity,isnull((SELECT top 1 code_cn_value FROM code_value WHERE code_id=40859 and value=xxx_skill_info1.eng_cert),''),isnull((SELECT top 1 code_cn_value FROM code_value WHERE code_id=40860 and value=xxx_skill_info1.pass_st),''),xxx_skill_info1.tofel,xxx_skill_info1.gre,xxx_skill_info1.gmat,isnull((SELECT top 1 code_cn_value FROM code_value WHERE code_id=40864 and value=xxx_skill_info1.oral_lv),''),isnull((SELECT top 1 code_cn_value FROM code_value WHERE code_id=40865 and value=xxx_skill_info1.oth_lang),''),xxx_skill_info1.oth_lang_lv,isnull((SELECT top 1 code_cn_value FROM code_value WHERE code_id=40867 and value=xxx_skill_info1.dialect),''),isnull((SELECT top 1 code_cn_value FROM code_value WHERE code_id=40868 and value=xxx_skill_info1.dialect_lv),''),isnull((SELECT top 1 a.code_cn_value FROM code_value a INNER JOIN xxx_value_cust b ON a.value=b.value WHERE a.code_id=40904 and b.cust_id=5143 and b.repeat_id=1 and b.xxx_id=143011050509001379),''),isnull((SELECT top 1 value FROM xxx_value_cust WHERE cust_id=5144 and repeat_id=1 and xxx_id=143011050509001379),''),xxx_skill_info1.oth_certificate,xxx_skill_info1.computer_lang,xxx_skill_info1.oth_skill,xxx_skill_info1.hobby,xxx_project_info1.start_date,isnull((SELECT top 1 value FROM xxx_value_cust WHERE cust_id=5518 and repeat_id=1 and xxx_id=143011050509001379),''),xxx_project_info1.project_name,xxx_project_info1.project_duty,xxx_project_info1.project_desc,xxx_project_info2.start_date,isnull((SELECT top 1 value FROM xxx_value_cust WHERE cust_id=5523 and repeat_id=2 and xxx_id=143011050509001379),''),xxx_project_info2.project_name,xxx_project_info2.project_duty,xxx_project_info2.project_desc,xxx_project_info3.start_date,isnull((SELECT top 1 value FROM xxx_value_cust WHERE cust_id=5528 and repeat_id=3 and xxx_id=143011050509001379),''),xxx_project_info3.project_name,xxx_project_info3.project_duty,xxx_project_info3.project_desc,xxx_project_info4.start_date,isnull((SELECT top 1 value FROM xxx_value_cust WHERE cust_id=5533 and repeat_id=4 and xxx_id=143011050509001379),''),xxx_project_info4.project_name,xxx_project_info4.project_duty,xxx_project_info4.project_desc,xxx_exp_info1.start_date,isnull((SELECT top 1 value FROM xxx_value_cust WHERE cust_id=5538 and repeat_id=1 and xxx_id=143011050509001379),''),xxx_exp_info1.company_name,xxx_exp_info1.ooo_title,isnull((SELECT top 1 code_cn_value FROM code_value WHERE code_id=40885 and value=xxx_exp_info1.ooo_type),''),xxx_exp_info1.reason,xxx_exp_info1.ooo_duty,xxx_exp_info2.start_date,isnull((SELECT top 1 value FROM xxx_value_cust WHERE cust_id=5545 and repeat_id=2 and xxx_id=143011050509001379),''),xxx_exp_info2.company_name,xxx_exp_info2.ooo_title,isnull((SELECT top 1 code_cn_value FROM code_value WHERE code_id=40885 and value=xxx_exp_info2.ooo_type),''),xxx_exp_info2.reason,xxx_exp_info2.ooo_duty,xxx_exp_info3.start_date,isnull((SELECT top 1 value FROM xxx_value_cust WHERE cust_id=5552 and repeat_id=3 and xxx_id=143011050509001379),''),xxx_exp_info3.company_name,xxx_exp_info3.ooo_title,isnull((SELECT top 1 code_cn_value FROM code_value WHERE code_id=40885 and value=xxx_exp_info3.ooo_type),''),xxx_exp_info3.reason,xxx_exp_info3.ooo_duty,xxx_exp_info4.start_date,isnull((SELECT top 1 value FROM xxx_value_cust WHERE cust_id=5559 and repeat_id=4 and xxx_id=143011050509001379),''),xxx_exp_info4.company_name,xxx_exp_info4.ooo_title,isnull((SELECT top 1 code_cn_value FROM code_value WHERE code_id=40885 and value=xxx_exp_info4.ooo_type),''),xxx_exp_info4.reason,xxx_exp_info4.ooo_duty,xxx_refer_info1.name,xxx_refer_info1.relationship,xxx_refer_info1.company,xxx_refer_info1.tel,xxx_refer_info2.name,xxx_refer_info2.relationship,xxx_refer_info2.company,xxx_refer_info2.tel,ibm_question11.question1,ibm_question11.question2,ibm_question11.question3,ibm_question21.question1,ibm_question21.question2,ibm_question21.question3,ibm_question21.question4,ibm_question21.question5 FROM xxx_info cv INNER JOIN ooo_info ooo ON cv.ooo_id=ooo.ooo_id LEFT OUTER JOIN xxx_basic_info xxx_basic_info1 ON cv.xxx_id=xxx_basic_info1.xxx_id and xxx_basic_info1.repeat_id=1 LEFT OUTER JOIN xxx_degree_info xxx_degree_info1 ON cv.xxx_id=xxx_degree_info1.xxx_id and xxx_degree_info1.repeat_id=1 LEFT OUTER JOIN xxx_degree_info xxx_degree_info2 ON cv.xxx_id=xxx_degree_info2.xxx_id and xxx_degree_info2.repeat_id=2 LEFT OUTER JOIN xxx_degree_info xxx_degree_info3 ON cv.xxx_id=xxx_degree_info3.xxx_id and xxx_degree_info3.repeat_id=3 LEFT OUTER JOIN xxx_edu_info xxx_edu_info1 ON cv.xxx_id=xxx_edu_info1.xxx_id and xxx_edu_info1.repeat_id=1 LEFT OUTER JOIN xxx_skill_info xxx_skill_info1 ON cv.xxx_id=xxx_skill_info1.xxx_id and xxx_skill_info1.repeat_id=1 LEFT OUTER JOIN xxx_project_info xxx_project_info1 ON cv.xxx_id=xxx_project_info1.xxx_id and xxx_project_info1.repeat_id=1 LEFT OUTER JOIN xxx_project_info xxx_project_info2 ON cv.xxx_id=xxx_project_info2.xxx_id and xxx_project_info2.repeat_id=2 LEFT OUTER JOIN xxx_project_info xxx_project_info3 ON cv.xxx_id=xxx_project_info3.xxx_id and xxx_project_info3.repeat_id=3 LEFT OUTER JOIN xxx_project_info xxx_project_info4 ON cv.xxx_id=xxx_project_info4.xxx_id and xxx_project_info4.repeat_id=4 LEFT OUTER JOIN xxx_exp_info xxx_exp_info1 ON cv.xxx_id=xxx_exp_info1.xxx_id and xxx_exp_info1.repeat_id=1 LEFT OUTER JOIN xxx_exp_info xxx_exp_info2 ON cv.xxx_id=xxx_exp_info2.xxx_id and xxx_exp_info2.repeat_id=2 LEFT OUTER JOIN xxx_exp_info xxx_exp_info3 ON cv.xxx_id=xxx_exp_info3.xxx_id and xxx_exp_info3.repeat_id=3 LEFT OUTER JOIN xxx_exp_info xxx_exp_info4 ON cv.xxx_id=xxx_exp_info4.xxx_id and xxx_exp_info4.repeat_id=4 LEFT OUTER JOIN xxx_refer_info xxx_refer_info1 ON cv.xxx_id=xxx_refer_info1.xxx_id and xxx_refer_info1.repeat_id=1 LEFT OUTER JOIN xxx_refer_info xxx_refer_info2 ON cv.xxx_id=xxx_refer_info2.xxx_id and xxx_refer_info2.repeat_id=2 LEFT OUTER JOIN ibm_question1 ibm_question11 ON cv.xxx_id=ibm_question11.xxx_id and ibm_question11.repeat_id=1 LEFT OUTER JOIN ibm_question2 ibm_question21 ON cv.xxx_id=ibm_question21.xxx_id and ibm_question21.repeat_id=1 WHERE cv.xxx_id=143011050509001379<BR><BR><IMG height=450 src="http://storage.msn.com/x1pw_6EGuGCxXBNWI5PnaJ9I1JY8SVs1AdxSc3VvTWC42NQNWCu7zEHr3WjDonqJn1PhZ3UvRr-jG8d0CcoofqqOHWFmPVTF8xVCSbrG1hPWSGKrBAQTOGRwB5DN3zvr3nq" width=600><BR>&nbsp;<BR>&nbsp;<BR>&nbsp;<BR>&nbsp;<BR><img src ="http://www.cnitblog.com/ffan/aggbug/6213.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ffan/" target="_blank">ffan</a> 2006-01-17 11:25 <a href="http://www.cnitblog.com/ffan/archive/2006/01/17/6213.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>t-sql 存储过程返回多个记录集（ASP）</title><link>http://www.cnitblog.com/ffan/archive/2005/11/21/4778.html</link><dc:creator>ffan</dc:creator><author>ffan</author><pubDate>Mon, 21 Nov 2005 10:20:00 GMT</pubDate><guid>http://www.cnitblog.com/ffan/archive/2005/11/21/4778.html</guid><wfw:comment>http://www.cnitblog.com/ffan/comments/4778.html</wfw:comment><comments>http://www.cnitblog.com/ffan/archive/2005/11/21/4778.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cnitblog.com/ffan/comments/commentRss/4778.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ffan/services/trackbacks/4778.html</trackback:ping><description><![CDATA[<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">%</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">do</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">while</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">not</SPAN><SPAN style="COLOR: #000000">&nbsp;adoRes.eof<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;Response.write&nbsp;adoRes(</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">#</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;Response.write&nbsp;adoRes(</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">#&lt;br&gt;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;adoRes.movenext<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">loop</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><STRONG><FONT color=#ff1493><SPAN style="COLOR: #0000ff">Set</SPAN><SPAN style="COLOR: #000000">&nbsp;adoRes&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN></FONT></STRONG><SPAN style="COLOR: #000000"><STRONG><FONT color=#ff1493>&nbsp;adoRes.NextRecordset</FONT></STRONG><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top>Response.write&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&lt;br&gt;&lt;br&gt;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">do</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">while</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">not</SPAN><SPAN style="COLOR: #000000">&nbsp;adoRes.eof<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;Response.write&nbsp;adoRes(</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">#</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;Response.write&nbsp;adoRes(</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">#&lt;br&gt;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;adoRes.movenext<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">loop</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top>adoRes.close<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">set</SPAN><SPAN style="COLOR: #000000">&nbsp;adoRes&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">nothing</SPAN><SPAN style="COLOR: #000000">&nbsp;<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top>%</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN></DIV><img src ="http://www.cnitblog.com/ffan/aggbug/4778.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ffan/" target="_blank">ffan</a> 2005-11-21 18:20 <a href="http://www.cnitblog.com/ffan/archive/2005/11/21/4778.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Oracle和SqlServer存储过程的调试、出错处理</title><link>http://www.cnitblog.com/ffan/archive/2005/11/11/4338.html</link><dc:creator>ffan</dc:creator><author>ffan</author><pubDate>Fri, 11 Nov 2005 07:01:00 GMT</pubDate><guid>http://www.cnitblog.com/ffan/archive/2005/11/11/4338.html</guid><wfw:comment>http://www.cnitblog.com/ffan/comments/4338.html</wfw:comment><comments>http://www.cnitblog.com/ffan/archive/2005/11/11/4338.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/ffan/comments/commentRss/4338.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ffan/services/trackbacks/4338.html</trackback:ping><description><![CDATA[来源:CMFU<BR>在大型数据库中，因为开发的需要，经常地需要调用Procedure，开发Procedure在Oracle和Sqlserver上因为语法不同，而有所区别，但是在调试上，都是比较的不容易，尤其是在一些错误处理上。<BR>　　首先说调试：<BR>　　1。对于Oracle的调试，可以借助于第三方的工具，比如Pl/Sql Developer，我在用的版本是6.0.5.926；首先对该Procedure右键处理，添加“add debug information”，然后选择“test”，打开新的测试窗口，在下方对应的输入输出变量地方，添加相应的测试数据，注意：这里的数据输入，不需要引号，输出参数不需要输入然后点击“start debugger”，或者按F9，进行测试，可以选择测试的步骤如“step into”等，然后可以在下面的script窗口看到，中间变量；<BR>　　<BR>　　2。对于SqlServer调试，我还没有找到比较好的第三方工具，目前是采用将中间的变量值或者sql语句插入到另一个表中，或者直接print出来的方式。第一种需要借助 exec('') 方法，注意里面的取变量值的写法，可参考下面的例子：<BR>
<P class=code clear=both><BR>exec('update SupplyplanLack set Completedate = GetDate()<BR>from supplyPlanLack a where '+ @ssTmp +' and <BR>'+@iCompleteQty+' &gt;= (select s.RequestQty * a.RationQty as ReqQty<BR>from Balance a, SupplyPlan s where a.Vehicle = s.Vehicle<BR>and a.Part=s.Part and a.SupplyPlanNo = s.SupplyPlanNo<BR>and '+ @ssTmp +')')</P><BR>　　在print中，需要注意类型的转换，一般是借用 convert(varchar(11),@spNOTo) 方法来实现，否则会提示类型转换错误！<BR><BR>　　3。错误处理上，对于Sqlserver可以采用开始自定义变量，然后根据不同判断，改变该值然后推出的方法来处理，见下例<BR>
<P class=code clear=both><BR>set @exec_num=0<BR><BR>if (@spNOFrom=0 or @spNOTo=0 or @reuseUser=NULL)<BR>begin<BR>set @exec_num=1<BR>goto the_end<BR>end<BR><BR>the_end: <BR>return</P><BR>　　或者是这种：<BR>
<P class=code clear=both><BR>set nocount on<BR><BR>if (@property is null) or (@property = '')<BR>begin<BR>raiserror('Must specify a property name.',-1,-1)<BR>return (1)<BR>end</P><BR>　　或者是对该错误全局变量 @@error 数值的判断上。<img src ="http://www.cnitblog.com/ffan/aggbug/4338.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ffan/" target="_blank">ffan</a> 2005-11-11 15:01 <a href="http://www.cnitblog.com/ffan/archive/2005/11/11/4338.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>SQL SERVER常用函数</title><link>http://www.cnitblog.com/ffan/archive/2005/10/28/3623.html</link><dc:creator>ffan</dc:creator><author>ffan</author><pubDate>Fri, 28 Oct 2005 02:12:00 GMT</pubDate><guid>http://www.cnitblog.com/ffan/archive/2005/10/28/3623.html</guid><wfw:comment>http://www.cnitblog.com/ffan/comments/3623.html</wfw:comment><comments>http://www.cnitblog.com/ffan/archive/2005/10/28/3623.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cnitblog.com/ffan/comments/commentRss/3623.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ffan/services/trackbacks/3623.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;决定还是先帖简单的，时间日期函数1.DATEADD在向指定日期加上一段时间的基础上，返回新的 datetime 值。语法DATEADD ( datepart , number, date ) 参数datepart是规定应向日期的哪一部分返回新值的参数。下表列出了 Microsoft? SQL Server? 识别的日期部分和缩写。日期部分&nbsp;&nbsp;...&nbsp;&nbsp;<a href='http://www.cnitblog.com/ffan/archive/2005/10/28/3623.html'>阅读全文</a><img src ="http://www.cnitblog.com/ffan/aggbug/3623.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ffan/" target="_blank">ffan</a> 2005-10-28 10:12 <a href="http://www.cnitblog.com/ffan/archive/2005/10/28/3623.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>SQL Server CHARINDEX和PATINDEX详解 </title><link>http://www.cnitblog.com/ffan/archive/2005/10/28/3617.html</link><dc:creator>ffan</dc:creator><author>ffan</author><pubDate>Fri, 28 Oct 2005 00:57:00 GMT</pubDate><guid>http://www.cnitblog.com/ffan/archive/2005/10/28/3617.html</guid><wfw:comment>http://www.cnitblog.com/ffan/comments/3617.html</wfw:comment><comments>http://www.cnitblog.com/ffan/archive/2005/10/28/3617.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cnitblog.com/ffan/comments/commentRss/3617.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ffan/services/trackbacks/3617.html</trackback:ping><description><![CDATA[<P><FONT color=#0000ff size=5>SQL Server CHARINDEX和PATINDEX详解</FONT></P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 假如你写过很多程序，你可能偶尔会碰到要确定字符或字符窜串否包含在一段文字中，在这篇文章中，我将讨论使用CHARINDEX和PATINDEX函数来搜索文字列和字符串。我将告诉你这两个函数是如何运转的，解释他们的区别。同时提供一些例子，通过这些例子，你可以可以考虑使用这两个函数来解决很多不同的字符搜索的问题。<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CHARINDEX和PATINDEX函数常常用来在一段字符中搜索字符或者字符串。如果被搜索的字符中包含有要搜索的字符，那么这两个函数返回一个非零的整数，这个整数是要搜索的字符在被搜索的字符中的开始位数。PATINDEX函数支持使用通配符来进行搜索，然而CHARINDEX不支持通佩符。接下来，我们逐个分析这两个函数。</P>
<P><STRONG><FONT color=#0000ff>如何使用CHARINDEX函数</FONT></STRONG></P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CHARINDEX函数返回字符或者字符串在另一个字符串中的起始位置。CHARINDEX函数调用方法如下：</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#b5076f>CHARINDEX ( expression1 , expression2 [ , start_location ] )</FONT></P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Expression1是要到expression2中寻找的字符中，start_location是CHARINDEX函数开始在expression2中找expression1的位置。</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CHARINDEX函数返回一个整数，返回的整数是要找的字符串在被找的字符串中的位置。假如CHARINDEX没有找到要找的字符串，那么函数整数“0”。让我们看看下面的函数命令执行的结果：</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT color=#9c5120>CHARINDEX('SQL', 'Microsoft SQL Server')</FONT></P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 这个函数命令将返回在“Microsoft SQL Server”中“SQL”的起始位置，在这个例子中，CHARINDEX函数将返回“S”在“Microsoft SQL Server”中的位置11。<BR>接下来，我们看这个CHARINDEX命令：</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#9c5120>&nbsp; CHARINDEX('7.0', 'Microsoft SQL Server 2000')</FONT></P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在这个例子中，CHARINDEX返回零，因为字符串“7.0” 不能在“Microsoft SQL Server”中被找到。接下来通过两个例子来看看如何使用CHARINDEX函数来解决实际的T-SQL问题。</P>
<P>&nbsp;&nbsp;&nbsp; &nbsp;第一个例子，假设你要显示Northwind数据库Customer表前5行联系人列的Last Name。这是前5行数据<BR><FONT color=#008800>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;ContactName<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;------------------------------&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Maria Anders<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Ana Trujillo<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Antonio Moreno<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thomas Hardy<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Christina Berglund</FONT></P>
<P>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;你可以看到，CustomName包含客户的First Name和Last Name，它们之间被一个空格隔开。我用CHARINDX函数确定两个名字中间空格的位置。通过这个方法，我们可以分析ContactName列的空格位置，这样我们可以只显示这个列的last name部分。这是显示Northwind的Customer表前5行last name的记录！</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#9c5120> select top 5 substring(ContactName,charindex(' ',ContactName)+1 ,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; len(ContactName)) as [Last Name] from Northwind.dbo.customers</FONT></P>
<P>下面是这个命令输出的结果。<BR><FONT color=#008800>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Last Name<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;------------------------------&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Anders<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Trujillo<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Moreno<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hardy<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Berglund</FONT></P>
<P>&nbsp;&nbsp;&nbsp; &nbsp; CHARINDEX函数找到First Name和Last Name之间的空格，所以SUBSTRING函数可以分开ContactName列，这样就只有Last Name被选出。我在CHARINDEX函数返回的整数上加1，这样Last Name不是从空格开始。</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在第二个例子中，即如说你要计算记录中，某一个字段包含特定字符的所有记录数。CHARINDEX函数可以方便的解决你的问题。计算Northwind.dbo.Customer表中Addresses字段中包含单词Road或者它的缩写Rd的记录数，选择语句类似这样：</P>
<P><FONT color=#9c5120>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SELECT count(*) from Northwind.dbo.Customers <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WHERE CHARINDEX('Rd',Address) &gt; 0 or CHARINDEX('Road',Address)&gt; 1</FONT> </P>
<P><STRONG><FONT color=#0000ff>如何使用PATINDEX函数</FONT></STRONG></P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PATINDEX函数返回字符或者字符串在另一个字符串或者表达式中的起始位置，PATINDEX函数支持搜索字符串中使用通配符，这使PATINDEX函数对于变化的搜索字符串很有价值。PATINDEX函数的命令如下：</P>
<P>&nbsp;&nbsp;&nbsp; &nbsp; <FONT color=#d60883>PATINDEX ( '%pattern%' , expression )</FONT></P>
<P>&nbsp;&nbsp;&nbsp; &nbsp; Pattern是你要搜索的字符串，expression是被搜索的字符串。一般情况下expression是一个表中的一个字段，pattern的前后需要用“%”标记，除非你搜索的字符串在被收缩的字符串的最前面或者最后面。</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 和CHARINDEX函数一样，PATINDEX函数返回搜索字符串在被搜索字符串中的起始位置。假如有这样一个PATINDEX函数：</P>
<P>&nbsp;&nbsp;&nbsp; &nbsp; <FONT color=#9c5120>PATINDEX('%BC%','ABCD')</FONT></P>
<P>&nbsp;&nbsp;&nbsp; &nbsp; 这个PATINDEX函数返回的结果是2，这和CHARINDEX函数一样。这里的%标记告诉PATINDEX函数去找字符串“BC”，不管被搜索的字符串中在“BC”的前后有多少字符！<BR>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;假如你想知道被搜索字符串是否由特定的字符串开始，你可以省去前面的%标记。PATINDED函数就要这样写：</P>
<P>&nbsp;&nbsp;&nbsp; &nbsp; <FONT color=#9c5120>PATINDEX('AB%','ABCD')</FONT></P>
<P>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;这个命令执行的结果返回1，表示搜索的字符串“AB”在被搜索的字符串中“ABCD”被找到。</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 使用通配符可以编辑比我以上举得简单例子复杂得多的搜索字符串。假如说你要确定一个字符串是否包含字母A和Z，还有任何数字，这个PARINDEX函数命令可能像这样：</P>
<P>&nbsp;&nbsp;&nbsp; &nbsp;<FONT color=#9c5120> PATINDEX('%[A,Z,0-9]%[A,Z,0-9]%[A,Z,0-9]%','XYZABC123')</FONT></P>
<P>&nbsp;&nbsp; &nbsp;&nbsp; 注意在上面这个例子中的搜索字符部分使用了很多的通陪符。察看SQL Server联机丛书可以获得更多关于通佩符的信息。接下来，我们用两个例子来看PATINDEX和SELECT怎么联合起来使用。<BR>&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;假设你想要找出Northwind.dbo.Categories表中Description字段中是包含单词“Bread”或“bread”的所有记录，那么选择语句就可能是这样：</P>
<P><FONT color=#9c5120>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SELECT Description from Northwind.dbo.Categories<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WHERE patindex('%[b,B]read%',description) &gt; 0</FONT></P>
<P>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;这里我用通配符来确定大写和小写的“b”。我在Notthwind数据库中执行这个脚本后，得到下面的结果：<BR><FONT color=#008800>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Description<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--------------------------------------------------------<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Desserts, candies, and sweet breads<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Breads, crackers, pasta, and cereal</FONT></P>
<P>&nbsp; &nbsp;&nbsp;&nbsp; 这是再用另外一个额外的通配符来查找一些记录的例子。这个例子是如何选出上面的查询结果中，Description字段的第二子字母不是“e”的纪录。</P>
<P><FONT color=#9c5120>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; select Description from Northwind.dbo.Categories&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; where patindex('%[b,B]read%',description) &gt; 0&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; and patindex('_[^e]%',description) = 1</FONT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </P>
<P>&nbsp; &nbsp;&nbsp;&nbsp; 通过在条件语句中增加一个使用^通配符的PATINDEX函数，我们可以过滤掉“Dessert, candies, and sweet breads”这条记录。上面的查询结果只有一条记录。<BR><FONT color=#008800>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Description<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --------------------------------------------------------<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Breads, crackers, pasta, and cereal</FONT>&nbsp; </P>
<P><STRONG><FONT color=#0909f7>总结</FONT></STRONG></P>
<P>&nbsp;&nbsp;&nbsp; &nbsp; 你现在可以发现CHARINDEX和PATINDEX搜索字符串时的区别了吧。PATINDEX函数支持使用通配符，可以用在很多有变化的查找中。而CHARINDEX不可以。根据你自己不同的情况，这两个函数对你在SQL Server中的字符串的搜索、控制、分析很有帮助。</P>
<P></P>
<P>来源：<A id=ArticleContent1_ArticleContent1_AuthorBlogLink href="http://blog.csdn.net/DigJim/" target=_blank>http://blog.csdn.net/DigJim/</A><BR></P><img src ="http://www.cnitblog.com/ffan/aggbug/3617.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ffan/" target="_blank">ffan</a> 2005-10-28 08:57 <a href="http://www.cnitblog.com/ffan/archive/2005/10/28/3617.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>实现千万级数据的分页显示</title><link>http://www.cnitblog.com/ffan/archive/2005/10/21/3481.html</link><dc:creator>ffan</dc:creator><author>ffan</author><pubDate>Fri, 21 Oct 2005 10:57:00 GMT</pubDate><guid>http://www.cnitblog.com/ffan/archive/2005/10/21/3481.html</guid><wfw:comment>http://www.cnitblog.com/ffan/comments/3481.html</wfw:comment><comments>http://www.cnitblog.com/ffan/archive/2005/10/21/3481.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/ffan/comments/commentRss/3481.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ffan/services/trackbacks/3481.html</trackback:ping><description><![CDATA[<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG id=Codehighlighter1_0_185_Open_Image onclick="this.style.display='none'; Codehighlighter1_0_185_Open_Text.style.display='none'; Codehighlighter1_0_185_Closed_Image.style.display='inline'; Codehighlighter1_0_185_Closed_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_0_185_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_0_185_Closed_Text.style.display='none'; Codehighlighter1_0_185_Open_Image.style.display='inline'; Codehighlighter1_0_185_Open_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ContractedBlock.gif" align=top><SPAN id=Codehighlighter1_0_185_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**/</SPAN><SPAN id=Codehighlighter1_0_185_Open_Text><SPAN style="COLOR: #008080">/*</SPAN><SPAN style="COLOR: #008080"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;经测试，在&nbsp;14483461&nbsp;条记录中查询第&nbsp;100000&nbsp;页，每页&nbsp;10&nbsp;条记录按升序和降序第一次时间均为&nbsp;0.47&nbsp;秒，第二次时间均为&nbsp;0.43&nbsp;秒，测试语法如下：<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;exec&nbsp;GetRecordFromPage&nbsp;news,newsid,10,100000<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;news&nbsp;为&nbsp;表名,&nbsp;newsid&nbsp;为关键字段,&nbsp;使用时请先对&nbsp;newsid&nbsp;建立索引。<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top></SPAN><SPAN style="COLOR: #008080">*/</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top><BR><IMG id=Codehighlighter1_188_532_Open_Image onclick="this.style.display='none'; Codehighlighter1_188_532_Open_Text.style.display='none'; Codehighlighter1_188_532_Closed_Image.style.display='inline'; Codehighlighter1_188_532_Closed_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_188_532_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_188_532_Closed_Text.style.display='none'; Codehighlighter1_188_532_Open_Image.style.display='inline'; Codehighlighter1_188_532_Open_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN id=Codehighlighter1_188_532_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**/</SPAN><SPAN id=Codehighlighter1_188_532_Open_Text><SPAN style="COLOR: #008080">/*</SPAN><SPAN style="COLOR: #008080"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;函数名称:&nbsp;GetRecordFromPage<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;函数功能:&nbsp;获取指定页的数据<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;参数说明:&nbsp;@tblName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;包含数据的表名<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@fldName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;关键字段名<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@PageSize&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;每页记录数<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@PageIndex&nbsp;&nbsp;&nbsp;&nbsp;要获取的页码<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@OrderType&nbsp;&nbsp;&nbsp;&nbsp;排序类型,&nbsp;0&nbsp;-&nbsp;升序,&nbsp;1&nbsp;-&nbsp;降序<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@strWhere&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;查询条件&nbsp;(注意:&nbsp;不要加&nbsp;where)<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;作　　者:&nbsp;铁拳<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;邮　　箱:&nbsp;sunjianhua_kki@sina.com<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;创建时间:&nbsp;2004-07-04<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;修改时间:&nbsp;2004-07-04<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top></SPAN><SPAN style="COLOR: #008080">*/</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">CREATE</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">PROCEDURE</SPAN><SPAN style="COLOR: #000000">&nbsp;GetRecordFromPage<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">@tblName</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #000000">varchar</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #800000">255</SPAN><SPAN style="COLOR: #000000">),&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008080">--</SPAN><SPAN style="COLOR: #008080">&nbsp;表名</SPAN><SPAN style="COLOR: #008080"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">@fldName</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #000000">varchar</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #800000">255</SPAN><SPAN style="COLOR: #000000">),&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008080">--</SPAN><SPAN style="COLOR: #008080">&nbsp;字段名</SPAN><SPAN style="COLOR: #008080"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">@PageSize</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #000000">int</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #808080">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #800000">10</SPAN><SPAN style="COLOR: #000000">,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008080">--</SPAN><SPAN style="COLOR: #008080">&nbsp;页尺寸</SPAN><SPAN style="COLOR: #008080"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">@PageIndex</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #000000">int</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #808080">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1</SPAN><SPAN style="COLOR: #000000">,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008080">--</SPAN><SPAN style="COLOR: #008080">&nbsp;页码</SPAN><SPAN style="COLOR: #008080"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">@OrderType</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #000000">bit</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #808080">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #800000">0</SPAN><SPAN style="COLOR: #000000">,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008080">--</SPAN><SPAN style="COLOR: #008080">&nbsp;设置排序类型,&nbsp;非&nbsp;0&nbsp;值则降序</SPAN><SPAN style="COLOR: #008080"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">@strWhere</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #000000">varchar</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #800000">2000</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN style="COLOR: #808080">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #ff0000">''</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008080">--</SPAN><SPAN style="COLOR: #008080">&nbsp;查询条件&nbsp;(注意:&nbsp;不要加&nbsp;where)</SPAN><SPAN style="COLOR: #008080"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">AS</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">declare</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #008000">@strSQL</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #000000">varchar</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #800000">6000</SPAN><SPAN style="COLOR: #000000">)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008080">--</SPAN><SPAN style="COLOR: #008080">&nbsp;主语句</SPAN><SPAN style="COLOR: #008080"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">declare</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #008000">@strTmp</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #000000">varchar</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1000</SPAN><SPAN style="COLOR: #000000">)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008080">--</SPAN><SPAN style="COLOR: #008080">&nbsp;临时变量</SPAN><SPAN style="COLOR: #008080"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">declare</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #008000">@strOrder</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #000000">varchar</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #800000">500</SPAN><SPAN style="COLOR: #000000">)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008080">--</SPAN><SPAN style="COLOR: #008080">&nbsp;排序类型</SPAN><SPAN style="COLOR: #008080"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #008000">@OrderType</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #808080">!=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #800000">0</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">begin</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">set</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #008000">@strTmp</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #808080">=</SPAN><SPAN style="COLOR: #000000">&nbsp;"</SPAN><SPAN style="COLOR: #808080">&lt;</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #0000ff">select</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #ff00ff">min</SPAN><SPAN style="COLOR: #000000">"<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">set</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #008000">@strOrder</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #808080">=</SPAN><SPAN style="COLOR: #000000">&nbsp;"&nbsp;</SPAN><SPAN style="COLOR: #0000ff">order</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">by</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #ff0000">[</SPAN><SPAN style="COLOR: #ff0000">"&nbsp;+&nbsp;@fldName&nbsp;+"</SPAN><SPAN style="COLOR: #ff0000">]</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">desc</SPAN><SPAN style="COLOR: #000000">"<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">end</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">else</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">begin</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">set</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #008000">@strTmp</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #808080">=</SPAN><SPAN style="COLOR: #000000">&nbsp;"</SPAN><SPAN style="COLOR: #808080">&gt;</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #0000ff">select</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #ff00ff">max</SPAN><SPAN style="COLOR: #000000">"<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">set</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #008000">@strOrder</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #808080">=</SPAN><SPAN style="COLOR: #000000">&nbsp;"&nbsp;</SPAN><SPAN style="COLOR: #0000ff">order</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">by</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #ff0000">[</SPAN><SPAN style="COLOR: #ff0000">"&nbsp;+&nbsp;@fldName&nbsp;+"</SPAN><SPAN style="COLOR: #ff0000">]</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">asc</SPAN><SPAN style="COLOR: #000000">"<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">end</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">set</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #008000">@strSQL</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #808080">=</SPAN><SPAN style="COLOR: #000000">&nbsp;"</SPAN><SPAN style="COLOR: #0000ff">select</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">top</SPAN><SPAN style="COLOR: #000000">&nbsp;"&nbsp;</SPAN><SPAN style="COLOR: #808080">+</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #ff00ff">str</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #008000">@PageSize</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN style="COLOR: #808080">+</SPAN><SPAN style="COLOR: #000000">&nbsp;"&nbsp;</SPAN><SPAN style="COLOR: #808080">*</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">from</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #ff0000">[</SPAN><SPAN style="COLOR: #ff0000">"<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;+&nbsp;@tblName&nbsp;+&nbsp;"</SPAN><SPAN style="COLOR: #ff0000">]</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">where</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #ff0000">[</SPAN><SPAN style="COLOR: #ff0000">"&nbsp;+&nbsp;@fldName&nbsp;+&nbsp;"</SPAN><SPAN style="COLOR: #ff0000">]</SPAN><SPAN style="COLOR: #000000">"&nbsp;</SPAN><SPAN style="COLOR: #808080">+</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #008000">@strTmp</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #808080">+</SPAN><SPAN style="COLOR: #000000">&nbsp;"(</SPAN><SPAN style="COLOR: #ff0000">[</SPAN><SPAN style="COLOR: #ff0000">"<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;+&nbsp;@fldName&nbsp;+&nbsp;"</SPAN><SPAN style="COLOR: #ff0000">]</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN style="COLOR: #0000ff">from</SPAN><SPAN style="COLOR: #000000">&nbsp;(</SPAN><SPAN style="COLOR: #0000ff">select</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">top</SPAN><SPAN style="COLOR: #000000">&nbsp;"&nbsp;</SPAN><SPAN style="COLOR: #808080">+</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #ff00ff">str</SPAN><SPAN style="COLOR: #000000">((</SPAN><SPAN style="COLOR: #008000">@PageIndex</SPAN><SPAN style="COLOR: #808080">-</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #808080">*</SPAN><SPAN style="COLOR: #008000">@PageSize</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN style="COLOR: #808080">+</SPAN><SPAN style="COLOR: #000000">&nbsp;"&nbsp;</SPAN><SPAN style="COLOR: #ff0000">[</SPAN><SPAN style="COLOR: #ff0000">"<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;+&nbsp;@fldName&nbsp;+&nbsp;"</SPAN><SPAN style="COLOR: #ff0000">]</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">from</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #ff0000">[</SPAN><SPAN style="COLOR: #ff0000">"&nbsp;+&nbsp;@tblName&nbsp;+&nbsp;"</SPAN><SPAN style="COLOR: #ff0000">]</SPAN><SPAN style="COLOR: #000000">"&nbsp;</SPAN><SPAN style="COLOR: #808080">+</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #008000">@strOrder</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #808080">+</SPAN><SPAN style="COLOR: #000000">&nbsp;")&nbsp;</SPAN><SPAN style="COLOR: #0000ff">as</SPAN><SPAN style="COLOR: #000000">&nbsp;tblTmp)"<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #808080">+</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #008000">@strOrder</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #008000">@strWhere</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #808080">!=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #ff0000">''</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">set</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #008000">@strSQL</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #808080">=</SPAN><SPAN style="COLOR: #000000">&nbsp;"</SPAN><SPAN style="COLOR: #0000ff">select</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">top</SPAN><SPAN style="COLOR: #000000">&nbsp;"&nbsp;</SPAN><SPAN style="COLOR: #808080">+</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #ff00ff">str</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #008000">@PageSize</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN style="COLOR: #808080">+</SPAN><SPAN style="COLOR: #000000">&nbsp;"&nbsp;</SPAN><SPAN style="COLOR: #808080">*</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">from</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #ff0000">[</SPAN><SPAN style="COLOR: #ff0000">"<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+&nbsp;@tblName&nbsp;+&nbsp;"</SPAN><SPAN style="COLOR: #ff0000">]</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">where</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #ff0000">[</SPAN><SPAN style="COLOR: #ff0000">"&nbsp;+&nbsp;@fldName&nbsp;+&nbsp;"</SPAN><SPAN style="COLOR: #ff0000">]</SPAN><SPAN style="COLOR: #000000">"&nbsp;</SPAN><SPAN style="COLOR: #808080">+</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #008000">@strTmp</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #808080">+</SPAN><SPAN style="COLOR: #000000">&nbsp;"(</SPAN><SPAN style="COLOR: #ff0000">[</SPAN><SPAN style="COLOR: #ff0000">"<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+&nbsp;@fldName&nbsp;+&nbsp;"</SPAN><SPAN style="COLOR: #ff0000">]</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN style="COLOR: #0000ff">from</SPAN><SPAN style="COLOR: #000000">&nbsp;(</SPAN><SPAN style="COLOR: #0000ff">select</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">top</SPAN><SPAN style="COLOR: #000000">&nbsp;"&nbsp;</SPAN><SPAN style="COLOR: #808080">+</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #ff00ff">str</SPAN><SPAN style="COLOR: #000000">((</SPAN><SPAN style="COLOR: #008000">@PageIndex</SPAN><SPAN style="COLOR: #808080">-</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #808080">*</SPAN><SPAN style="COLOR: #008000">@PageSize</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN style="COLOR: #808080">+</SPAN><SPAN style="COLOR: #000000">&nbsp;"&nbsp;</SPAN><SPAN style="COLOR: #ff0000">[</SPAN><SPAN style="COLOR: #ff0000">"<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+&nbsp;@fldName&nbsp;+&nbsp;"</SPAN><SPAN style="COLOR: #ff0000">]</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">from</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #ff0000">[</SPAN><SPAN style="COLOR: #ff0000">"&nbsp;+&nbsp;@tblName&nbsp;+&nbsp;"</SPAN><SPAN style="COLOR: #ff0000">]</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">where</SPAN><SPAN style="COLOR: #000000">&nbsp;"&nbsp;</SPAN><SPAN style="COLOR: #808080">+</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #008000">@strWhere</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #808080">+</SPAN><SPAN style="COLOR: #000000">&nbsp;"&nbsp;"<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #808080">+</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #008000">@strOrder</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #808080">+</SPAN><SPAN style="COLOR: #000000">&nbsp;")&nbsp;</SPAN><SPAN style="COLOR: #0000ff">as</SPAN><SPAN style="COLOR: #000000">&nbsp;tblTmp)&nbsp;</SPAN><SPAN style="COLOR: #808080">and</SPAN><SPAN style="COLOR: #000000">&nbsp;"&nbsp;</SPAN><SPAN style="COLOR: #808080">+</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #008000">@strWhere</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #808080">+</SPAN><SPAN style="COLOR: #000000">&nbsp;"&nbsp;"&nbsp;</SPAN><SPAN style="COLOR: #808080">+</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #008000">@strOrder</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #008000">@PageIndex</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #808080">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">begin</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">set</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #008000">@strTmp</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #808080">=</SPAN><SPAN style="COLOR: #000000">&nbsp;""<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #008000">@strWhere</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #808080">!=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #ff0000">''</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">set</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #008000">@strTmp</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #808080">=</SPAN><SPAN style="COLOR: #000000">&nbsp;"&nbsp;</SPAN><SPAN style="COLOR: #0000ff">where</SPAN><SPAN style="COLOR: #000000">&nbsp;("&nbsp;</SPAN><SPAN style="COLOR: #808080">+</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #008000">@strWhere</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #808080">+</SPAN><SPAN style="COLOR: #000000">&nbsp;")"<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">set</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #008000">@strSQL</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #808080">=</SPAN><SPAN style="COLOR: #000000">&nbsp;"</SPAN><SPAN style="COLOR: #0000ff">select</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">top</SPAN><SPAN style="COLOR: #000000">&nbsp;"&nbsp;</SPAN><SPAN style="COLOR: #808080">+</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #ff00ff">str</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #008000">@PageSize</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN style="COLOR: #808080">+</SPAN><SPAN style="COLOR: #000000">&nbsp;"&nbsp;</SPAN><SPAN style="COLOR: #808080">*</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">from</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #ff0000">[</SPAN><SPAN style="COLOR: #ff0000">"<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+&nbsp;@tblName&nbsp;+&nbsp;"</SPAN><SPAN style="COLOR: #ff0000">]</SPAN><SPAN style="COLOR: #000000">"&nbsp;</SPAN><SPAN style="COLOR: #808080">+</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #008000">@strTmp</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #808080">+</SPAN><SPAN style="COLOR: #000000">&nbsp;"&nbsp;"&nbsp;</SPAN><SPAN style="COLOR: #808080">+</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #008000">@strOrder</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">end</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">exec</SPAN><SPAN style="COLOR: #000000">&nbsp;(</SPAN><SPAN style="COLOR: #008000">@strSQL</SPAN><SPAN style="COLOR: #000000">)<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">GO</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top></SPAN></DIV><BR>本文引用通告地址： <A href="http://blog.csdn.net/Sun_Jianhua/services/trackbacks/33574.aspx">http://blog.csdn.net/Sun_Jianhua/services/trackbacks/33574.aspx</A> <img src ="http://www.cnitblog.com/ffan/aggbug/3481.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ffan/" target="_blank">ffan</a> 2005-10-21 18:57 <a href="http://www.cnitblog.com/ffan/archive/2005/10/21/3481.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>海量数据库的查询优化及分页算法方案</title><link>http://www.cnitblog.com/ffan/archive/2005/10/20/3425.html</link><dc:creator>ffan</dc:creator><author>ffan</author><pubDate>Thu, 20 Oct 2005 05:06:00 GMT</pubDate><guid>http://www.cnitblog.com/ffan/archive/2005/10/20/3425.html</guid><wfw:comment>http://www.cnitblog.com/ffan/comments/3425.html</wfw:comment><comments>http://www.cnitblog.com/ffan/archive/2005/10/20/3425.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cnitblog.com/ffan/comments/commentRss/3425.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ffan/services/trackbacks/3425.html</trackback:ping><description><![CDATA[<DIV>CREATE TABLE [dbo].[TGongwen] (<SPAN>&nbsp;&nbsp;&nbsp; --TGongwen</SPAN>是红头文件表名</DIV>
<DIV><SPAN>&nbsp;&nbsp; [Gid] [int] IDENTITY (1, 1) NOT NULL ,<BR>--</SPAN>本表的<SPAN>id号，也是主键</SPAN></DIV>
<DIV><SPAN>&nbsp;&nbsp; [title] [varchar] (80) COLLATE Chinese_PRC_CI_AS NULL ,&nbsp;<BR>--</SPAN>红头文件的标题</DIV>
<DIV><SPAN>&nbsp;&nbsp; [fariqi] [datetime] NULL ,<BR>--</SPAN>发布日期</DIV>
<DIV><SPAN>&nbsp;&nbsp; [neibuYonghu] [varchar] (70) COLLATE Chinese_PRC_CI_AS NULL ,<BR>--</SPAN>发布用户</DIV>
<DIV><SPAN>&nbsp;&nbsp; [reader] [varchar] (900) COLLATE Chinese_PRC_CI_AS NULL ,</SPAN></DIV>
<DIV>--需要浏览的用户。每个用户中间用分隔符“<SPAN>,”分开</SPAN></DIV>
<DIV>) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]</DIV>
<DIV>GO</DIV>
<DIV>&nbsp;</DIV>
<DIV>下面，我们来往数据库中添加<SPAN>1000万条数据：</SPAN></DIV>
<DIV>declare @i int</DIV>
<DIV>set @i=1</DIV>
<DIV>while @i&lt;=250000</DIV>
<DIV>begin</DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp; insert into Tgongwen(fariqi,neibuyonghu,reader,title) values('2004-2-5','</SPAN>通信科<SPAN>','通信科,办公室,王局长,刘局长,张局长,admin,刑侦支队,特勤支队,交巡警支队,经侦支队,户政科,治安支队,外事科','这是最先的25万条记录')</SPAN></DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp; set @i=@i+1</SPAN></DIV>
<DIV>end</DIV>
<DIV>GO</DIV>
<DIV>&nbsp;</DIV>
<DIV>declare @i int</DIV>
<DIV>set @i=1</DIV>
<DIV>while @i&lt;=250000</DIV>
<DIV>begin</DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp; insert into Tgongwen(fariqi,neibuyonghu,reader,title) values('2004-9-16','</SPAN>办公室<SPAN>','办公室,通信科,王局长,刘局长,张局长,admin,刑侦支队,特勤支队,交巡警支队,经侦支队,户政科,外事科','这是中间的25万条记录')</SPAN></DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp; set @i=@i+1</SPAN></DIV>
<DIV>end</DIV>
<DIV>GO</DIV>
<DIV>&nbsp;</DIV>
<DIV>declare @h int</DIV>
<DIV>set @h=1</DIV>
<DIV>while @h&lt;=100</DIV>
<DIV>begin</DIV>
<DIV>declare @i int</DIV>
<DIV>set @i=2002</DIV>
<DIV>while @i&lt;=2003</DIV>
<DIV>begin</DIV>
<DIV>declare @j int</DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; set @j=0</SPAN></DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while @j&lt;50</SPAN></DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; begin</SPAN></DIV>
<DIV>declare @k int</DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set @k=0</SPAN></DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while @k&lt;50</SPAN></DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; begin</SPAN></DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp; insert into Tgongwen(fariqi,neibuyonghu,reader,title) values(cast(@i as varchar(4))+'-8-15 3:'+cast(@j as varchar(2))+':'+cast(@j as varchar(2)),'</SPAN>通信科<SPAN>','办公室,通信科,王局长,刘局长,张局长,admin,刑侦支队,特勤支队,交巡警支队,经侦支队,户政科,外事科','这是最后的50万条记录')</SPAN></DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; set @k=@k+1</SPAN></DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end</SPAN></DIV>
<DIV>set @j=@j+1</DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end</SPAN></DIV>
<DIV>set @i=@i+1</DIV>
<DIV>end</DIV>
<DIV>set @h=@h+1</DIV>
<DIV>end</DIV>
<DIV>GO</DIV>
<DIV>&nbsp;</DIV>
<DIV>declare @i int</DIV>
<DIV>set @i=1</DIV>
<DIV>while @i&lt;=9000000</DIV>
<DIV>begin</DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp; insert into Tgongwen(fariqi,neibuyonghu,reader,title) values('2004-5-5','</SPAN>通信科<SPAN>','通信科,办公室,王局长,刘局长,张局长,admin,刑侦支队,特勤支队,交巡警支队,经侦支队,户政科,治安支队,外事科','这是最后添加的900万条记录')</SPAN></DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp; set @i=@i+1000000</SPAN></DIV>
<DIV>end</DIV>
<DIV>
<DIV>GO</DIV></DIV>
<DIV>通过以上语句，我们创建了<SPAN>25万条由通信科于2004年2月5日发布的记录，25万条由办公室于2004年9月6日发布的记录，2002年和2003年各100个2500条相同日期、不同分秒的由通信科发布的记录（共50万条），还有由通信科于2004年5月5日发布的900万条记录，合计1000万条。</SPAN></DIV>
<DIV>&nbsp;</DIV>
<DIV>一、因情制宜，建立“适当”的索引</DIV>
<DIV>建立“适当”的索引是实现查询优化的首要前提。</DIV>
<DIV>索引（<SPAN>index）是除表之外另一重要的、用户定义的存储在物理介质上的数据结构。当根据索引码的值搜索数据时，索引提供了对数据的快速访问。事实上，没有索引,数据库也能根据SELECT语句成功地检索到结果，但随着表变得越来越大，使用“适当”的索引的效果就越来越明显。注意，在这句话中，我们用了“适当”这个词，这是因为，如果使用索引时不认真考虑其实现过程，索引既可以提高也会破坏数据库的工作性能。</SPAN></DIV>
<DIV><B>（一）深入浅出理解索引结构</B></DIV>
<DIV>实际上，您可以把索引理解为一种特殊的目录。微软的<SPAN>SQL SERVER提供了两种索引：聚集索引（clustered index，也称聚类索引、簇集索引）和非聚集索引（nonclustered index，也称非聚类索引、非簇集索引）。下面，我们举例来说明一下聚集索引和非聚集索引的区别：</SPAN></DIV>
<DIV>其实，我们的汉语字典的正文本身就是一个聚集索引。比如，我们要查“安”字，就会很自然地翻开字典的前几页，因为“安”的拼音是“<SPAN>an”，而按照拼音排序汉字的字典是以英文字母“a”开头并以“z”结尾的，那么“安”字就自然地排在字典的前部。如果您翻完了所有以“a”开头的部分仍然找不到这个字，那么就说明您的字典中没有这个字；同样的，如果查“张”字，那您也会将您的字典翻到最后部分，因为“张”的拼音是“zhang”。也就是说，字典的正文部分本身就是一个目录，您不需要再去查其他目录来找到您需要找的内容。</SPAN></DIV>
<DIV>我们把这种正文内容本身就是一种按照一定规则排列的目录称为“聚集索引”。</DIV>
<DIV>如果您认识某个字，您可以快速地从自动中查到这个字。但您也可能会遇到您不认识的字，不知道它的发音，这时候，您就不能按照刚才的方法找到您要查的字，而需要去根据“偏旁部首”查到您要找的字，然后根据这个字后的页码直接翻到某页来找到您要找的字。但您结合“部首目录”和“检字表”而查到的字的排序并不是真正的正文的排序方法，比如您查“张”字，我们可以看到在查部首之后的检字表中“张”的页码是<SPAN>672页，检字表中“张”的上面是“驰”字，但页码却是63页，“张”的下面是“弩”字，页面是390页。很显然，这些字并不是真正的分别位于“张”字的上下方，现在您看到的连续的“驰、张、弩”三字实际上就是他们在非聚集索引中的排序，是字典正文中的字在非聚集索引中的映射。我们可以通过这种方式来找到您所需要的字，但它需要两个过程，先找到目录中的结果，然后再翻到您所需要的页码。</SPAN></DIV>
<DIV>我们把这种目录纯粹是目录，正文纯粹是正文的排序方式称为“非聚集索引”。</DIV>
<DIV>通过以上例子，我们可以理解到什么是“聚集索引”和“非聚集索引”。</DIV>
<DIV>进一步引申一下，我们可以很容易的理解：每个表只能有一个聚集索引，因为目录只能按照一种方法进行排序。</DIV>
<DIV><B>（二）何时使用聚集索引或非聚集索引</B></DIV>
<DIV>下面的表总结了何时使用聚集索引或非聚集索引（<B>很重要</B>）。</DIV>
<P>
<TABLE cellSpacing=0 cellPadding=0 border=1>
<TBODY>
<TR>
<TD vAlign=top width=189>
<DIV align=center><B>动作描述</B></DIV></TD>
<TD vAlign=top width=189>
<DIV align=center><B>使用聚集索引</B></DIV></TD>
<TD vAlign=top width=189>
<DIV align=center><B>使用非聚集索引</B></DIV></TD></TR>
<TR>
<TD width=189>
<DIV align=center>列经常被分组排序</DIV></TD>
<TD width=189>
<DIV align=center>应</DIV></TD>
<TD width=189>
<DIV align=center>应</DIV></TD></TR>
<TR>
<TD width=189>
<DIV align=center>返回某范围内的数据</DIV></TD>
<TD width=189>
<DIV align=center>应</DIV></TD>
<TD width=189>
<DIV align=center>不应</DIV></TD></TR>
<TR>
<TD width=189>
<DIV align=center>一个或极少不同值</DIV></TD>
<TD width=189>
<DIV align=center>不应</DIV></TD>
<TD width=189>
<DIV align=center>不应</DIV></TD></TR>
<TR>
<TD width=189>
<DIV align=center>小数目的不同值</DIV></TD>
<TD width=189>
<DIV align=center>应</DIV></TD>
<TD width=189>
<DIV align=center>不应</DIV></TD></TR>
<TR>
<TD width=189>
<DIV align=center>大数目的不同值</DIV></TD>
<TD width=189>
<DIV align=center>不应</DIV></TD>
<TD width=189>
<DIV align=center>应</DIV></TD></TR>
<TR>
<TD width=189>
<DIV align=center>频繁更新的列</DIV></TD>
<TD width=189>
<DIV align=center>不应</DIV></TD>
<TD width=189>
<DIV align=center>应</DIV></TD></TR>
<TR>
<TD width=189>
<DIV align=center>外键列</DIV></TD>
<TD width=189>
<DIV align=center>应</DIV></TD>
<TD width=189>
<DIV align=center>应</DIV></TD></TR>
<TR>
<TD width=189>
<DIV align=center>主键列</DIV></TD>
<TD width=189>
<DIV align=center>应</DIV></TD>
<TD width=189>
<DIV align=center>应</DIV></TD></TR>
<TR>
<TD width=189>
<DIV align=center>频繁修改索引列</DIV></TD>
<TD width=189>
<DIV align=center>不应</DIV></TD>
<TD width=189>
<DIV align=center>应</DIV></TD></TR></TBODY></TABLE></P>
<DIV>事实上，我们可以通过前面聚集索引和非聚集索引的定义的例子来理解上表。如：返回某范围内的数据一项。比如您的某个表有一个时间列，恰好您把聚合索引建立在了该列，这时您查询<SPAN>2004年1月1日至2004年10月1日之间的全部数据时，这个速度就将是很快的，因为您的这本字典正文是按日期进行排序的，聚类索引只需要找到要检索的所有数据中的开头和结尾数据即可；而不像非聚集索引，必须先查到目录中查到每一项数据对应的页码，然后再根据页码查到具体内容。</SPAN></DIV>
<DIV><B>（三）结合实际，谈索引使用的误区</B></DIV>
<DIV>理论的目的是应用。虽然我们刚才列出了何时应使用聚集索引或非聚集索引，但在实践中以上规则却很容易被忽视或不能根据实际情况进行综合分析。下面我们将根据在实践中遇到的实际问题来谈一下索引使用的误区，以便于大家掌握索引建立的方法。</DIV>
<DIV><B>1</B><B>、主键就是聚集索引</B></DIV>
<DIV>这种想法笔者认为是极端错误的，是对聚集索引的一种浪费。虽然<SPAN>SQL SERVER默认是在主键上建立聚集索引的。</SPAN></DIV>
<DIV>通常，我们会在每个表中都建立一个<SPAN>ID列，以区分每条数据，并且这个ID列是自动增大的，步长一般为1。我们的这个办公自动化的实例中的列Gid就是如此。此时，如果我们将这个列设为主键，SQL SERVER会将此列默认为聚集索引。这样做有好处，就是可以让您的数据在数据库中按照ID进行物理排序，但笔者认为这样做意义不大。</SPAN></DIV>
<DIV>显而易见，聚集索引的优势是很明显的，而每个表中只能有一个聚集索引的规则，这使得聚集索引变得更加珍贵。</DIV>
<DIV>从我们前面谈到的聚集索引的定义我们可以看出，使用聚集索引的最大好处就是能够根据查询要求，迅速缩小查询范围，避免全表扫描。在实际应用中，因为<SPAN>ID号是自动生成的，我们并不知道每条记录的ID号，所以我们很难在实践中用ID号来进行查询。这就使让ID号这个主键作为聚集索引成为一种资源浪费。其次，让每个ID号都不同的字段作为聚集索引也不符合“大数目的不同值情况下不应建立聚合索引”规则；当然，这种情况只是针对用户经常修改记录内容，特别是索引项的时候会负作用，但对于查询速度并没有影响。</SPAN></DIV>
<DIV>在办公自动化系统中，无论是系统首页显示的需要用户签收的文件、会议还是用户进行文件查询等任何情况下进行数据查询都离不开字段的是“日期”还有用户本身的“用户名”。</DIV>
<DIV>通常，办公自动化的首页会显示每个用户尚未签收的文件或会议。虽然我们的<SPAN>where语句可以仅仅限制当前用户尚未签收的情况，但如果您的系统已建立了很长时间，并且数据量很大，那么，每次每个用户打开首页的时候都进行一次全表扫描，这样做意义是不大的，绝大多数的用户1个月前的文件都已经浏览过了，这样做只能徒增数据库的开销而已。事实上，我们完全可以让用户打开系统首页时，数据库仅仅查询这个用户近3个月来未阅览的文件，通过“日期”这个字段来限制表扫描，提高查询速度。如果您的办公自动化系统已经建立的2年，那么您的首页显示速度理论上将是原来速度8倍，甚至更快。</SPAN></DIV>
<DIV>在这里之所以提到“理论上”三字，是因为如果您的聚集索引还是盲目地建在<SPAN>ID这个主键上时，您的查询速度是没有这么高的，即使您在“日期”这个字段上建立的索引（非聚合索引）。下面我们就来看一下在1000万条数据量的情况下各种查询的速度表现（3个月内的数据为25万条）：</SPAN></DIV>
<DIV>（<SPAN>1）仅在主键上建立聚集索引，并且不划分时间段：</SPAN></DIV>
<DIV>Select gid,fariqi,neibuyonghu,title from tgongwen</DIV>
<DIV>用时：<SPAN>128470毫秒（即：128秒）</SPAN></DIV>
<DIV>（<SPAN>2）在主键上建立聚集索引，在fariq上建立非聚集索引：</SPAN></DIV>
<DIV>select gid,fariqi,neibuyonghu,title from Tgongwen</DIV>
<DIV>where fariqi&gt; dateadd(day,-90,getdate())</DIV>
<DIV>用时：<SPAN>53763毫秒（54秒）</SPAN></DIV>
<DIV>（<SPAN>3）将聚合索引建立在日期列（fariqi）上：</SPAN></DIV>
<DIV>select gid,fariqi,neibuyonghu,title from Tgongwen</DIV>
<DIV>where fariqi&gt; dateadd(day,-90,getdate())</DIV>
<DIV>用时：<SPAN>2423毫秒（2秒）</SPAN></DIV>
<DIV>虽然每条语句提取出来的都是<SPAN>25万条数据，各种情况的差异却是巨大的，特别是将聚集索引建立在日期列时的差异。事实上，如果您的数据库真的有1000万容量的话，把主键建立在ID列上，就像以上的第1、2种情况，在网页上的表现就是超时，根本就无法显示。这也是我摒弃ID列作为聚集索引的一个最重要的因素。</SPAN></DIV>
<DIV>得出以上速度的方法是：在各个<SPAN>select语句前加：declare @d datetime</SPAN></DIV>
<DIV>set @d=getdate()</DIV>
<DIV>并在<SPAN>select语句后加：</SPAN></DIV>
<DIV>select [语句执行花费时间<SPAN>(毫秒)]=datediff(ms,@d,getdate())</SPAN></DIV>
<DIV><B>2</B><B>、只要建立索引就能显著提高查询速度</B></DIV>
<DIV>事实上，我们可以发现上面的例子中，第<SPAN>2、3条语句完全相同，且建立索引的字段也相同；不同的仅是前者在fariqi字段上建立的是非聚合索引，后者在此字段上建立的是聚合索引，但查询速度却有着天壤之别。所以，并非是在任何字段上简单地建立索引就能提高查询速度。</SPAN></DIV>
<DIV>从建表的语句中，我们可以看到这个有着<SPAN>1000万数据的表中fariqi字段有5003个不同记录。在此字段上建立聚合索引是再合适不过了。在现实中，我们每天都会发几个文件，这几个文件的发文日期就相同，这完全符合建立聚集索引要求的：“既不能绝大多数都相同，又不能只有极少数相同”的规则。由此看来，我们建立“适当”的聚合索引对于我们提高查询速度是非常重要的。</SPAN></DIV>
<DIV><B>3</B><B>、把所有需要提高查询速度的字段都加进聚集索引，以提高查询速度</B></DIV>
<DIV>上面已经谈到：在进行数据查询时都离不开字段的是“日期”还有用户本身的“用户名”。既然这两个字段都是如此的重要，我们可以把他们合并起来，建立一个复合索引（<SPAN>compound index）。</SPAN></DIV>
<DIV>很多人认为只要把任何字段加进聚集索引，就能提高查询速度，也有人感到迷惑：如果把复合的聚集索引字段分开查询，那么查询速度会减慢吗？带着这个问题，我们来看一下以下的查询速度（结果集都是<SPAN>25万条数据）：（日期列fariqi首先排在复合聚集索引的起始列，用户名neibuyonghu排在后列）</SPAN></DIV>
<DIV>（<SPAN>1）select gid,fariqi,neibuyonghu,title from Tgongwen where fariqi&gt;'2004-5-5' </SPAN></DIV>
<DIV>查询速度：<SPAN>2513毫秒</SPAN></DIV>
<DIV>（<SPAN>2）select gid,fariqi,neibuyonghu,title from Tgongwen where fariqi&gt;'2004-5-5' and neibuyonghu='办公室'</SPAN></DIV>
<DIV>查询速度：<SPAN>2516毫秒</SPAN></DIV>
<DIV>（<SPAN>3）select gid,fariqi,neibuyonghu,title from Tgongwen where neibuyonghu='办公室'</SPAN></DIV>
<DIV>查询速度：<SPAN>60280毫秒</SPAN></DIV>
<DIV>从以上试验中，我们可以看到如果仅用聚集索引的起始列作为查询条件和同时用到复合聚集索引的全部列的查询速度是几乎一样的，甚至比用上全部的复合索引列还要略快（在查询结果集数目一样的情况下）；而如果仅用复合聚集索引的非起始列作为查询条件的话，这个索引是不起任何作用的。当然，语句<SPAN>1、2的查询速度一样是因为查询的条目数一样，如果复合索引的所有列都用上，而且查询结果少的话，这样就会形成“索引覆盖”，因而性能可以达到最优。同时，请记住：无论您是否经常使用聚合索引的其他列，但其前导列一定要是使用最频繁的列。</SPAN></DIV>
<DIV><B>（四）其他书上没有的索引使用经验总结</B></DIV>
<DIV><B>1</B><B>、用聚合索引比用不是聚合索引的主键速度快</B></DIV>
<DIV>下面是实例语句：（都是提取<SPAN>25万条数据）</SPAN></DIV>
<DIV>select gid,fariqi,neibuyonghu,reader,title from Tgongwen where fariqi='2004-9-16'</DIV>
<DIV>使用时间：<SPAN>3326毫秒</SPAN></DIV>
<DIV>select gid,fariqi,neibuyonghu,reader,title from Tgongwen where gid&lt;=250000</DIV>
<DIV>使用时间：<SPAN>4470毫秒</SPAN></DIV>
<DIV>这里，用聚合索引比用不是聚合索引的主键速度快了近<SPAN>1/4。</SPAN></DIV>
<DIV><B>2</B><B>、用聚合索引比用一般的主键作<SPAN>order by时速度快，特别是在小数据量情况下</SPAN></B></DIV>
<DIV>select gid,fariqi,neibuyonghu,reader,title from Tgongwen order by fariqi</DIV>
<DIV>用时：<SPAN>12936</SPAN></DIV>
<DIV>select gid,fariqi,neibuyonghu,reader,title from Tgongwen order by gid</DIV>
<DIV>用时：<SPAN>18843</SPAN></DIV>
<DIV>这里，用聚合索引比用一般的主键作<SPAN>order by时，速度快了3/10。事实上，如果数据量很小的话，用聚集索引作为排序列要比使用非聚集索引速度快得明显的多；而数据量如果很大的话，如10万以上，则二者的速度差别不明显。</SPAN></DIV>
<DIV><B>3</B><B>、使用聚合索引内的时间段，搜索时间会按数据占整个数据表的百分比成比例减少，而无论聚合索引使用了多少个</B></DIV>
<DIV>select gid,fariqi,neibuyonghu,reader,title from Tgongwen where fariqi&gt;'2004-1-1'</DIV>
<DIV>用时：<SPAN>6343毫秒（提取100万条） </SPAN></DIV>
<DIV>select gid,fariqi,neibuyonghu,reader,title from Tgongwen where fariqi&gt;'2004-6-6'</DIV>
<DIV>用时：<SPAN>3170毫秒（提取50万条）</SPAN></DIV>
<DIV>select gid,fariqi,neibuyonghu,reader,title from Tgongwen where fariqi='2004-9-16'</DIV>
<DIV>用时：<SPAN>3326毫秒（和上句的结果一模一样。如果采集的数量一样，那么用大于号和等于号是一样的）</SPAN></DIV>
<DIV>select gid,fariqi,neibuyonghu,reader,title from Tgongwen where fariqi&gt;'2004-1-1' and fariqi&lt;'2004-6-6'</DIV>
<DIV>用时：<SPAN>3280毫秒</SPAN></DIV>
<DIV><B>4 </B><B>、日期列不会因为有分秒的输入而减慢查询速度</B></DIV>
<DIV>下面的例子中，共有<SPAN>100万条数据，2004年1月1日以后的数据有50万条，但只有两个不同的日期，日期精确到日；之前有数据50万条，有5000个不同的日期，日期精确到秒。</SPAN></DIV>
<DIV>select gid,fariqi,neibuyonghu,reader,title from Tgongwen where fariqi&gt;'2004-1-1' order by fariqi</DIV>
<DIV>用时：<SPAN>6390毫秒</SPAN></DIV>
<DIV>select gid,fariqi,neibuyonghu,reader,title from Tgongwen where fariqi&lt;'2004-1-1' order by fariqi</DIV>
<DIV>用时：<SPAN>6453毫秒</SPAN></DIV>
<DIV><B>（五）其他注意事项</B></DIV>
<DIV>“水可载舟，亦可覆舟”，索引也一样。索引有助于提高检索性能，但过多或不当的索引也会导致系统低效。因为用户在表中每加进一个索引，数据库就要做更多的工作。过多的索引甚至会导致索引碎片。</DIV>
<DIV>所以说，我们要建立一个“适当”的索引体系，特别是对聚合索引的创建，更应精益求精，以使您的数据库能得到高性能的发挥。</DIV>
<DIV>当然，在实践中，作为一个尽职的数据库管理员，您还要多测试一些方案，找出哪种方案效率最高、最为有效。</DIV>
<DIV>&nbsp;</DIV>
<DIV>二、改善<SPAN>SQL语句</SPAN></DIV>
<DIV>很多人不知道<SPAN>SQL语句在SQL SERVER中是如何执行的，他们担心自己所写的SQL语句会被SQL SERVER误解。比如：</SPAN></DIV>
<DIV>select * from table1 where name='zhangsan' and tID &gt; 10000</DIV>
<DIV>和执行<SPAN>:</SPAN></DIV>
<DIV>select * from table1 where tID &gt; 10000 and name='zhangsan'</DIV>
<DIV>一些人不知道以上两条语句的执行效率是否一样，因为如果简单的从语句先后上看，这两个语句的确是不一样，如果<SPAN>tID是一个聚合索引，那么后一句仅仅从表的10000条以后的记录中查找就行了；而前一句则要先从全表中查找看有几个name='zhangsan'的，而后再根据限制条件条件tID&gt;10000来提出查询结果。</SPAN></DIV>
<DIV>事实上，这样的担心是不必要的。<SPAN>SQL SERVER中有一个“查询分析优化器”，它可以计算出where子句中的搜索条件并确定哪个索引能缩小表扫描的搜索空间，也就是说，它能实现自动优化。</SPAN></DIV>
<DIV>虽然查询优化器可以根据<SPAN>where子句自动的进行查询优化，但大家仍然有必要了解一下“查询优化器”的工作原理，如非这样，有时查询优化器就会不按照您的本意进行快速查询。</SPAN></DIV>
<DIV>在查询分析阶段，查询优化器查看查询的每个阶段并决定限制需要扫描的数据量是否有用。如果一个阶段可以被用作一个扫描参数（<SPAN>SARG），那么就称之为可优化的，并且可以利用索引快速获得所需数据。</SPAN></DIV>
<DIV>SARG的定义：用于限制搜索的一个操作，因为它通常是指一个特定的匹配，一个值得范围内的匹配或者两个以上条件的<SPAN>AND连接。形式如下：</SPAN></DIV>
<DIV>列名 操作符<SPAN> &lt;常数 或 变量&gt;</SPAN></DIV>
<DIV>或</DIV>
<DIV>&lt;常数 或 变量<SPAN>&gt; 操作符列名</SPAN></DIV>
<DIV>列名可以出现在操作符的一边，而常数或变量出现在操作符的另一边。如：</DIV>
<DIV>Name=’张三’</DIV>
<DIV>价格<SPAN>&gt;5000</SPAN></DIV>
<DIV>5000&lt;价格</DIV>
<DIV>Name=’张三’ and 价格<SPAN>&gt;5000</SPAN></DIV>
<DIV>如果一个表达式不能满足<SPAN>SARG的形式，那它就无法限制搜索的范围了，也就是SQL SERVER必须对每一行都判断它是否满足WHERE子句中的所有条件。所以一个索引对于不满足SARG形式的表达式来说是无用的。</SPAN></DIV>
<DIV>介绍完<SPAN>SARG后，我们来总结一下使用SARG以及在实践中遇到的和某些资料上结论不同的经验：</SPAN></DIV>
<DIV><B>1</B><B>、<SPAN>Like语句是否属于SARG取决于所使用的通配符的类型</SPAN></B></DIV>
<DIV>如：<SPAN>name like </SPAN>‘张<SPAN>%</SPAN>’ ，这就属于<SPAN>SARG</SPAN></DIV>
<DIV>而：<SPAN>name like </SPAN>‘%张’ ,就不属于<SPAN>SARG。</SPAN></DIV>
<DIV>原因是通配符<SPAN>%在字符串的开通使得索引无法使用。</SPAN></DIV>
<DIV><B>2</B><B>、<SPAN>or 会引起全表扫描</SPAN></B></DIV>
<DIV>Name=’张三’ and 价格<SPAN>&gt;5000 符号SARG，而：Name=</SPAN>’张三’ or 价格<SPAN>&gt;5000 则不符合SARG。使用or会引起全表扫描。</SPAN></DIV>
<DIV><B>3</B><B>、非操作符、函数引起的不满足<SPAN>SARG形式的语句</SPAN></B></DIV>
<DIV>不满足<SPAN>SARG形式的语句最典型的情况就是包括非操作符的语句，如：NOT、!=、&lt;&gt;、!&lt;、!&gt;、NOT EXISTS、NOT IN、NOT LIKE等，另外还有函数。下面就是几个不满足SARG形式的例子：</SPAN></DIV>
<DIV>ABS(价格<SPAN>)&lt;5000</SPAN></DIV>
<DIV>Name like ‘%三’</DIV>
<DIV>有些表达式，如：</DIV>
<DIV>WHERE 价格<SPAN>*2&gt;5000</SPAN></DIV>
<DIV>SQL SERVER也会认为是<SPAN>SARG，SQL SERVER会将此式转化为：</SPAN></DIV>
<DIV>WHERE 价格<SPAN>&gt;2500/2</SPAN></DIV>
<DIV>但我们不推荐这样使用，因为有时<SPAN>SQL SERVER不能保证这种转化与原始表达式是完全等价的。</SPAN></DIV>
<DIV><B>4</B><B>、<SPAN>IN 的作用相当与OR</SPAN></B></DIV>
<DIV>语句：</DIV>
<DIV>Select * from table1 where tid in (2,3)</DIV>
<DIV>和</DIV>
<DIV>Select * from table1 where tid=2 or tid=3</DIV>
<DIV>是一样的，都会引起全表扫描，如果<SPAN>tid上有索引，其索引也会失效。</SPAN></DIV>
<DIV><B>5</B><B>、尽量少用<SPAN>NOT</SPAN></B></DIV>
<DIV><B>6</B><B>、<SPAN>exists 和 in 的执行效率是一样的</SPAN></B></DIV>
<DIV>很多资料上都显示说，<SPAN>exists要比in的执行效率要高，同时应尽可能的用not exists来代替not in。但事实上，我试验了一下，发现二者无论是前面带不带not，二者之间的执行效率都是一样的。因为涉及子查询，我们试验这次用SQL SERVER自带的pubs数据库。运行前我们可以把SQL SERVER的statistics I/O状态打开。</SPAN></DIV>
<DIV>（<SPAN>1）select title,price from titles where title_id in (select title_id from sales where qty&gt;30)</SPAN></DIV>
<DIV>
<DIV>该句的执行结果为：</DIV></DIV>
<DIV>表<SPAN> 'sales'。扫描计数 18，逻辑读 56 次，物理读 0 次，预读 0 次。</SPAN></DIV>
<DIV>
<DIV>表<SPAN> 'titles'。扫描计数 1，逻辑读 2 次，物理读 0 次，预读 0 次。</SPAN></DIV></DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp; </SPAN></DIV>
<DIV>（<SPAN>2）select title,price from titles where exists (select * from sales where sales.title_id=titles.title_id and qty&gt;30)</SPAN></DIV>
<DIV>
<DIV>第二句的执行结果为：</DIV></DIV>
<DIV>表<SPAN> 'sales'。扫描计数 18，逻辑读 56 次，物理读 0 次，预读 0 次。</SPAN></DIV>
<DIV>
<DIV>表<SPAN> 'titles'。扫描计数 1，逻辑读 2 次，物理读 0 次，预读 0 次。</SPAN></DIV></DIV>
<DIV>我们从此可以看到用<SPAN>exists和用in的执行效率是一样的。</SPAN></DIV>
<DIV><B>7</B><B>、用函数<SPAN>charindex()和前面加通配符%的LIKE执行效率一样</SPAN></B></DIV>
<DIV>前面，我们谈到，如果在<SPAN>LIKE前面加上通配符%，那么将会引起全表扫描，所以其执行效率是低下的。但有的资料介绍说，用函数charindex()来代替LIKE速度会有大的提升，经我试验，发现这种说明也是错误的：</SPAN></DIV>
<DIV>select gid,title,fariqi,reader from tgongwen where charindex('刑侦支队<SPAN>',reader)&gt;0 and fariqi&gt;'2004-5-5'</SPAN></DIV>
<DIV>用时：<SPAN>7秒，另外：扫描计数 4，逻辑读 7155 次，物理读 0 次，预读 0 次。</SPAN></DIV>
<DIV>select gid,title,fariqi,reader from tgongwen where reader like '%' + '刑侦支队<SPAN>' + '%' and fariqi&gt;'2004-5-5'</SPAN></DIV>
<DIV>用时：<SPAN>7秒，另外：扫描计数 4，逻辑读 7155 次，物理读 0 次，预读 0 次。</SPAN></DIV>
<DIV><B>8</B><B>、<SPAN>union并不绝对比or的执行效率高</SPAN></B></DIV>
<DIV>我们前面已经谈到了在<SPAN>where子句中使用or会引起全表扫描，一般的，我所见过的资料都是推荐这里用union来代替or。事实证明，这种说法对于大部分都是适用的。</SPAN></DIV>
<DIV>select gid,fariqi,neibuyonghu,reader,title from Tgongwen where fariqi='2004-9-16' or gid&gt;9990000</DIV>
<DIV>用时：<SPAN>68秒。扫描计数 1，逻辑读 404008 次，物理读 283 次，预读 392163 次。</SPAN></DIV>
<DIV>select gid,fariqi,neibuyonghu,reader,title from Tgongwen where fariqi='2004-9-16' </DIV>
<DIV>union</DIV>
<DIV>select gid,fariqi,neibuyonghu,reader,title from Tgongwen where gid&gt;9990000</DIV>
<DIV>用时：<SPAN>9秒。扫描计数 8，逻辑读 67489 次，物理读 216 次，预读 7499 次。</SPAN></DIV>
<DIV>看来，用<SPAN>union在通常情况下比用or的效率要高的多。</SPAN></DIV>
<DIV>但经过试验，笔者发现如果<SPAN>or两边的查询列是一样的话，那么用union则反倒和用or的执行速度差很多，虽然这里union扫描的是索引，而or扫描的是全表。</SPAN></DIV>
<DIV>select gid,fariqi,neibuyonghu,reader,title from Tgongwen where fariqi='2004-9-16' or fariqi='2004-2-5'</DIV>
<DIV>用时：<SPAN>6423毫秒。扫描计数 2，逻辑读 14726 次，物理读 1 次，预读 7176 次。</SPAN></DIV>
<DIV>select gid,fariqi,neibuyonghu,reader,title from Tgongwen where fariqi='2004-9-16' </DIV>
<DIV>union</DIV>
<DIV>select gid,fariqi,neibuyonghu,reader,title from Tgongwen where&nbsp;fariqi='2004-2-5'</DIV>
<DIV>用时：<SPAN>11640毫秒。扫描计数 8，逻辑读 14806 次，物理读 108 次，预读 1144 次。</SPAN></DIV>
<DIV><B>9</B><B>、字段提取要按照“需多少、提多少”的原则，避免“<SPAN>select *”</SPAN></B></DIV>
<DIV>我们来做一个试验：</DIV>
<DIV>select top 10000 gid,fariqi,reader,title from tgongwen order by gid desc</DIV>
<DIV>用时：<SPAN>4673毫秒</SPAN></DIV>
<DIV>select top 10000 gid,fariqi,title from tgongwen order by gid desc</DIV>
<DIV>用时：<SPAN>1376毫秒</SPAN></DIV>
<DIV>select top 10000 gid,fariqi from tgongwen order by gid desc</DIV>
<DIV>用时：<SPAN>80毫秒</SPAN></DIV>
<DIV>由此看来，我们每少提取一个字段，数据的提取速度就会有相应的提升。提升的速度还要看您舍弃的字段的大小来判断。</DIV>
<DIV><B>10</B><B>、<SPAN>count(*)不比count(字段)慢</SPAN></B></DIV>
<DIV>某些资料上说：用<SPAN>*会统计所有列，显然要比一个世界的列名效率低。这种说法其实是没有根据的。我们来看：</SPAN></DIV>
<DIV>select count(*) from Tgongwen</DIV>
<DIV>用时：<SPAN>1500毫秒</SPAN></DIV>
<DIV>select count(gid) from Tgongwen </DIV>
<DIV>用时：<SPAN>1483毫秒</SPAN></DIV>
<DIV>select count(fariqi) from Tgongwen</DIV>
<DIV>用时：<SPAN>3140毫秒</SPAN></DIV>
<DIV>select count(title) from Tgongwen</DIV>
<DIV>用时：<SPAN>52050毫秒</SPAN></DIV>
<DIV>从以上可以看出，如果用<SPAN>count(*)和用count(主键)的速度是相当的，而count(*)却比其他任何除主键以外的字段汇总速度要快，而且字段越长，汇总的速度就越慢。我想，如果用count(*)， SQL SERVER可能会自动查找最小字段来汇总的。当然，如果您直接写count(主键)将会来的更直接些。</SPAN></DIV>
<DIV><B>11</B><B>、<SPAN>order by按聚集索引列排序效率最高</SPAN></B></DIV>
<DIV>我们来看：（<SPAN>gid是主键，fariqi是聚合索引列）</SPAN></DIV>
<DIV>select top 10000 gid,fariqi,reader,title from tgongwen</DIV>
<DIV>用时：<SPAN>196 毫秒。 扫描计数 1，逻辑读 289 次，物理读 1 次，预读 1527 次。</SPAN></DIV>
<DIV>select top 10000 gid,fariqi,reader,title from tgongwen order by gid asc</DIV>
<DIV>用时：<SPAN>4720毫秒。 扫描计数 1，逻辑读 41956 次，物理读 0 次，预读 1287 次。</SPAN></DIV>
<DIV>select top 10000 gid,fariqi,reader,title from tgongwen order by gid desc</DIV>
<DIV>用时：<SPAN>4736毫秒。 扫描计数 1，逻辑读 55350 次，物理读 10 次，预读 775 次。</SPAN></DIV>
<DIV>select top 10000 gid,fariqi,reader,title from tgongwen order by fariqi asc</DIV>
<DIV>用时：<SPAN>173毫秒。 扫描计数 1，逻辑读 290 次，物理读 0 次，预读 0 次。</SPAN></DIV>
<DIV>select top 10000 gid,fariqi,reader,title from tgongwen order by fariqi desc</DIV>
<DIV>用时：<SPAN>156毫秒。 扫描计数 1，逻辑读 289 次，物理读 0 次，预读 0 次。</SPAN></DIV>
<DIV>从以上我们可以看出，不排序的速度以及逻辑读次数都是和“<SPAN>order by 聚集索引列” 的速度是相当的，但这些都比“order by 非聚集索引列”的查询速度是快得多的。</SPAN></DIV>
<DIV>同时，按照某个字段进行排序的时候，无论是正序还是倒序，速度是基本相当的。</DIV>
<DIV><B>12</B><B>、高效的<SPAN>TOP</SPAN></B></DIV>
<DIV>事实上，在查询和提取超大容量的数据集时，影响数据库响应时间的最大因素不是数据查找，而是物理的<SPAN>I/0操作。如：</SPAN></DIV>
<DIV>select top 10 * from (</DIV>
<DIV>select top 10000 gid,fariqi,title from tgongwen</DIV>
<DIV>where neibuyonghu='办公室<SPAN>'</SPAN></DIV>
<DIV>order by gid desc) as a</DIV>
<DIV>order by gid asc</DIV>
<DIV>这条语句，从理论上讲，整条语句的执行时间应该比子句的执行时间长，但事实相反。因为，子句执行后返回的是<SPAN>10000条记录，而整条语句仅返回10条语句，所以影响数据库响应时间最大的因素是物理I/O操作。而限制物理I/O操作此处的最有效方法之一就是使用TOP关键词了。TOP关键词是SQL SERVER中经过系统优化过的一个用来提取前几条或前几个百分比数据的词。经笔者在实践中的应用，发现TOP确实很好用，效率也很高。但这个词在另外一个大型数据库ORACLE中却没有，这不能说不是一个遗憾，虽然在ORACLE中可以用其他方法（如：rownumber）来解决。在以后的关于“实现千万级数据的分页显示存储过程”的讨论中，我们就将用到TOP这个关键词。</SPAN></DIV>
<DIV>&nbsp;</DIV>
<DIV>到此为止，我们上面讨论了如何实现从大容量的数据库中快速地查询出您所需要的数据方法。当然，我们介绍的这些方法都是“软”方法，在实践中，我们还要考虑各种“硬”因素，如：网络性能、服务器的性能、操作系统的性能，甚至网卡、交换机等。</DIV>
<DIV>&nbsp;</DIV>
<DIV>三、实现小数据量和海量数据的通用分页显示存储过程</DIV>
<DIV>建立一个<SPAN>web 应用，分页浏览功能必不可少。这个问题是数据库处理中十分常见的问题。经典的数据分页方法是:ADO</SPAN> 纪录集分页法，也就是利用<SPAN>ADO自带的分页功能（利用游标）来实现分页。但这种分页方法仅适用于较小数据量的情形，因为游标本身有缺点：游标是存放在内存中，很费内存。游标一建立，就将相关的记录锁住，直到取消游标。游标提供了对特定集合中逐行扫描的手段，一般使用游标来逐行遍历数据，根据取出数据条件的不同进行不同的操作。而对于多表和大表中定义的游标（大的数据集合）循环很容易使程序进入一个漫长的等待甚至死机。</SPAN></DIV>
<DIV>更重要的是，对于非常大的数据模型而言，分页检索时，如果按照传统的每次都加载整个数据源的方法是非常浪费资源的。现在流行的分页方法一般是检索页面大小的块区的数据，而非检索所有的数据，然后单步执行当前行。</DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp; </SPAN>最早较好地实现这种根据页面大小和页码来提取数据的方法大概就是“俄罗斯存储过程”。这个存储过程用了游标，由于游标的局限性，所以这个方法并没有得到大家的普遍认可。</DIV>
<DIV>
<DIV>后来，网上有人改造了此存储过程，下面的存储过程就是结合我们的办公自动化实例写的分页存储过程：</DIV></DIV>
<DIV>CREATE procedure pagination1</DIV>
<DIV>(@pagesize int,&nbsp;--页面大小，如每页存储<SPAN>20条记录</SPAN></DIV>
<DIV>@pageindex int<SPAN>&nbsp;&nbsp; --</SPAN>当前页码</DIV>
<DIV>)</DIV>
<DIV>as</DIV>
<DIV>set nocount on</DIV>
<DIV>begin</DIV>
<DIV>declare @indextable table(id int identity(1,1),nid int)&nbsp;--定义表变量</DIV>
<DIV>declare @PageLowerBound int&nbsp;--定义此页的底码</DIV>
<DIV>declare @PageUpperBound int&nbsp;--定义此页的顶码</DIV>
<DIV>set @PageLowerBound=(@pageindex-1)*@pagesize</DIV>
<DIV>set @PageUpperBound=@PageLowerBound+@pagesize</DIV>
<DIV>set rowcount @PageUpperBound</DIV>
<DIV>insert into @indextable(nid) select gid from TGongwen where fariqi &gt;dateadd(day,-365,getdate()) order by fariqi desc</DIV>
<DIV>select O.gid,O.mid,O.title,O.fadanwei,O.fariqi from TGongwen O,@indextable t where O.gid=t.nid</DIV>
<DIV>and t.id&gt;@PageLowerBound and t.id&lt;=@PageUpperBound order by t.id</DIV>
<DIV>end</DIV>
<DIV>
<DIV>set nocount off</DIV></DIV>
<DIV>以上存储过程运用了<SPAN>SQL SERVER的最新技术</SPAN>――表变量。应该说这个存储过程也是一个非常优秀的分页存储过程。当然，在这个过程中，您也可以把其中的表变量写成临时表：<SPAN>CREATE TABLE #Temp。但很明显，在SQL SERVER中，用临时表是没有用表变量快的。所以笔者刚开始使用这个存储过程时，感觉非常的不错，速度也比原来的ADO的好。但后来，我又发现了比此方法更好的方法。</SPAN></DIV>
<DIV>
<DIV>笔者曾在网上看到了一篇小短文《<SPAN>从数据表中取出第<SPAN>n条到第m条的记录的方法》，全文如下：</SPAN></SPAN></DIV></DIV>
<DIV>从<SPAN>publish 表中取出第 n 条到第 m 条的记录： <BR>SELECT TOP m-n+1 * <BR>FROM publish <BR>WHERE (id NOT IN <BR>　　　　(SELECT TOP n-1 id <BR>　　　　 FROM publish)) </SPAN></DIV>
<DIV>id 为<SPAN>publish 表的关键字 </SPAN></DIV>
<DIV>
<DIV>我当时看到这篇文章的时候，真的是精神为之一振，觉得思路非常得好。等到后来，我在作办公自动化系统（<SPAN>ASP.NET+ C#＋SQL SERVER）的时候，忽然想起了这篇文章，我想如果把这个语句改造一下，这就可能是一个非常好的分页存储过程。于是我就满网上找这篇文章，没想到，文章还没找到，却找到了一篇根据此语句写的一个分页存储过程，这个存储过程也是目前较为流行的一种分页存储过程，我很后悔没有争先把这段文字改造成存储过程：</SPAN></DIV></DIV>
<DIV>CREATE PROCEDURE pagination2<BR>(<BR>&nbsp;@SQL nVARCHAR(4000),<SPAN>&nbsp;&nbsp;&nbsp; --</SPAN>不带排序语句的<SPAN>SQL语句<BR></SPAN>&nbsp;@Page int,<SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --</SPAN>页码<SPAN><BR></SPAN>&nbsp;@RecsPerPage int,<SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --</SPAN>每页容纳的记录数<SPAN><BR></SPAN>&nbsp;@ID VARCHAR(255),<SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --</SPAN>需要排序的不重复的<SPAN>ID号<BR></SPAN>&nbsp;@Sort VARCHAR(255)<SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --</SPAN>排序字段及规则<SPAN><BR>)<BR>AS</SPAN></DIV>
<DIV>DECLARE @Str nVARCHAR(4000)</DIV>
<DIV>SET @Str='SELECT&nbsp;&nbsp; TOP '+CAST(@RecsPerPage AS VARCHAR(20))+' * FROM ('+@SQL+') T WHERE T.'+@ID+'NOT IN <BR>(SELECT&nbsp;&nbsp; TOP '+CAST((@RecsPerPage*(@Page-1)) AS VARCHAR(20))+' '+@ID+' FROM ('+@SQL+') T9 ORDER BY '+@Sort+') ORDER BY '+@Sort</DIV>
<DIV>PRINT @Str</DIV>
<DIV>
<DIV>EXEC sp_ExecuteSql @Str<BR>GO</DIV></DIV>
<DIV>其实，以上语句可以简化为：</DIV>
<DIV>SELECT TOP 页大小<SPAN> *</SPAN></DIV>
<DIV>FROM Table1</DIV>
<DIV>WHERE (ID NOT IN</DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (SELECT TOP </SPAN>页大小<SPAN>*页数 id</SPAN></DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FROM </SPAN>表</DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ORDER BY id))</SPAN></DIV>
<DIV>ORDER BY ID</DIV>
<DIV>但这个存储过程有一个致命的缺点，就是它含有<SPAN>NOT IN字样。虽然我可以把它改造为：</SPAN></DIV>
<DIV>SELECT TOP 页大小<SPAN> *</SPAN></DIV>
<DIV>FROM Table1</DIV>
<DIV>WHERE not exists</DIV>
<DIV>(select * from (select top (页大小<SPAN>*页数) * from table1 order by id) b where b.id=a.id )</SPAN></DIV>
<DIV>order by id</DIV>
<DIV>即，用<SPAN>not exists来代替not in，但我们前面已经谈过了，二者的执行效率实际上是没有区别的。</SPAN></DIV>
<DIV>既便如此，用<SPAN>TOP 结合NOT IN的这个方法还是比用游标要来得快一些。</SPAN></DIV>
<DIV>虽然用<SPAN>not exists并不能挽救上个存储过程的效率，但使用SQL SERVER中的TOP关键字却是一个非常明智的选择。因为分页优化的最终目的就是避免产生过大的记录集，而我们在前面也已经提到了TOP的优势，通过TOP 即可实现对数据量的控制。</SPAN></DIV>
<DIV>在分页算法中，影响我们查询速度的关键因素有两点：<SPAN>TOP和NOT IN。TOP可以提高我们的查询速度，而NOT IN会减慢我们的查询速度，所以要提高我们整个分页算法的速度，就要彻底改造NOT IN，同其他方法来替代它。</SPAN></DIV>
<DIV>我们知道，几乎任何字段，我们都可以通过<SPAN>max(字段)或min(字段)来提取某个字段中的最大或最小值，所以如果这个字段不重复，那么就可以利用这些不重复的字段的max或min作为分水岭，使其成为分页算法中分开每页的参照物。在这里，我们可以用操作符“&gt;”或“&lt;”号来完成这个使命，使查询语句符合SARG形式。如：</SPAN></DIV>
<DIV>Select&nbsp;top&nbsp;10&nbsp;*&nbsp;from&nbsp;table1&nbsp;where&nbsp;id&gt;<SPAN>200</SPAN></DIV>
<DIV>于是就有了如下分页方案：</DIV>
<DIV>select top 页大小<SPAN> *</SPAN></DIV>
<DIV>from table1 </DIV>
<DIV>where id&gt;</DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (select max (id) from </SPAN></DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (select top ((</SPAN>页码<SPAN>-1)*页大小) id from table1 order by id) as T</SPAN></DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;)&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></DIV>
<DIV>&nbsp;&nbsp;order by id</DIV>
<DIV>在选择即不重复值，又容易分辨大小的列时，我们通常会选择主键。下表列出了笔者用有着<SPAN>1000万数据的办公自动化系统中的表，在以GID（GID是主键，但并不是聚集索引。）为排序列、提取gid,fariqi,title字段，分别以第1、10、100、500、1000、1万、10万、25万、50万页为例，测试以上三种分页方案的执行速度：（单位：毫秒）</SPAN></DIV>
<P>
<TABLE cellSpacing=0 cellPadding=0 border=1>
<TBODY>
<TR>
<TD vAlign=top width=142>
<DIV align=center>页<SPAN>&nbsp;码</SPAN></DIV></TD>
<TD vAlign=top width=142>
<DIV align=center>方案<SPAN>1</SPAN></DIV></TD>
<TD vAlign=top width=142>
<DIV align=center>方案<SPAN>2</SPAN></DIV></TD>
<TD vAlign=top width=142>
<DIV align=center>方案<SPAN>3</SPAN></DIV></TD></TR>
<TR>
<TD vAlign=top width=142>
<DIV align=center>1</DIV></TD>
<TD vAlign=top width=142>
<DIV align=center>60</DIV></TD>
<TD vAlign=top width=142>
<DIV align=center>30</DIV></TD>
<TD vAlign=top width=142>
<DIV align=center>76</DIV></TD></TR>
<TR>
<TD vAlign=top width=142>
<DIV align=center>10</DIV></TD>
<TD vAlign=top width=142>
<DIV align=center>46</DIV></TD>
<TD vAlign=top width=142>
<DIV align=center>16</DIV></TD>
<TD vAlign=top width=142>
<DIV align=center>63</DIV></TD></TR>
<TR>
<TD vAlign=top width=142>
<DIV align=center>100</DIV></TD>
<TD vAlign=top width=142>
<DIV align=center>1076</DIV></TD>
<TD vAlign=top width=142>
<DIV align=center>720</DIV></TD>
<TD vAlign=top width=142>
<DIV align=center>130</DIV></TD></TR>
<TR>
<TD vAlign=top width=142>
<DIV align=center>500</DIV></TD>
<TD vAlign=top width=142>
<DIV align=center>540</DIV></TD>
<TD vAlign=top width=142>
<DIV align=center>12943</DIV></TD>
<TD vAlign=top width=142>
<DIV align=center>83</DIV></TD></TR>
<TR>
<TD vAlign=top width=142>
<DIV align=center>1000</DIV></TD>
<TD vAlign=top width=142>
<DIV align=center>17110</DIV></TD>
<TD vAlign=top width=142>
<DIV align=center>470</DIV></TD>
<TD vAlign=top width=142>
<DIV align=center>250</DIV></TD></TR>
<TR>
<TD vAlign=top width=142>
<DIV align=center>1万</DIV></TD>
<TD vAlign=top width=142>
<DIV align=center>24796</DIV></TD>
<TD vAlign=top width=142>
<DIV align=center>4500</DIV></TD>
<TD vAlign=top width=142>
<DIV align=center>140</DIV></TD></TR>
<TR>
<TD vAlign=top width=142>
<DIV align=center>10万</DIV></TD>
<TD vAlign=top width=142>
<DIV align=center>38326</DIV></TD>
<TD vAlign=top width=142>
<DIV align=center>42283</DIV></TD>
<TD vAlign=top width=142>
<DIV align=center>1553</DIV></TD></TR>
<TR>
<TD vAlign=top width=142>
<DIV align=center>25万</DIV></TD>
<TD vAlign=top width=142>
<DIV align=center>28140</DIV></TD>
<TD vAlign=top width=142>
<DIV align=center>128720</DIV></TD>
<TD vAlign=top width=142>
<DIV align=center>2330</DIV></TD></TR>
<TR>
<TD vAlign=top width=142>
<DIV align=center>50万</DIV></TD>
<TD vAlign=top width=142>
<DIV align=center>121686</DIV></TD>
<TD vAlign=top width=142>
<DIV align=center>127846</DIV></TD>
<TD vAlign=top width=142>
<DIV align=center>7168</DIV></TD></TR></TBODY></TABLE></P>
<DIV>从上表中，我们可以看出，三种存储过程在执行<SPAN>100页以下的分页命令时，都是可以信任的，速度都很好。但第一种方案在执行分页1000页以上后，速度就降了下来。第二种方案大约是在执行分页1万页以上后速度开始降了下来。而第三种方案却始终没有大的降势，后劲仍然很足。</SPAN></DIV>
<DIV>
<DIV>在确定了第三种分页方案后，我们可以据此写一个存储过程。大家知道<SPAN>SQL SERVER的存储过程是事先编译好的SQL语句，它的执行效率要比通过WEB页面传来的SQL语句的执行效率要高。下面的存储过程不仅含有分页方案，还会根据页面传来的参数来确定是否进行数据总数统计。</SPAN></DIV></DIV>
<DIV>-- 获取指定页的数据</DIV>
<DIV>CREATE PROCEDURE pagination3</DIV>
<DIV>@tblName<SPAN>&nbsp;&nbsp; varchar(255),&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- </SPAN>表名</DIV>
<DIV>@strGetFields varchar(1000) = '*',&nbsp;-- 需要返回的列</DIV>
<DIV>@fldName varchar(255)='',<SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- </SPAN>排序的字段名</DIV>
<DIV>@PageSize <SPAN>&nbsp;&nbsp;int = 10,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- </SPAN>页尺寸</DIV>
<DIV>@PageIndex&nbsp;int = 1,<SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- </SPAN>页码</DIV>
<DIV>@doCount&nbsp;bit = 0,<SPAN>&nbsp;&nbsp; -- </SPAN>返回记录总数<SPAN>, 非 0 值则返回</SPAN></DIV>
<DIV>@OrderType bit = 0,&nbsp;-- 设置排序类型<SPAN>, 非 0 值则降序</SPAN></DIV>
<DIV>@strWhere&nbsp;varchar(1500) = ''&nbsp;-- 查询条件<SPAN> (注意: 不要加 where)</SPAN></DIV>
<DIV>AS</DIV>
<DIV>declare @strSQL<SPAN>&nbsp;&nbsp; varchar(5000)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- </SPAN>主语句</DIV>
<DIV>declare @strTmp<SPAN>&nbsp;&nbsp; varchar(110)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- </SPAN>临时变量</DIV>
<DIV>declare @strOrder varchar(400)<SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- </SPAN>排序类型</DIV>
<DIV>&nbsp;</DIV>
<DIV>if @doCount != 0</DIV>
<DIV>&nbsp;begin</DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp; if @strWhere !=''</SPAN></DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp; set @strSQL = "select count(*) as Total from [" + @tblName + "] where "+@strWhere</SPAN></DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp; else</SPAN></DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp; set @strSQL = "select count(*) as Total from [" + @tblName + "]"</SPAN></DIV>
<DIV>end&nbsp;</DIV>
<DIV>--以上代码的意思是如果<SPAN>@doCount传递过来的不是0，就执行总数统计。以下的所有代码都是@doCount为0的情况</SPAN></DIV>
<DIV>else</DIV>
<DIV>begin</DIV>
<DIV>&nbsp;</DIV>
<DIV>if @OrderType != 0</DIV>
<DIV>begin</DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp; set @strTmp = "&lt;(select min"</SPAN></DIV>
<DIV>set @strOrder = " order by [" + @fldName +"] desc"</DIV>
<DIV>--如果<SPAN>@OrderType不是0，就执行降序，这句很重要！</SPAN></DIV>
<DIV>end</DIV>
<DIV>else</DIV>
<DIV>begin</DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp; set @strTmp = "&gt;(select max"</SPAN></DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp; set @strOrder = " order by [" + @fldName +"] asc"</SPAN></DIV>
<DIV>end</DIV>
<DIV>&nbsp;</DIV>
<DIV>if @PageIndex = 1</DIV>
<DIV>begin</DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp; if @strWhere != ''&nbsp;&nbsp; </SPAN></DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp; set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ "&nbsp;from [" + @tblName + "] where " + @strWhere + " " + @strOrder</SPAN></DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp;&nbsp; else</SPAN></DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp;&nbsp; set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ "&nbsp;from ["+ @tblName + "] "+ @strOrder</SPAN></DIV>
<DIV>--如果是第一页就执行以上代码，这样会加快执行速度</DIV>
<DIV>end</DIV>
<DIV>else</DIV>
<DIV>begin</DIV>
<DIV>--以下代码赋予了<SPAN>@strSQL以真正执行的SQL代码</SPAN></DIV>
<DIV>set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ "&nbsp;from ["</DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp; + @tblName + "] where [" + @fldName + "]" + @strTmp + "(["+ @fldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["+ @fldName + "] from [" + @tblName + "]" + @strOrder + ") as tblTmp)"+ @strOrder</SPAN></DIV>
<DIV>&nbsp;</DIV>
<DIV>if @strWhere != ''</DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp; set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ "&nbsp;from ["</SPAN></DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + @tblName + "] where [" + @fldName + "]" + @strTmp + "(["</SPAN></DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + @fldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["</SPAN></DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + @fldName + "] from [" + @tblName + "] where " + @strWhere + " "</SPAN></DIV>
<DIV><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + @strOrder + ") as tblTmp) and " + @strWhere + " " + @strOrder</SPAN></DIV>
<DIV>end </DIV>
<DIV>end<SPAN>&nbsp;&nbsp; </SPAN></DIV>
<DIV>exec (@strSQL)</DIV>
<DIV>
<DIV>GO</DIV></DIV>
<DIV>上面的这个存储过程是一个通用的存储过程，其注释已写在其中了。</DIV>
<DIV>在大数据量的情况下，特别是在查询最后几页的时候，查询时间一般不会超过<SPAN>9秒；而用其他存储过程，在实践中就会导致超时，所以这个存储过程非常适用于大容量数据库的查询。</SPAN></DIV>
<DIV>笔者希望能够通过对以上存储过程的解析，能给大家带来一定的启示，并给工作带来一定的效率提升，同时希望同行提出更优秀的实时数据分页算法。</DIV>
<DIV>&nbsp;</DIV>
<DIV>四、聚集索引的重要性和如何选择聚集索引</DIV>
<DIV>在上一节的标题中，笔者写的是：<B>实现小数据量和海量数据的通用分页显示存储过程。</B>这是因为在将本存储过程应用于“办公自动化”系统的实践中时，笔者发现这第三种存储过程在小数据量的情况下，有如下现象：</DIV>
<DIV>1、分页速度一般维持在<SPAN>1秒和3秒之间。</SPAN></DIV>
<DIV>2、在查询最后一页时，速度一般为<SPAN>5秒至8秒，哪怕分页总数只有3页或30万页。</SPAN></DIV>
<DIV>虽然在超大容量情况下，这个分页的实现过程是很快的，但在分前几页时，这个<SPAN>1－3秒的速度比起第一种甚至没有经过优化的分页方法速度还要慢，借用户的话说就是“还没有ACCESS数据库速度快”，这个认识足以导致用户放弃使用您开发的系统。</SPAN></DIV>
<DIV>笔者就此分析了一下，原来产生这种现象的症结是如此的简单，但又如此的重要：<B>排序的字段不是聚集索引！</B></DIV>
<DIV>本篇文章的题目是：“查询优化及分页算法方案”。笔者只所以把“查询优化”和“分页算法”这两个联系不是很大的论题放在一起，就是因为二者都需要一个非常重要的东西――聚集索引。</DIV>
<DIV>在前面的讨论中我们已经提到了，聚集索引有两个最大的优势：</DIV>
<DIV><B>1</B><B>、以最快的速度缩小查询范围。</B></DIV>
<DIV><B>2</B><B>、以最快的速度进行字段排序。</B></DIV>
<DIV>第<SPAN>1条多用在查询优化时，而第2条多用在进行分页时的数据排序。</SPAN></DIV>
<DIV>而聚集索引在每个表内又只能建立一个，这使得聚集索引显得更加的重要。聚集索引的挑选可以说是实现“查询优化”和“高效分页”的最关键因素。</DIV>
<DIV>但要既使聚集索引列既符合查询列的需要，又符合排序列的需要，这通常是一个矛盾。</DIV>
<DIV>笔者前