??xml version="1.0" encoding="utf-8" standalone="yes"?>四虎精品永久免费,888av在线,精品国产网站地址http://www.aygfsteel.com/Reg/category/45432.htmlzh-cnSat, 19 Mar 2011 02:45:28 GMTSat, 19 Mar 2011 02:45:28 GMT60昄sql server的查询语句运行时_具体到毫U?/title><link>http://www.aygfsteel.com/Reg/archive/2011/03/18/346546.html</link><dc:creator>艾L</dc:creator><author>艾L</author><pubDate>Fri, 18 Mar 2011 08:31:00 GMT</pubDate><guid>http://www.aygfsteel.com/Reg/archive/2011/03/18/346546.html</guid><wfw:comment>http://www.aygfsteel.com/Reg/comments/346546.html</wfw:comment><comments>http://www.aygfsteel.com/Reg/archive/2011/03/18/346546.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/Reg/comments/commentRss/346546.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/Reg/services/trackbacks/346546.html</trackback:ping><description><![CDATA[<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><span style="color: #0000ff">declare</span><span style="color: #000000">   </span><span style="color: #008000">@ct</span><span style="color: #000000">   </span><span style="color: #000000; font-weight: bold">datetime</span><span style="color: #000000">   <br />   </span><span style="color: #0000ff">set</span><span style="color: #000000">   </span><span style="color: #008000">@ct</span><span style="color: #000000">   </span><span style="color: #808080">=</span><span style="color: #000000">   </span><span style="color: #ff00ff">getdate</span><span style="color: #000000">()   <br />   </span><span style="color: #008080">--</span><span style="color: #008080">-   </span><span style="color: #008080"><br /> </span><span style="color: #000000">  </span><span style="color: #008080">--</span><span style="color: #008080">sql   语句   </span><span style="color: #008080"><br /> </span><span style="color: #000000"><br />   </span><span style="color: #008080">--</span><span style="color: #008080">-   </span><span style="color: #008080"><br /> </span><span style="color: #000000">  </span><span style="color: #0000ff">select</span><span style="color: #000000">   </span><span style="color: #ff00ff">datediff</span><span style="color: #000000">(ms,   </span><span style="color: #008000">@ct</span><span style="color: #000000">   ,</span><span style="color: #ff00ff">getdate</span><span style="color: #000000">())   </span><span style="color: #0000ff">as</span><span style="color: #000000">   </span><span style="color: #ff0000">'</span><span style="color: #ff0000">查询旉Q毫U)</span><span style="color: #ff0000">'</span><span style="color: #000000"> <br /> </span></div> <img src ="http://www.aygfsteel.com/Reg/aggbug/346546.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/Reg/" target="_blank">艾L</a> 2011-03-18 16:31 <a href="http://www.aygfsteel.com/Reg/archive/2011/03/18/346546.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>?Oracle用Start with...Connect By子句递归查询http://www.aygfsteel.com/Reg/archive/2011/03/16/346370.html艾L艾LWed, 16 Mar 2011 03:30:00 GMThttp://www.aygfsteel.com/Reg/archive/2011/03/16/346370.htmlhttp://www.aygfsteel.com/Reg/comments/346370.htmlhttp://www.aygfsteel.com/Reg/archive/2011/03/16/346370.html#Feedback0http://www.aygfsteel.com/Reg/comments/commentRss/346370.htmlhttp://www.aygfsteel.com/Reg/services/trackbacks/346370.htmlStart withConnect By子句递归查询一般用于一个表l护树Şl构的应用?br /> 创徏CZ表:
CREATE TABLE TBL_TEST
(
  ID    
NUMBER,
  NAME  
VARCHAR2(100 BYTE),
  PID   
NUMBER                                  DEFAULT 0
);
 
插入试数据Q?br />
INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('1','10','0');
INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('2','11','1');
INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('3','20','0');
INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('4','12','1');
INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('5','121','2');
 
从Root往树末梢递归
select * from TBL_TEST
 start 
with id=1
 connect 
by prior id = pid
 
从末梢往树ROOT递归
select * from TBL_TEST
 start 
with id=5
 connect 
by prior pid = id
=====
对于oracleq行单树查询(递归查询)

DEPTID PAREDEPTID NAME 
NUMBER NUMBER CHAR (40 Byte) 
部门id 爉门id(所属部门id) 部门名称 


通过子节点向根节点追?

Sql代码 
1.select * from persons.dept start with deptid=76 connect by prior paredeptid=deptid   
Sql代码  
1.select * from persons.dept start with deptid=76 connect by prior paredeptid=deptid   
 
select * from persons.dept start with deptid=76 connect by prior paredeptid=deptid  

通过根节炚w历子节点. 

Sql代码 
1.select * from persons.dept start with paredeptid=0 connect by prior deptid=paredeptid   
Sql代码  
1.select * from persons.dept start with paredeptid=0 connect by prior deptid=paredeptid   
select * from persons.dept start with paredeptid=0 connect by prior deptid=paredeptid  

可通过level 关键字查询所在层? 

Sql代码 
1.select a.*,level from persons.dept a start with paredeptid=0 connect by prior deptid=paredeptid   
Sql代码  
1.select a.*,level from persons.dept a start with paredeptid=0 connect by prior deptid=paredeptid   
select a.*,level from persons.dept a start with paredeptid=0 connect by prior deptid=paredeptid  

再次复习一?start 
with connect by 的用法, start with 后面所跟的是是递归的种子?nbsp;

递归的种子也是递归开始的地方 connect 
by 后面?prior" 如果~省Q则只能查询到符合条件的起始行,q不q行递归查询Q?nbsp;

connect 
by prior 后面所攄字段是有关系的,它指明了查询的方向?nbsp;

l习Q?nbsp;通过子节点获得顶节点 

Sql代码 
1.select FIRST_VALUE(deptid) OVER (ORDER BY LEVEL DESC ROWS UNBOUNDED PRECEDING) AS firstdeptid from persons.dept start with deptid=76 connect by prior paredeptid=deptid  
====q种Ҏ只是当表里就有一颗树,多棵树怎么办?

.声明QJavaEye文章版权属于作者,受法律保护。没有作者书面许可不得{载?nbsp;


艾L 2011-03-16 11:30 发表评论
]]>
SQLSERVER 字符函数整理http://www.aygfsteel.com/Reg/archive/2011/03/16/346369.html艾L艾LWed, 16 Mar 2011 03:17:00 GMThttp://www.aygfsteel.com/Reg/archive/2011/03/16/346369.htmlhttp://www.aygfsteel.com/Reg/comments/346369.htmlhttp://www.aygfsteel.com/Reg/archive/2011/03/16/346369.html#Feedback0http://www.aygfsteel.com/Reg/comments/commentRss/346369.htmlhttp://www.aygfsteel.com/Reg/services/trackbacks/346369.html  /* 字符函数 */
  
/* q回字符表达式中最左侧字符的ASCII代码?nbsp;*/
  
select Ascii(‘a‘) --a:97,A:65
  /* 整数ASCII代码转换为字W?nbsp;*/
  
select Char(97)--97:a,65:A
  /* q回表达式中指定字符的开始位|?nbsp;*/
  
select Charindex(‘b‘,‘abcdefg‘,5)
  
/* 以整数返回两个字W表辑ּ的SOUNDEXg?nbsp;*/
  
select Difference(‘bet‘,‘bit‘)--3
  /* q回字符表达式最左侧指定数目的字W?nbsp;*/
  
select Left(‘abcdefg‘,3)--abc
  /* q回l定字符串表辄字符?nbsp;*/
  
select Len(‘abcdefg‘)--7
  /* q回大写字W{换ؓ字W的字符表达?nbsp;*/
  
select Lower(‘ABCDEFG‘)--abcdefg
  /* q回删除了前导空g后字W表辑ּ */
  
select Ltrim(‘   abcdefg‘)--abcdefg
  /* q回hl定的整C码的UNICODE字符 */
  
select Nchar(65)--A
  /* q回指定表达式中模式W一ơ出现的开始位|?nbsp;*/
  
select Patindex(‘%_cd%‘,‘abcdefg‘)--2
  /* q回为成为有效的SQL SERVER分隔标识W而添加了分隔W的UNICODE字符?nbsp;*/
  
select Quotename(‘create table‘)
  
/* 用第三个表达式替换第一个表辑ּ中出现的W二个表辑ּ */
  
select Replace(‘abcdefg‘,‘cd‘,‘xxx‘)--abxxxefg
  /* 按指定次数重复表辑ּ */
  
select Replicate(‘abc|‘,4)--abc|abc|abc|abc|
  /* q回字符表达式的逆向表达?nbsp;*/
  
select Reverse(‘abc‘)--cba
  /* q回字符表达式右侧指定数目的字符 */
  
select Right(‘abcd‘,3)--bcd
  /* q回截断了所有尾随空g后的字符表达?nbsp;*/
  
select Rtrim(‘abcd    ‘)--abcd
  /* q回由四个字W表辄SOUNDEX代码 */
  
select Soundex(‘abcd‘)--A120
  /* q回由重复空格组成的字符?nbsp;*/
  
select Space(10)--[  ]
  /* q回从默认表达{换而来的字W串 */
  
select Str(100)--[     100]
  /*  */
  
select Str(100,3)--[100]
  /*  */
  
select Str(14.4444,5,4)--[14.44]
  /* 删除指定长度的字W?q在指定的v点处插入另一l字W?nbsp;*/
  
select Stuff(‘abcdefg‘,2,4,‘xxx‘)--axxxfg
  /* q回字符表达?二进?文本表达式或囑փ表达的一部分 */
  
select Substring(‘abcdefg‘,2,3)--bcd
  /* q回表达W一个字W的UNICODE整数?nbsp;*/
  
select Unicode(‘a‘)--97
  /* q回小写字W{换ؓ大写字符的字W表辑ּ */
  
select Upper(‘a‘)--‘A‘

艾L 2011-03-16 11:17 发表评论
]]>
SQLSERVER 数字格式?/title><link>http://www.aygfsteel.com/Reg/archive/2011/03/15/346336.html</link><dc:creator>艾L</dc:creator><author>艾L</author><pubDate>Tue, 15 Mar 2011 09:57:00 GMT</pubDate><guid>http://www.aygfsteel.com/Reg/archive/2011/03/15/346336.html</guid><wfw:comment>http://www.aygfsteel.com/Reg/comments/346336.html</wfw:comment><comments>http://www.aygfsteel.com/Reg/archive/2011/03/15/346336.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/Reg/comments/commentRss/346336.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/Reg/services/trackbacks/346336.html</trackback:ping><description><![CDATA[<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><span style="color: #ff00ff">Cast</span><span style="color: #000000">(</span><span style="color: #ff00ff">round</span><span style="color: #000000">((a.M_weight</span><span style="color: #808080">*</span><span style="color: #000000">a.length</span><span style="color: #808080">*</span><span style="color: #000000">a.R_NUMBER),</span><span style="color: #800000; font-weight: bold">2</span><span style="color: #000000">) </span><span style="color: #0000ff">as</span><span style="color: #000000"> numeric(</span><span style="color: #800000; font-weight: bold">12</span><span style="color: #000000">,</span><span style="color: #800000; font-weight: bold">2</span><span style="color: #000000">))total</span></div> <img src ="http://www.aygfsteel.com/Reg/aggbug/346336.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/Reg/" target="_blank">艾L</a> 2011-03-15 17:57 <a href="http://www.aygfsteel.com/Reg/archive/2011/03/15/346336.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>SQL SERVER 2008 PIVOThttp://www.aygfsteel.com/Reg/archive/2011/03/09/346046.html艾L艾LWed, 09 Mar 2011 09:57:00 GMThttp://www.aygfsteel.com/Reg/archive/2011/03/09/346046.htmlhttp://www.aygfsteel.com/Reg/comments/346046.htmlhttp://www.aygfsteel.com/Reg/archive/2011/03/09/346046.html#Feedback0http://www.aygfsteel.com/Reg/comments/commentRss/346046.htmlhttp://www.aygfsteel.com/Reg/services/trackbacks/346046.html select doctype,docid,pro_type,create_date from TSPRO_SALESROOM_STOCKIN_MAIN where CREATE_DATE>'2011-02-01' and CREATE_DATE<='2011-02-28'
 
  
select count(*from TSPRO_SALESROOM_STOCKIN_MAIN where CREATE_DATE>'2011-02-01' and CREATE_DATE<='2011-02-28' and pro_type=2
 
 
 
SELECT pro_type,
[1] AS JAN,
[2] AS FEB,
[3] AS MAR,
[4] AS APR
 
from (
 
select pro_type,DATEPART(MM,create_date) AS MON 
  
from TSPRO_SALESROOM_STOCKIN_MAIN where CREATE_DATE>'2011-01-20' and CREATE_DATE<='2011-03-09'
 )s 
 PIVOT
 (
     
COUNT(MON) FOR MON IN ([1],[2],[3],[4])
 )
AS p

        
        
        


艾L 2011-03-09 17:57 发表评论
]]>
水号创?SQL2005)http://www.aygfsteel.com/Reg/archive/2010/12/16/340917.html艾L艾LThu, 16 Dec 2010 11:39:00 GMThttp://www.aygfsteel.com/Reg/archive/2010/12/16/340917.htmlhttp://www.aygfsteel.com/Reg/comments/340917.htmlhttp://www.aygfsteel.com/Reg/archive/2010/12/16/340917.html#Feedback0http://www.aygfsteel.com/Reg/comments/commentRss/340917.htmlhttp://www.aygfsteel.com/Reg/services/trackbacks/340917.htmlright(cast(power(10,8as varchar)+IDENT_CURRENT('tablebame'),8)


艾L 2010-12-16 19:39 发表评论
]]>
SQL Server 2005 自定义函数语汇小l?/title><link>http://www.aygfsteel.com/Reg/archive/2010/12/16/340916.html</link><dc:creator>艾L</dc:creator><author>艾L</author><pubDate>Thu, 16 Dec 2010 11:38:00 GMT</pubDate><guid>http://www.aygfsteel.com/Reg/archive/2010/12/16/340916.html</guid><wfw:comment>http://www.aygfsteel.com/Reg/comments/340916.html</wfw:comment><comments>http://www.aygfsteel.com/Reg/archive/2010/12/16/340916.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.aygfsteel.com/Reg/comments/commentRss/340916.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/Reg/services/trackbacks/340916.html</trackback:ping><description><![CDATA[<div style="background-color: rgb(238, 238, 238); font-size: 13px; border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 0, 0);">׃工作的需要,了解下SQL Server </span><span style="color: rgb(128, 0, 0); font-weight: bold;">2005</span><span style="color: rgb(0, 0, 0);"> 函数的写法,现在ȝ一下:<br /> <br /> 对于SQL Server </span><span style="color: rgb(128, 0, 0); font-weight: bold;">2005</span><span style="color: rgb(0, 0, 0);"> 数据库而言Q函C存储q程在语法方面是有很大的相同点,<br /> <br /> 最大的不同是函数有返回|直接使用returns ,而存储过E则使用output来声明输出变?br /> <br /> 一、下面先说明下,如何创徏函数<br /> <br /> </span><span style="color: rgb(128, 0, 0); font-weight: bold;">1</span><span style="color: rgb(0, 0, 0);">、创建没有返回g没有参数的函?br /> <br /> </span><span style="color: rgb(0, 0, 255);">CREATE</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">FUNCTION</span><span style="color: rgb(0, 0, 0);"> my_function()<br /> <br /> </span><span style="color: rgb(0, 0, 255);">AS</span><span style="color: rgb(0, 0, 0);"><br /> <br /> </span><span style="color: rgb(0, 0, 255);">BEGIN</span><span style="color: rgb(0, 0, 0);"><br /> <br />     </span><span style="color: rgb(0, 0, 255);">DECLARE</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@variable</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">varchar</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 0); font-weight: bold;">255</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">声明字符型变?/span><span style="color: rgb(0, 128, 128);"><br /> </span><span style="color: rgb(0, 0, 0);"><br />     </span><span style="color: rgb(0, 0, 255);">DECLARE</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@variable</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">int</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">声明整Ş型变?/span><span style="color: rgb(0, 128, 128);"><br /> </span><span style="color: rgb(0, 0, 0);"><br />     <img src="http://www.aygfsteel.com/Images/dot.gif" alt="" />(do something)<br /> <br /> </span><span style="color: rgb(0, 0, 255);">SET</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@variable</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">12345</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">对变量variable赋?/span><span style="color: rgb(0, 128, 128);"><br /> </span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">END</span><span style="color: rgb(0, 0, 0);"><br /> <br /> </span><span style="color: rgb(128, 0, 0); font-weight: bold;">2</span><span style="color: rgb(0, 0, 0);">、创建没有返回值有参数的函?br /> <br /> </span><span style="color: rgb(0, 0, 255);">CREATE</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">FUNCTION</span><span style="color: rgb(0, 0, 0);"> my_function(</span><span style="color: rgb(0, 128, 0);">@user_Name</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">varchar</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 0); font-weight: bold;">128</span><span style="color: rgb(0, 0, 0);">),</span><span style="color: rgb(0, 128, 0);">@password</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">int</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 0); font-weight: bold;">6</span><span style="color: rgb(0, 0, 0);">))<br /> <br /> </span><span style="color: rgb(0, 0, 255);">AS</span><span style="color: rgb(0, 0, 0);"><br /> <br /> </span><span style="color: rgb(0, 0, 255);">BEGIN</span><span style="color: rgb(0, 0, 0);"><br /> <br />     </span><span style="color: rgb(0, 0, 255);">DECLARE</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@variable_1</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">varchar</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 0); font-weight: bold;">255</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">声明字符型变?/span><span style="color: rgb(0, 128, 128);"><br /> </span><span style="color: rgb(0, 0, 0);"><br />     <img src="http://www.aygfsteel.com/Images/dot.gif" alt="" />(do something)<br /> <br /> </span><span style="color: rgb(0, 0, 255);">SET</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@variable_1</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@user_Name</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 255);">convert</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0); font-weight: bold;">varchar</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 0); font-weight: bold;">255</span><span style="color: rgb(0, 0, 0);">),</span><span style="color: rgb(0, 128, 0);">@password</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">变量@user_Name与@passwordq接赋给@variable_1Q其中convert()函数是将int型{为varchar?/span><span style="color: rgb(0, 128, 128);"><br /> </span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">END</span><span style="color: rgb(0, 0, 0);"><br /> <br /> </span><span style="color: rgb(128, 0, 0); font-weight: bold;">3</span><span style="color: rgb(0, 0, 0);">、创建有q回g有参数的函数<br /> <br /> </span><span style="color: rgb(0, 0, 255);">CREATE</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">FUNCTION</span><span style="color: rgb(0, 0, 0);"> my_function(</span><span style="color: rgb(0, 128, 0);">@user_Name</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">varchar</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 0); font-weight: bold;">128</span><span style="color: rgb(0, 0, 0);">),</span><span style="color: rgb(0, 128, 0);">@password</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">int</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 0); font-weight: bold;">6</span><span style="color: rgb(0, 0, 0);">))<br /> <br /> </span><span style="color: rgb(0, 0, 255);">returns</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">varchar</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 0); font-weight: bold;">255</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">讄q回|C是returns 而不是return</span><span style="color: rgb(0, 128, 128);"><br /> </span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">AS</span><span style="color: rgb(0, 0, 0);"><br /> <br /> </span><span style="color: rgb(0, 0, 255);">BEGIN</span><span style="color: rgb(0, 0, 0);"><br /> <br />     </span><span style="color: rgb(0, 0, 255);">DECLARE</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@result</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">varchar</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 0); font-weight: bold;">5</span><span style="color: rgb(0, 0, 0);">)<br /> <br />     </span><span style="color: rgb(0, 0, 255);">DECLARE</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@fagle</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">varchar</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 0); font-weight: bold;">5</span><span style="color: rgb(0, 0, 0);">)<br /> <br /> </span><span style="color: rgb(0, 0, 255);">SET</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@result</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">select</span><span style="color: rgb(0, 0, 0);"> users.</span><span style="color: rgb(255, 0, 255);">user_Name</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> USERS </span><span style="color: rgb(0, 0, 255);">as</span><span style="color: rgb(0, 0, 0);"> users </span><span style="color: rgb(0, 0, 255);">where</span><span style="color: rgb(0, 0, 0);"> users.</span><span style="color: rgb(255, 0, 255);">user_Name</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@user_Name</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">and</span><span style="color: rgb(0, 0, 0);"> users.password </span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@password</span><span style="color: rgb(0, 0, 0);"><br /> <br /> </span><span style="color: rgb(0, 0, 255);">IF</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@result</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 0);">''</span><span style="color: rgb(0, 0, 0);"><br /> <br />     </span><span style="color: rgb(0, 0, 255);">BEGIN</span><span style="color: rgb(0, 0, 0);"><br /> <br />        </span><span style="color: rgb(0, 0, 255);">SET</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@fagle</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">NO</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"><br /> <br />     </span><span style="color: rgb(0, 0, 255);">END</span><span style="color: rgb(0, 0, 0);"><br /> <br /> </span><span style="color: rgb(0, 0, 255);">ELSE</span><span style="color: rgb(0, 0, 0);"><br /> <br />     </span><span style="color: rgb(0, 0, 255);">BEGIN</span><span style="color: rgb(0, 0, 0);"><br /> <br />        </span><span style="color: rgb(0, 0, 255);">SET</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@falge</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">YES</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"><br /> <br />     </span><span style="color: rgb(0, 0, 255);">END</span><span style="color: rgb(0, 0, 0);"><br /> <br /> </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@result</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">q回l果</span><span style="color: rgb(0, 128, 128);"><br /> </span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">END</span><span style="color: rgb(0, 0, 0);"><br /> <br /> 二、删除一个函数语?br /> <br /> </span><span style="color: rgb(0, 0, 255);">DROP</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">FUNCTION</span><span style="color: rgb(0, 0, 0);"> my_function<br /> <br /> 三、执行一个函数语?br /> <br /> </span><span style="color: rgb(0, 0, 255);">select</span><span style="color: rgb(0, 0, 0);"> dbo.my_function(<img src="http://www.aygfsteel.com/Images/dot.gif" alt="" />) </span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">Ҏ有没参数来处?/span><span style="color: rgb(0, 128, 128);"><br /> </span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">go</span><span style="color: rgb(0, 0, 0);"><br /> <br /> </span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">注:在SQL Server 2005 中,有内部函C外部函数Q数据库pȝ自带函数Q如sum()Qcount(){等Q这些称为内部函敎ͼ而我们自定义的函数称为外部函数?/span><span style="color: rgb(0, 128, 128);"><br /> </span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">在执行函数语法中Q也有些区别Q如执行内部函数Qselect sum(total) from <img src="http://www.aygfsteel.com/Images/dot.gif" alt="" />Q那么执行外部函数则需要在函数名前加dbo. + 自定义函数名Q如</span><span style="color: rgb(0, 128, 128);"><br /> </span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">select dbo.my_function()</span><span style="color: rgb(0, 128, 128);"><br /> </span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">go</span><span style="color: rgb(0, 128, 128);"><br /> </span><span style="color: rgb(0, 0, 0);"><br /> 四、下面提供二个例?br /> <br /> </span><span style="color: rgb(128, 0, 0); font-weight: bold;">1</span><span style="color: rgb(0, 0, 0);">、日期判断,判断传进来的日期是否在上个月日到本月日之?br /> <br /> </span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">h意convert()函数的用?/span><span style="color: rgb(0, 128, 128);"><br /> </span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">create</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">function</span><span style="color: rgb(0, 0, 0);"> isNewContract(</span><span style="color: rgb(0, 128, 0);">@date</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">varchar</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 0); font-weight: bold;">32</span><span style="color: rgb(0, 0, 0);">))<br /> <br /> </span><span style="color: rgb(0, 0, 255);">returns</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">varchar</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 0); font-weight: bold;">32</span><span style="color: rgb(0, 0, 0);">)<br /> <br /> </span><span style="color: rgb(0, 0, 255);">AS</span><span style="color: rgb(0, 0, 0);"><br /> <br /> </span><span style="color: rgb(0, 0, 255);">begin</span><span style="color: rgb(0, 0, 0);"><br /> <br />     </span><span style="color: rgb(0, 0, 255);">declare</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@begin_time</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">varchar</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 0); font-weight: bold;">32</span><span style="color: rgb(0, 0, 0);">)<br /> <br />     </span><span style="color: rgb(0, 0, 255);">declare</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@end_time</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">varchar</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 0); font-weight: bold;">32</span><span style="color: rgb(0, 0, 0);">)<br /> <br />     </span><span style="color: rgb(0, 0, 255);">declare</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@temp</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">varchar</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 0); font-weight: bold;">4</span><span style="color: rgb(0, 0, 0);">)<br /> <br />     </span><span style="color: rgb(0, 0, 255);">declare</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@result</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">varchar</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 0); font-weight: bold;">32</span><span style="color: rgb(0, 0, 0);">)<br /> <br />     </span><span style="color: rgb(0, 0, 255);">declare</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@month</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">varchar</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 0); font-weight: bold;">2</span><span style="color: rgb(0, 0, 0);">)<br /> <br /> </span><span style="color: rgb(0, 0, 255);">set</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@begin_time</span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(255, 0, 255);">convert</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0); font-weight: bold;">varchar</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(255, 0, 255);">DATEPART</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(255, 0, 255);">year</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(255, 0, 255);">getdate</span><span style="color: rgb(0, 0, 0);">())) </span><span style="color: rgb(128, 128, 128);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">-</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"><br /> <br /> </span><span style="color: rgb(0, 0, 255);">set</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@end_time</span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(255, 0, 255);">convert</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0); font-weight: bold;">varchar</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(255, 0, 255);">DATEPART</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(255, 0, 255);">year</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(255, 0, 255);">getdate</span><span style="color: rgb(0, 0, 0);">())) </span><span style="color: rgb(128, 128, 128);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 255);">convert</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0); font-weight: bold;">varchar</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(255, 0, 255);">DATEPART</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(255, 0, 255);">month</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(255, 0, 255);">getdate</span><span style="color: rgb(0, 0, 0);">())) </span><span style="color: rgb(128, 128, 128);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 255);">convert</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0); font-weight: bold;">varchar</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">14</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /> <br /> </span><span style="color: rgb(0, 0, 255);">set</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@temp</span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(255, 0, 255);">convert</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0); font-weight: bold;">varchar</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 0); font-weight: bold;">2</span><span style="color: rgb(0, 0, 0);">),(</span><span style="color: rgb(255, 0, 255);">month</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(255, 0, 255);">getDate</span><span style="color: rgb(0, 0, 0);">())</span><span style="color: rgb(128, 128, 128);">-</span><span style="color: rgb(128, 0, 0); font-weight: bold;">1</span><span style="color: rgb(0, 0, 0);"> ))<br /> <br /> </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 128, 0);">@temp</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);"><</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0); font-weight: bold;">10</span><span style="color: rgb(0, 0, 0);"> )<br /> <br /> </span><span style="color: rgb(0, 0, 255);">begin</span><span style="color: rgb(0, 0, 0);"><br /> <br />     </span><span style="color: rgb(0, 0, 255);">set</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@begin_time</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@begin_time</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 255);">convert</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0); font-weight: bold;">varchar</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 0); font-weight: bold;">1</span><span style="color: rgb(0, 0, 0);">),</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">0</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(128, 128, 128);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@temp</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">-</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 255);">convert</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0); font-weight: bold;">varchar</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">15</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /> <br /> </span><span style="color: rgb(0, 0, 255);">end</span><span style="color: rgb(0, 0, 0);"><br /> <br /> </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 128, 0);">@date</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">>=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@begin_time</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">and</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@date</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);"><=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@end_time</span><span style="color: rgb(0, 0, 0);">)<br /> <br /> </span><span style="color: rgb(0, 0, 255);">begin</span><span style="color: rgb(0, 0, 0);"><br /> <br /> </span><span style="color: rgb(0, 0, 255);">set</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@result</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">?/span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"><br /> <br /> </span><span style="color: rgb(0, 0, 255);">end</span><span style="color: rgb(0, 0, 0);"><br /> <br /> </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"><br /> <br /> </span><span style="color: rgb(0, 0, 255);">begin</span><span style="color: rgb(0, 0, 0);"><br /> <br /> </span><span style="color: rgb(0, 0, 255);">set</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@result</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@begin_time</span><span style="color: rgb(0, 0, 0);"><br /> <br /> </span><span style="color: rgb(0, 0, 255);">end</span><span style="color: rgb(0, 0, 0);"><br /> <br /> </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@result</span><span style="color: rgb(0, 0, 0);"><br /> <br /> </span><span style="color: rgb(0, 0, 255);">end</span><span style="color: rgb(0, 0, 0);"><br /> <br /> </span><span style="color: rgb(128, 0, 0); font-weight: bold;">2</span><span style="color: rgb(0, 0, 0);">、查询多条记录合q成一条记录返回,q写入EXECL表中Q进行分行换行显C,其中使用游标q行循环处理<br /> <br /> </span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">在数据库中,EXECL默认换行WACSII码ؓ'10',在合q字W串之前Q需要先转ؓEXECL识别的换行符Q具体用chat()内部函数处理</span><span style="color: rgb(0, 128, 128);"><br /> </span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">create</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">function</span><span style="color: rgb(0, 0, 0);"> getExportReportCollect(</span><span style="color: rgb(0, 128, 0);">@projectId</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">int</span><span style="color: rgb(0, 0, 0);"> ,</span><span style="color: rgb(0, 128, 0);">@month</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">varchar</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 0); font-weight: bold;">64</span><span style="color: rgb(0, 0, 0);">))<br /> <br /> </span><span style="color: rgb(0, 0, 255);">returns</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">varchar</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 0); font-weight: bold;">4096</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">讄q回?/span><span style="color: rgb(0, 128, 128);"><br /> </span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">AS</span><span style="color: rgb(0, 0, 0);"><br /> <br /> </span><span style="color: rgb(0, 0, 255);">begin</span><span style="color: rgb(0, 0, 0);"><br /> <br />     </span><span style="color: rgb(0, 0, 255);">declare</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@num</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">varchar</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 0); font-weight: bold;">2</span><span style="color: rgb(0, 0, 0);">)<br /> <br />     </span><span style="color: rgb(0, 0, 255);">declare</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@end</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">varchar</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 0); font-weight: bold;">10</span><span style="color: rgb(0, 0, 0);">)<br /> <br />     </span><span style="color: rgb(0, 0, 255);">declare</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@Result</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">varchar</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 0); font-weight: bold;">4096</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">用于q回查询l果</span><span style="color: rgb(0, 128, 128);"><br /> </span><span style="color: rgb(0, 0, 0);"><br />     </span><span style="color: rgb(0, 0, 255);">declare</span><span style="color: rgb(0, 0, 0);"> city_cursor </span><span style="color: rgb(0, 0, 255);">cursor</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">声明游标变量</span><span style="color: rgb(0, 128, 128);"><br /> </span><span style="color: rgb(0, 0, 0);"><br />     </span><span style="color: rgb(0, 0, 255);">select</span><span style="color: rgb(0, 0, 0);"> reported.id<br /> <br />     </span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> dbo.Investment_Budget_Reported </span><span style="color: rgb(0, 0, 255);">as</span><span style="color: rgb(0, 0, 0);"> reported<br /> <br />     </span><span style="color: rgb(255, 0, 255);">left</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">join</span><span style="color: rgb(0, 0, 0);"> dbo.Investment_Budget_Contract </span><span style="color: rgb(0, 0, 255);">as</span><span style="color: rgb(0, 0, 0);"> con<br /> <br />     </span><span style="color: rgb(0, 0, 255);">on</span><span style="color: rgb(0, 0, 0);"> reported.contractId </span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);"> con.id<br /> <br />     </span><span style="color: rgb(0, 0, 255);">where</span><span style="color: rgb(0, 0, 0);"> reported.</span><span style="color: rgb(255, 0, 0);">[</span><span style="color: rgb(255, 0, 0);">month</span><span style="color: rgb(255, 0, 0);">]</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@month</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">and</span><span style="color: rgb(0, 0, 0);"> reported.projectId </span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@projectId</span><span style="color: rgb(0, 0, 0);"><br /> <br /> </span><span style="color: rgb(0, 0, 255);">set</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@Result</span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(255, 0, 0);">''</span><span style="color: rgb(0, 0, 0);"><br /> <br /> </span><span style="color: rgb(0, 0, 255);">set</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@num</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">1</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"><br /> <br /> </span><span style="color: rgb(0, 0, 255);">set</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@end</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">10</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">导入EXECL表中Q换行符的ACSII?/span><span style="color: rgb(0, 128, 128);"><br /> </span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">declare</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@Field</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">int</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">声明临时存放CityID的变?/span><span style="color: rgb(0, 128, 128);"><br /> </span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">open</span><span style="color: rgb(0, 0, 0);"> city_cursor </span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">打开游标</span><span style="color: rgb(0, 128, 128);"><br /> </span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">fetch</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">next</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> city_cursor </span><span style="color: rgb(0, 0, 255);">into</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@Field</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">实际ID赋给变量</span><span style="color: rgb(0, 128, 128);"><br /> </span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 128, 0); font-weight: bold;">@@fetch_status</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0); font-weight: bold;">0</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">循环开?/span><span style="color: rgb(0, 128, 128);"><br /> </span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">begin</span><span style="color: rgb(0, 0, 0);"><br /> <br />     </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">((</span><span style="color: rgb(0, 0, 255);">select</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 0);">[</span><span style="color: rgb(255, 0, 0);">content</span><span style="color: rgb(255, 0, 0);">]</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> dbo.Investment_Budget_Reported </span><span style="color: rgb(0, 0, 255);">where</span><span style="color: rgb(0, 0, 0);"> id </span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@Field</span><span style="color: rgb(0, 0, 0);"> )</span><span style="color: rgb(0, 0, 255);">is</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">not</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">)<br /> <br />     </span><span style="color: rgb(0, 0, 255);">BEGIN</span><span style="color: rgb(0, 0, 0);"><br /> <br />      <br /> <br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 128, 0);">@Result</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 0);">''</span><span style="color: rgb(0, 0, 0);">)<br /> <br />            </span><span style="color: rgb(0, 0, 255);">select</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@Result</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@Result</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@num</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">?/span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 0);">[</span><span style="color: rgb(255, 0, 0);">content</span><span style="color: rgb(255, 0, 0);">]</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> dbo.Investment_Budget_Reported </span><span style="color: rgb(0, 0, 255);">where</span><span style="color: rgb(0, 0, 0);"> id </span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@Field</span><span style="color: rgb(0, 0, 0);"><br /> <br />        </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"><br /> <br />            </span><span style="color: rgb(0, 0, 255);">select</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@Result</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@Result</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@num</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">?/span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 0);">[</span><span style="color: rgb(255, 0, 0);">content</span><span style="color: rgb(255, 0, 0);">]</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> dbo.Investment_Budget_Reported </span><span style="color: rgb(0, 0, 255);">where</span><span style="color: rgb(0, 0, 0);"> id </span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@Field</span><span style="color: rgb(0, 0, 0);"><br /> <br />     </span><span style="color: rgb(0, 0, 255);">set</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@Result</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@Result</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0); font-weight: bold;">char</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 128, 0);">@end</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">让换行符转ؓEXECL认识的换行符</span><span style="color: rgb(0, 128, 128);"><br /> </span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">set</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@num</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@num</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0); font-weight: bold;">1</span><span style="color: rgb(0, 0, 0);"><br /> <br />     </span><span style="color: rgb(0, 0, 255);">END</span><span style="color: rgb(0, 0, 0);"><br /> <br />        </span><span style="color: rgb(0, 0, 255);">fetch</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">next</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> city_cursor </span><span style="color: rgb(0, 0, 255);">into</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@Field</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">下一个reportId</span><span style="color: rgb(0, 128, 128);"><br /> </span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">end</span><span style="color: rgb(0, 0, 0);"><br /> <br /> </span><span style="color: rgb(0, 0, 255);">close</span><span style="color: rgb(0, 0, 0);"> city_cursor </span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">关闭游标</span><span style="color: rgb(0, 128, 128);"><br /> </span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">deallocate</span><span style="color: rgb(0, 0, 0);"> city_cursor </span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">释放游标引用</span><span style="color: rgb(0, 128, 128);"><br /> </span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@Result</span><span style="color: rgb(0, 0, 0);"><br /> <br /> </span><span style="color: rgb(0, 0, 255);">end</span><span style="color: rgb(0, 0, 0);"><br /> <br /> <br /> 本文来自CSDN博客Q{载请标明出处Qhttp:</span><span style="color: rgb(128, 128, 128);">//</span><span style="color: rgb(0, 0, 0);">blog.csdn.net</span><span style="color: rgb(128, 128, 128);">/</span><span style="color: rgb(0, 0, 0);">zhdd1234</span><span style="color: rgb(128, 128, 128);">/</span><span style="color: rgb(0, 0, 0);">archive</span><span style="color: rgb(128, 128, 128);">/</span><span style="color: rgb(128, 0, 0); font-weight: bold;">2009</span><span style="color: rgb(128, 128, 128);">/</span><span style="color: rgb(128, 0, 0); font-weight: bold;">10</span><span style="color: rgb(128, 128, 128);">/</span><span style="color: rgb(128, 0, 0); font-weight: bold;">27</span><span style="color: rgb(128, 128, 128);">/</span><span style="color: rgb(128, 0, 0); font-weight: bold;">4734789</span><span style="color: rgb(0, 0, 0);">.aspx</span></div> <img src ="http://www.aygfsteel.com/Reg/aggbug/340916.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/Reg/" target="_blank">艾L</a> 2010-12-16 19:38 <a href="http://www.aygfsteel.com/Reg/archive/2010/12/16/340916.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title><img src='http://blog.csdn.net/images/turnship.gif'/>sqlserver sql常用函数,语法http://www.aygfsteel.com/Reg/archive/2010/11/23/338758.html艾L艾LTue, 23 Nov 2010 02:21:00 GMThttp://www.aygfsteel.com/Reg/archive/2010/11/23/338758.htmlhttp://www.aygfsteel.com/Reg/comments/338758.htmlhttp://www.aygfsteel.com/Reg/archive/2010/11/23/338758.html#Feedback0http://www.aygfsteel.com/Reg/comments/commentRss/338758.htmlhttp://www.aygfsteel.com/Reg/services/trackbacks/338758.html阅读全文

艾L 2010-11-23 10:21 发表评论
]]>
SQLServer 实现rownum 的功?/title><link>http://www.aygfsteel.com/Reg/archive/2010/07/28/327358.html</link><dc:creator>艾L</dc:creator><author>艾L</author><pubDate>Wed, 28 Jul 2010 10:50:00 GMT</pubDate><guid>http://www.aygfsteel.com/Reg/archive/2010/07/28/327358.html</guid><wfw:comment>http://www.aygfsteel.com/Reg/comments/327358.html</wfw:comment><comments>http://www.aygfsteel.com/Reg/archive/2010/07/28/327358.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/Reg/comments/commentRss/327358.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/Reg/services/trackbacks/327358.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"><span style="color: #000000">Ҏ1: <br /> <br /> </span><span style="color: #0000ff">with</span><span style="color: #000000"> </span><span style="color: #0000ff">temp</span><span style="color: #000000"> </span><span style="color: #0000ff">as</span><span style="color: #000000"> <br /> <br /> ( </span><span style="color: #0000ff">select</span><span style="color: #000000"> row_number() </span><span style="color: #0000ff">over</span><span style="color: #000000">(</span><span style="color: #0000ff">order</span><span style="color: #000000"> </span><span style="color: #0000ff">by</span><span style="color: #000000"> cityID) </span><span style="color: #0000ff">as</span><span style="color: #000000"> rownum,cityName </span><span style="color: #0000ff">from</span><span style="color: #000000"> city ) <br /> <br /> </span><span style="color: #0000ff">select</span><span style="color: #000000"> </span><span style="color: #808080">*</span><span style="color: #000000"> </span><span style="color: #0000ff">from</span><span style="color: #000000"> </span><span style="color: #0000ff">temp</span><span style="color: #000000"> </span><span style="color: #0000ff">where</span><span style="color: #000000"> rownum </span><span style="color: #808080">between</span><span style="color: #000000"> </span><span style="font-weight: bold; color: #800000">10</span><span style="color: #000000"> </span><span style="color: #808080">and</span><span style="color: #000000"> </span><span style="font-weight: bold; color: #800000">20</span><span style="color: #000000"> <br /> <br /> </span><span style="color: #0000ff">go</span><span style="color: #000000"> <br /> <br /> 解释: <br /> <br /> </span><span style="font-weight: bold; color: #800000">1</span><span style="color: #000000"> 此方法把括号里的查询l果攑ֈ变量:</span><span style="color: #0000ff">temp</span><span style="color: #000000"> 里面( 我也不确定是不是变量), q用row_number() 函数q行一个行可t? 再用over 函数q行一个列的排序规? 是这必须?, q指定列名ؓ</span><span style="color: #ff0000">'</span><span style="color: #ff0000">rownum</span><span style="color: #ff0000">'</span><span style="color: #000000"> <br /> <br /> </span><span style="font-weight: bold; color: #800000">2</span><span style="color: #000000"> 紧接着在下面的语句可以?nbsp;</span><span style="color: #ff0000">'</span><span style="color: #ff0000">rownum</span><span style="color: #ff0000">'</span><span style="color: #000000"> q行一个指定行L查询 <br /> <br /> </span><span style="font-weight: bold; color: #800000">3</span><span style="color: #000000"> 此批语句执行完毕? 变量:</span><span style="color: #0000ff">temp</span><span style="color: #000000"> 释放 <br /> <br /> Ҏ2: <br /> <br /> </span><span style="color: #0000ff">select</span><span style="color: #000000"> </span><span style="color: #ff00ff">identity</span><span style="color: #000000">(</span><span style="font-weight: bold; color: #000000">int</span><span style="color: #000000">,</span><span style="font-weight: bold; color: #800000">1</span><span style="color: #000000">,</span><span style="font-weight: bold; color: #800000">1</span><span style="color: #000000">) </span><span style="color: #0000ff">as</span><span style="color: #000000"> rownum, cityName </span><span style="color: #0000ff">into</span><span style="color: #000000"> #</span><span style="color: #0000ff">temp</span><span style="color: #000000"> </span><span style="color: #0000ff">from</span><span style="color: #000000"> city <br /> <br /> </span><span style="color: #0000ff">select</span><span style="color: #000000"> </span><span style="color: #808080">*</span><span style="color: #000000"> </span><span style="color: #0000ff">from</span><span style="color: #000000"> #</span><span style="color: #0000ff">temp</span><span style="color: #000000"> </span><span style="color: #0000ff">where</span><span style="color: #000000"> rownum </span><span style="color: #808080">between</span><span style="color: #000000"> </span><span style="font-weight: bold; color: #800000">10</span><span style="color: #000000"> </span><span style="color: #808080">and</span><span style="color: #000000"> </span><span style="font-weight: bold; color: #800000">20</span><span style="color: #000000"> <br /> <br /> </span><span style="color: #0000ff">go</span><span style="color: #000000"> <br /> <br /> 解释: <br /> <br /> 此方法跟上面的差不多的意? 只不q把 row_number() 函数换成?nbsp;</span><span style="color: #ff00ff">identity</span><span style="color: #000000">() 函数 <br /> <br /> q把l果集放在一个时表里面, 当批语句执行完毕, 此时表q可以?nbsp;<br /> <br /> </span></div> <img src ="http://www.aygfsteel.com/Reg/aggbug/327358.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/Reg/" target="_blank">艾L</a> 2010-07-28 18:50 <a href="http://www.aygfsteel.com/Reg/archive/2010/07/28/327358.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>sql语句格式化数?前面?)的参考示?/title><link>http://www.aygfsteel.com/Reg/archive/2010/07/27/327243.html</link><dc:creator>艾L</dc:creator><author>艾L</author><pubDate>Tue, 27 Jul 2010 10:13:00 GMT</pubDate><guid>http://www.aygfsteel.com/Reg/archive/2010/07/27/327243.html</guid><wfw:comment>http://www.aygfsteel.com/Reg/comments/327243.html</wfw:comment><comments>http://www.aygfsteel.com/Reg/archive/2010/07/27/327243.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/Reg/comments/commentRss/327243.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/Reg/services/trackbacks/327243.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"><span style="color: #000000">一个数字例?3Q或1使用t</span><span style="color: #808080">-</span><span style="color: #000000">sql语句转换?33?01 以下是详l分析: <br /> </span><span style="font-weight: bold; color: #800000">1</span><span style="color: #000000">?/span><span style="color: #0000ff">select</span><span style="color: #000000"> </span><span style="color: #ff00ff">power</span><span style="color: #000000">(</span><span style="font-weight: bold; color: #800000">10</span><span style="color: #000000">,</span><span style="font-weight: bold; color: #800000">3</span><span style="color: #000000">)得到1000 <br /> </span><span style="font-weight: bold; color: #800000">2</span><span style="color: #000000">?/span><span style="color: #0000ff">select</span><span style="color: #000000"> </span><span style="color: #ff00ff">cast</span><span style="color: #000000">(</span><span style="font-weight: bold; color: #800000">1000</span><span style="color: #808080">+</span><span style="font-weight: bold; color: #800000">33</span><span style="color: #000000"> </span><span style="color: #0000ff">as</span><span style="color: #000000"> </span><span style="font-weight: bold; color: #000000">varchar</span><span style="color: #000000">Q?nbsp;?000转换cd <br /> </span><span style="font-weight: bold; color: #800000">3</span><span style="color: #000000">?/span><span style="color: #0000ff">select</span><span style="color: #000000"> </span><span style="color: #ff00ff">right</span><span style="color: #000000">(</span><span style="font-weight: bold; color: #800000">100033</span><span style="color: #000000">,</span><span style="font-weight: bold; color: #800000">3</span><span style="color: #000000">) 从右边取3个字W得?33 ?格式化同?nbsp;</span><span style="color: #0000ff">select</span><span style="color: #000000"> </span><span style="color: #ff00ff">right</span><span style="color: #000000">(</span><span style="color: #ff00ff">cast</span><span style="color: #000000">(</span><span style="color: #ff00ff">power</span><span style="color: #000000">(</span><span style="font-weight: bold; color: #800000">10</span><span style="color: #000000">,</span><span style="font-weight: bold; color: #800000">3</span><span style="color: #000000">) </span><span style="color: #0000ff">as</span><span style="color: #000000"> </span><span style="font-weight: bold; color: #000000">varchar</span><span style="color: #000000">)</span><span style="color: #808080">+</span><span style="font-weight: bold; color: #800000">33</span><span style="color: #000000">,</span><span style="font-weight: bold; color: #800000">3</span><span style="color: #000000">)<br /> <br /> </span></div> <img src ="http://www.aygfsteel.com/Reg/aggbug/327243.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/Reg/" target="_blank">艾L</a> 2010-07-27 18:13 <a href="http://www.aygfsteel.com/Reg/archive/2010/07/27/327243.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>l典的SQLhttp://www.aygfsteel.com/Reg/archive/2010/06/24/324290.html艾L艾LWed, 23 Jun 2010 16:43:00 GMThttp://www.aygfsteel.com/Reg/archive/2010/06/24/324290.htmlhttp://www.aygfsteel.com/Reg/comments/324290.htmlhttp://www.aygfsteel.com/Reg/archive/2010/06/24/324290.html#Feedback0http://www.aygfsteel.com/Reg/comments/commentRss/324290.htmlhttp://www.aygfsteel.com/Reg/services/trackbacks/324290.html 代码如下

select a.[id],a.mark from 

select [page].[id],100 as mark from [page] where [page].[url] like '%baidu%' 
union 
select [page].[id],50 as mark from [page] where [page].[title] like '%baidu%' 
union 
select [page].[id],10 as mark from [page] where [page].[body] like '%baidu%' 
as a  order by mark desc  


用union 实现联合查询Q在每个查询语句中定义一个时变量mark q给mark赋|在最后的输出旉用mark来排序,q样实现Q非常简单,我感觉这题更多考研我们的编E思想?


艾L 2010-06-24 00:43 发表评论
]]>
Oracle 游标ȝ+整理http://www.aygfsteel.com/Reg/archive/2010/02/24/313823.html艾L艾LWed, 24 Feb 2010 09:20:00 GMThttp://www.aygfsteel.com/Reg/archive/2010/02/24/313823.htmlhttp://www.aygfsteel.com/Reg/comments/313823.htmlhttp://www.aygfsteel.com/Reg/archive/2010/02/24/313823.html#Feedback2http://www.aygfsteel.com/Reg/comments/commentRss/313823.htmlhttp://www.aygfsteel.com/Reg/services/trackbacks/313823.html阅读全文

艾L 2010-02-24 17:20 发表评论
]]>
Oracle SQL 查询不区分大写解决Ҏ(java) <font color=red>[贴]</font>http://www.aygfsteel.com/Reg/archive/2009/12/14/305913.html艾L艾LMon, 14 Dec 2009 08:09:00 GMThttp://www.aygfsteel.com/Reg/archive/2009/12/14/305913.htmlhttp://www.aygfsteel.com/Reg/comments/305913.htmlhttp://www.aygfsteel.com/Reg/archive/2009/12/14/305913.html#Feedback0http://www.aygfsteel.com/Reg/comments/commentRss/305913.htmlhttp://www.aygfsteel.com/Reg/services/trackbacks/305913.htmlselect * from tablename where upper(ziduan) like '%" + ziduan.toUpperCase() + "%'"
sql += " and upper(t2.customer_name) like '%" + customerName.toUpperCase() + "%'";


艾L 2009-12-14 16:09 发表评论
]]>
Oracle 随机记录<font color=red>新手晋</font>http://www.aygfsteel.com/Reg/archive/2009/11/20/303044.html艾L艾LFri, 20 Nov 2009 06:50:00 GMThttp://www.aygfsteel.com/Reg/archive/2009/11/20/303044.htmlhttp://www.aygfsteel.com/Reg/comments/303044.htmlhttp://www.aygfsteel.com/Reg/archive/2009/11/20/303044.html#Feedback0http://www.aygfsteel.com/Reg/comments/commentRss/303044.htmlhttp://www.aygfsteel.com/Reg/services/trackbacks/303044.html--查询一条随机数上来

select * from tablename where t_id=(select trunc(dbms_random.value(min(条g),max(条g))) from tablename;

select * from dept where did=(select trunc(dbms_random.value(min(rownum),max(rownum))) from dept);

--查询N条随录上?/strong>

select * frmo (select * from tablename order by sys_guid()) where rownum<N

select * from (select * from dept order by sys_guid()) where rownum < 5;

--查询中X条中从N到M条特定记录上来(分页查询Q?/strong>


select * from (select rownum r,t.* from tablename t where rownum<X) ss where ss.r>N and ss.r <=M

select * from (select rownum r, t.* from dept t where rownum<6) ss where ss.r > 2 and ss.r <= 10

艾L 2009-11-20 14:50 发表评论
]]>
替换likeQsql语句更有效率<font color=red>【{?lt;/font>http://www.aygfsteel.com/Reg/archive/2009/11/18/302828.html艾L艾LWed, 18 Nov 2009 10:04:00 GMThttp://www.aygfsteel.com/Reg/archive/2009/11/18/302828.htmlhttp://www.aygfsteel.com/Reg/comments/302828.htmlhttp://www.aygfsteel.com/Reg/archive/2009/11/18/302828.html#Feedback1http://www.aygfsteel.com/Reg/comments/commentRss/302828.htmlhttp://www.aygfsteel.com/Reg/services/trackbacks/302828.html 

替换likeQsql语句更有效率

提到Like语句大家都很熟悉Q比如查扄户名包含?c"的所有用? 我们可以?

use mydatabase
select * from table1 where username like'%c%"

以下是完成上面功能的另一U写?

use mydatabase
select * from table1 where charindex('c',username)>0

q种Ҏ理论上比上一U方法多了一个判断语??gt;0, 但这个判断过E是最快的, 我相?0%以上的运都是花在查扑֭W串及其它的q算? 所以运用charindex函数也没什么大不了。用q种Ҏ也有好处, 那就是对%,|{在不能直接用like 查找到的字符中可以直接在qcharindex中运? 如下:

use mydatabase
select * from table1 where charindex('%',username)>0

大家q可以写?

 

use mydatabase
select * from table1 where charindex(char(37),username)>0

ASCII的字W即?



艾L 2009-11-18 18:04 发表评论
]]>
ORACLE查询树型关系<font color=#F1AF03>(start with connect by prior)</font><font color=red>【{?lt;/font>http://www.aygfsteel.com/Reg/archive/2009/11/18/302826.html艾L艾LWed, 18 Nov 2009 10:00:00 GMThttp://www.aygfsteel.com/Reg/archive/2009/11/18/302826.htmlhttp://www.aygfsteel.com/Reg/comments/302826.htmlhttp://www.aygfsteel.com/Reg/archive/2009/11/18/302826.html#Feedback0http://www.aygfsteel.com/Reg/comments/commentRss/302826.htmlhttp://www.aygfsteel.com/Reg/services/trackbacks/302826.html 

oracle中的select语句可以用START WITHCONNECT BY PRIOR子句实现递归查询Qconnect by 是结构化查询中用到的Q其基本语法是: 

select 
* from tablename start with cond1 

connect by cond2 

where cond3; 

单说来是一个树状结构存储在一张表里,比如一个表中存在两个字D? 

id,parentid那么通过表示每一条记录的parent是谁Q就可以形成一个树状结构?nbsp;

用上q语法的查询可以取得q棵树的所有记录?nbsp;

其中COND1是根l点的限定语句,当然可以攑֮限定条gQ以取得多个根结点,实际是多棵树?nbsp;

COND2是连接条Ӟ其中用PRIOR表示上一条记录,比如 CONNECT BY PRIOR ID
=PRAENTID是说上一条记录的ID是本条记录的PRAENTIDQ即本记录的父亲是上一条记录?nbsp;

COND3是过滤条Ӟ用于对返回的所有记录进行过滤?nbsp;

对于oracleq行单树查询(递归查询) 

DEPTID           NUMBER                   部门id 

PAREDEPTID   NUMBER                   爉门id(所属部门id) 

NAME              CHAR (
40 Byte)        部门名称 

 

通过子节点向根节点追? 

select 
* from persons.dept start with deptid=76 connect by prior paredeptid=deptid

通过根节炚w历子节点. 

select 
* from persons.dept start with paredeptid=0 connect by prior deptid=paredeptid

可通过level 关键字查询所在层? 

select a.
*,level from persons.dept a start with paredeptid=0 connect by prior deptid=paredeptid

PSQstart with 后面所跟的是是递归的种子,也就是递归开始的地方Q?br />
connect by  prior后面的字D顺序是有讲I的Q?nbsp;

若prior~省Q则只能查询到符合条件的起始行,q不q行递归查询Q?nbsp;

 例: 

select 
* from table

start with org_id 
= 'HBHqfWGWPy' 

connect by prior org_id 
= parent_id; 

  

单说来是一个树状结构存储在一张表里,比如一个表中存在两个字D? 

org_id,parent_id那么通过表示每一条记录的parent是谁Q就可以形成一个树状结构?nbsp;

用上q语法的查询可以取得q棵树的所有记录?nbsp;

其中Q?nbsp;

条g1 是根l点的限定语句,当然可以攑֮限定条gQ以取得多个根结点,实际是多棵树?nbsp;

条g2 是连接条Ӟ其中用PRIOR表示上一条记录, 

比如 CONNECT BY PRIOR org_id 
= parent_id是说上一条记录的org_id 是本条记录的parent_idQ即本记录的父亲是上一条记录?nbsp;

条g3 是过滤条Ӟ用于对返回的所有记录进行过滤?nbsp;

  

单介l如下: 

在扫描树l构表时Q需要依此访问树l构的每个节点,一个节点只能访问一ơ,其访问的步骤如下Q?nbsp;

W一步:从根节点开始; 

W二步:讉K该节点; 

W三步:判断该节Ҏ无未被访问的子节点,若有Q则转向它最左侧的未被访问的子节点,q执行第二步Q否则执行第四步Q?nbsp;

W四步:若该节点为根节点Q则讉K完毕Q否则执行第五步Q?nbsp;

W五步:q回到该节点的父节点Qƈ执行W三步骤?nbsp;

MQ扫描整个树l构的过E也x中序遍历树的q程?nbsp;

  

1Q?nbsp;树结构的描述 

树结构的数据存放在表中,数据之间的层ơ关pd父子关系Q通过表中的列与列间的关系来描qͼ?nbsp;EMP 表中?nbsp;EMPNO ?nbsp;MGR ?nbsp;EMPNO 表示该雇员的~号Q?nbsp;MGR 表示领导该雇员的人的~号Q即子节点的 MGR 值等于父节点?nbsp;EMPNO 倹{在表的每一行中都有一个表C父节点?nbsp;MGR Q除根节点外Q,通过每个节点的父节点Q就可以定整个树结构?nbsp;

?nbsp;SELECT 命o中?nbsp;CONNECT BY ?nbsp;START WITH 子句可以查询表中的树型结构关pR其命o格式如下Q?nbsp;

SELECT 。。?nbsp;

CONNECT BY 
{PRIOR 列名 1= 列名 2| 列名 1=PRIOR 裂名 2}

[START WITH] Q?nbsp;

其中Q?nbsp;CONNECT BY 子句说明每行数据是按层ơ顺序检索,q规定将表中的数据连入树型结构的关系中?nbsp;PRIORY q算W必L|在q接关系的两列中某一个的前面。对于节炚w的父子关p, PRIOR q算W在一侧表C父节点Q在另一侧表C子节点Q从而确定查找树l构是的序是自向下还是自底向上。在q接关系中,除了可以使用列名外,q允怋用列表达式?nbsp;START WITH 子句为可选项Q用来标识哪个节点作为查找树型结构的根节炏V若该子句被省略Q则表示所有满x询条件的行作为根节点?nbsp;

START WITHQ?nbsp;不但可以指定一个根节点Q还可以指定多个根节炏V?nbsp;

 

2Q?nbsp;关于 PRIOR

q算W?nbsp;PRIOR 被放|于{号前后的位|,军_着查询时的索顺序?nbsp;

PRIOR 被置?nbsp;CONNECT BY 子句中等L前面Ӟ则强制从根节点到叶节点的序索,即由父节点向子节Ҏ向通过树结构,我们UC向?nbsp;的方式。如Q?nbsp;

CONNECT BY PRIOR EMPNO
=MGR 

PIROR q算W被|于 CONNECT BY 子句中等L后面Ӟ则强制从叶节点到根节点的序索,即由子节点向父节Ҏ向通过树结构,我们UC底向?nbsp;的方式。例如: 

CONNECT BY EMPNO
=PRIOR MGR 

在这U方式中也应指定一个开始的节点?nbsp;

 

3Q?nbsp;定义查找起始节点 

在自向下查询树l构Ӟ不但可以从根节点开始,q可以定义Q何节点ؓ起始节点Q以此开始向下查找。这h扄l果是以该节点为开始的l构树的一枝?nbsp;

 

4Q?nbsp;LEVEL

在具有树l构的表中,每一行数据都是树l构中的一个节点,׃节点所处的层次位置不同Q所以每行记录都可以有一个层受层h据节点与根节点的距离定。不Z哪个节点开始,该v始根节点的层号始lؓ 
1 Q根节点的子节点?nbsp;2 Q?nbsp;依此cL?nbsp;

 

5Q节点和分支的裁?nbsp;

在对树结构进行查询时Q可以去掉表中的某些行,也可以剪掉树中的一个分支,使用 WHERE 子句来限定树型结构中的单个节点,以去掉树中的单个节点Q但它却不媄响其后代节点Q自向下检索时Q或前辈节点Q自底向检索时Q?nbsp;

6Q排序显C?nbsp;

象在其它查询中一P在树l构查询中也可以使用 ORDER BY 子句Q改变查询结果的昄序Q而不必按照遍历树l构的顺序?nbsp;


艾L 2009-11-18 18:00 发表评论
]]>
վ֩ģ壺 | ȫ| ̨| Ȫ| ƽ| | ֹ| | | ʡ| ΢ɽ| ϻ| | | ư| | | ̨| ¡| | | | ɣ| | ij| | | ƽ| ɽ| ϻ| | | | ؿ˹| | | Ǩ| | | | ߰|