﻿<?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>C++博客-xyjzsh-随笔分类-sqlserver2008</title><link>http://www.cppblog.com/xyjzsh/category/18015.html</link><description /><language>zh-cn</language><lastBuildDate>Fri, 04 Nov 2011 12:30:20 GMT</lastBuildDate><pubDate>Fri, 04 Nov 2011 12:30:20 GMT</pubDate><ttl>60</ttl><item><title>sqlserver2008里面的游标</title><link>http://www.cppblog.com/xyjzsh/archive/2011/11/03/159571.html</link><dc:creator> 呆人</dc:creator><author> 呆人</author><pubDate>Thu, 03 Nov 2011 08:28:00 GMT</pubDate><guid>http://www.cppblog.com/xyjzsh/archive/2011/11/03/159571.html</guid><wfw:comment>http://www.cppblog.com/xyjzsh/comments/159571.html</wfw:comment><comments>http://www.cppblog.com/xyjzsh/archive/2011/11/03/159571.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/xyjzsh/comments/commentRss/159571.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/xyjzsh/services/trackbacks/159571.html</trackback:ping><description><![CDATA[&nbsp;SQL是一种基于集合的语言(a set-based language) ,他更擅长操作和提出一组数据，而不是对<br />&nbsp;数据进行一行一行的处理。<br />&nbsp;SQL is a set-based language ,meaning that is excels at mantipulating and retrieving<br />&nbsp;set of rows ,rather than performing single row-by-row processing.<br />&nbsp;如果你的程序里一定要一条一条的执行，那么一定要先考虑使用如while循环，子查询，<br />&nbsp;临时表，表变量等等，如果这些都不能满足要求，在考虑使用游标。<br />&nbsp;<br />&nbsp;T-SQL中游标的生存周期：<br />&nbsp;1.用返回一个有效结果集的sql语句来定义一个游标。<br />&nbsp; a cursor is defined via a SQL statement that returns a valid result set.<br />&nbsp;2. 打开游标<br />&nbsp;3. 一旦游标被打开就可以从游标中每次取出一行数据，要根据游标的定义可以向前去数据或<br />&nbsp;向后取数据<br />&nbsp;the rows can be fetched moving forword or backword ,depending on the original cursor definition.<br />&nbsp;4. 根据游标的类型，数据可以被修改或者只能读。<br />&nbsp;5.最后，用完游标后，必须被显示的关闭，并且从内存中移除。<br />&nbsp;<br />&nbsp;游标定义格式：<br />&nbsp;declare cursor_name cursor<br />&nbsp;[local|global]<br />&nbsp;[forword_only|scroll]<br />&nbsp;[static|keyset|dynamic|fast_forword]<br />&nbsp;[read_only| scroll_locks|optimistic]<br />&nbsp;[type_warning]<br />&nbsp;for select_statement[for update [of column[,...]]]<br />&nbsp;<br />The select_statement argument is the query used to define the data within the cursor. Avoid<br />using a query that hasmore columns and rows than will actually be used, because cursors, while<br />open, are kept inmemory. The UPDATE [OF column_name [,...n]] is used to specify those columns<br />that are allowed to be updated by the cursor.<br />&nbsp;<img src ="http://www.cppblog.com/xyjzsh/aggbug/159571.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/xyjzsh/" target="_blank"> 呆人</a> 2011-11-03 16:28 <a href="http://www.cppblog.com/xyjzsh/archive/2011/11/03/159571.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>sqlserver2008 条件循环</title><link>http://www.cppblog.com/xyjzsh/archive/2011/11/03/159570.html</link><dc:creator> 呆人</dc:creator><author> 呆人</author><pubDate>Thu, 03 Nov 2011 08:27:00 GMT</pubDate><guid>http://www.cppblog.com/xyjzsh/archive/2011/11/03/159570.html</guid><wfw:comment>http://www.cppblog.com/xyjzsh/comments/159570.html</wfw:comment><comments>http://www.cppblog.com/xyjzsh/archive/2011/11/03/159570.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/xyjzsh/comments/commentRss/159570.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/xyjzsh/services/trackbacks/159570.html</trackback:ping><description><![CDATA[<p>条件处理：condtional processing<br />1.case函数以一个表达式作为输入，一个值作为输出<br />格式：case 后面有输入表达式，when后面的的每一个表达式都会和case后面的输入表达式进行比较运算<br />如果相等，则返回，否则返回else后面的表达式，如果没有else则返回NULL。<br />case input_expression<br />&nbsp;&nbsp;&nbsp;&nbsp; when when_expression then result_expression<br />&nbsp;&nbsp;&nbsp;&nbsp; [...n]<br />&nbsp;&nbsp;&nbsp;&nbsp; [else else_result_expression]<br />end</p>
<p>case的第二种情况：<br />case后面没有表达式，when后面跟的是bool表达式。返回第一个when后面计算为true的表达式<br />格式为：<br />case<br />&nbsp;when bool_expression then result_expression<br />&nbsp;[...n]<br />&nbsp;else result_expression<br />end</p>
<p>2. if....else...的格式<br />if bool_expression<br />{sql_statement|sql_block}<br />[else<br />{sql_statement|sql_block}<br />]</p>
<p>3.begin ....end 相当于c++中的{...}用来形成一个代码块</p>
<p>4.条件循环<br />return,while,goto,waitfor</p>
<p>return <br />return 用于结束当前的sql块,查询，存储过程。<br />类似于c++中的return。<br />return 可以返回一个数字</p>
<p>while 类似c++中的while，同样支持break，continue，break来结束当前内层循环，continue继续当前循环</p>
<p>waitfor格式<br />waitfor<br />&nbsp;delay 'time_to_pass'//执行前等待的时间：格式为00:00:00小时：分钟：秒<br />&nbsp;|time 'time_to_execute'//设置实际执行的时间<br />&nbsp;|（receive_statement）[,TimeOUT timeout]<br />&nbsp;可以利用waitfor将某些复杂的执行设定为在相对空闲的时间内进行。</p><img src ="http://www.cppblog.com/xyjzsh/aggbug/159570.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/xyjzsh/" target="_blank"> 呆人</a> 2011-11-03 16:27 <a href="http://www.cppblog.com/xyjzsh/archive/2011/11/03/159570.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>sqlserver2008中数据类型的优先级</title><link>http://www.cppblog.com/xyjzsh/archive/2011/10/31/159382.html</link><dc:creator> 呆人</dc:creator><author> 呆人</author><pubDate>Mon, 31 Oct 2011 03:25:00 GMT</pubDate><guid>http://www.cppblog.com/xyjzsh/archive/2011/10/31/159382.html</guid><wfw:comment>http://www.cppblog.com/xyjzsh/comments/159382.html</wfw:comment><comments>http://www.cppblog.com/xyjzsh/archive/2011/10/31/159382.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/xyjzsh/comments/commentRss/159382.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/xyjzsh/services/trackbacks/159382.html</trackback:ping><description><![CDATA[<p>当两个不同数据类型的表达式用运算符组合后，数据类型优先级规则指定将优先级较低的数据类型转换为优先级较高的数据类型。如果此转换不是所支持的隐式转换，则返回错误。当两个操作数表达式具有相同的数据类型时，运算的结果便为该数据类型。</p>
<p>SQL Server 对数据类型使用以下优先级顺序：</p>
<ol><li>
<p>用户定义数据类型（最高）</p></li><li>
<p><strong>sql_varian</strong><strong>t</strong></p></li><li>
<p><strong>xml</strong></p></li><li>
<p><strong>datetimeoffset</strong></p></li><li>
<p><strong>datetime2</strong></p></li><li>
<p><strong>datetime</strong></p></li><li>
<p><strong>smalldatetime</strong></p></li><li>
<p><strong>date</strong></p></li><li>
<p><strong>time</strong></p></li><li>
<p><strong>float</strong></p></li><li>
<p><strong>real</strong></p></li><li>
<p><strong>decimal</strong></p></li><li>
<p><strong>money</strong></p></li><li>
<p><strong>smallmoney</strong></p></li><li>
<p><strong>bigint</strong></p></li><li>
<p><strong>int</strong></p></li><li>
<p><strong>smallint</strong></p></li><li>
<p><strong>tinyint</strong></p></li><li>
<p><strong>bit</strong></p></li><li>
<p><strong>ntext</strong></p></li><li>
<p><strong>text</strong></p></li><li>
<p><strong>image</strong></p></li><li>
<p><strong>timestamp</strong></p></li><li>
<p><strong>uniqueidentifier</strong></p></li><li>
<p><strong>nvarchar</strong>（包括 <span class="input">nvarchar(max)</span>）</p></li><li>
<p><strong>nchar</strong></p></li><li>
<p><strong>varchar</strong>（包括 <span class="input">varchar(max)</span>）</p></li><li>
<p><strong>char</strong></p></li><li>
<p><strong>varbinary</strong>（包括 <span class="input">varbinary(max)</span>）</p></li><li>
<p><strong>binary</strong>（最低）</p></li></ol><img src ="http://www.cppblog.com/xyjzsh/aggbug/159382.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/xyjzsh/" target="_blank"> 呆人</a> 2011-10-31 11:25 <a href="http://www.cppblog.com/xyjzsh/archive/2011/10/31/159382.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>