﻿<?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>BlogJava-JAVA学习点点滴滴-随笔分类-数据库</title><link>http://www.blogjava.net/mkchen/category/19009.html</link><description>用开放的脑子去闯荡;用开阔的视野去拼搏;用平和的身心去磨练;用美好的理想去追求!</description><language>zh-cn</language><lastBuildDate>Thu, 08 Mar 2007 06:41:57 GMT</lastBuildDate><pubDate>Thu, 08 Mar 2007 06:41:57 GMT</pubDate><ttl>60</ttl><item><title>[转]sqlserver中,sql编程的几个小常识</title><link>http://www.blogjava.net/mkchen/archive/2007/01/07/92212.html</link><dc:creator>海思</dc:creator><author>海思</author><pubDate>Sun, 07 Jan 2007 06:34:00 GMT</pubDate><guid>http://www.blogjava.net/mkchen/archive/2007/01/07/92212.html</guid><wfw:comment>http://www.blogjava.net/mkchen/comments/92212.html</wfw:comment><comments>http://www.blogjava.net/mkchen/archive/2007/01/07/92212.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/mkchen/comments/commentRss/92212.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/mkchen/services/trackbacks/92212.html</trackback:ping><description><![CDATA[
		<p>1、取出刚刚插入(删除)的数据SELECT 字段名 FROM INSERTED(DELETED)<br />2、对于UPDATE实际上是先DELETE然后再INSERT所以如果想得到UPDATE前后的数据值，应该先从DELETED取出，然后从INSERTED取出；<br />3、IF UPDATE(列名)可以判断更新或插入哪一个字段的值；<br />4、@@ROWCOUNT可以判断上一行查询操作得到的列数；<br />5、给变量赋值用SET @ZQB = 13;<br />6、察看是否有符合条件的记录IF EXISTS (SELECT name FROM sysobjects WHERE name = 'reminder' AND type = 'TR');<br />7、定义游标，如下：<br />DECLARE c1 CURSOR FOR<br />SELECT emp_mgr.emp<br />FROM emp_mgr, inserted<br />WHERE emp_mgr.emp = inserted.mgr 
</p>
		<p>OPEN c1<br />FETCH NEXT FROM c1 INTO @e--从游标中取出数据<br />WHILE @@fetch_status = 0--判断是否到最后<br />BEGIN<br />UPDATE emp_mgr<br />SET emp_mgr.NoOfReports = emp_mgr.NoOfReports + 1 -- Add 1 for newly<br />WHERE emp_mgr.emp = @e -- added employee.</p>
		<p>FETCH NEXT FROM c1 INTO @e<br />END<br />CLOSE c1<br />DEALLOCATE c1--删除游标引用 </p>
<img src ="http://www.blogjava.net/mkchen/aggbug/92212.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/mkchen/" target="_blank">海思</a> 2007-01-07 14:34 <a href="http://www.blogjava.net/mkchen/archive/2007/01/07/92212.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>是精妙SQL语句吗？？？(转) </title><link>http://www.blogjava.net/mkchen/archive/2006/02/22/32023.html</link><dc:creator>海思</dc:creator><author>海思</author><pubDate>Wed, 22 Feb 2006 14:04:00 GMT</pubDate><guid>http://www.blogjava.net/mkchen/archive/2006/02/22/32023.html</guid><wfw:comment>http://www.blogjava.net/mkchen/comments/32023.html</wfw:comment><comments>http://www.blogjava.net/mkchen/archive/2006/02/22/32023.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/mkchen/comments/commentRss/32023.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/mkchen/services/trackbacks/32023.html</trackback:ping><description><![CDATA[<DIV class=content>说明：复制表(只复制结构,源表名：a &nbsp;新表名：b)<BR>SQL: &nbsp;select &nbsp;* &nbsp;into &nbsp;b &nbsp;from &nbsp;a &nbsp;where &nbsp;1&lt;&gt;1<BR>说明：拷贝表(拷贝数据,源表名：a &nbsp;目标表名：b)<BR>SQL: &nbsp;insert &nbsp;into &nbsp;b(a, &nbsp;b, &nbsp;c) &nbsp;select &nbsp;d,e,f &nbsp;from &nbsp;b;<BR>说明：显示文章、提交人和最后回复时间<BR>SQL: &nbsp;select &nbsp;a.title,a.username,b.adddate &nbsp;from &nbsp;table &nbsp;a,(select &nbsp;max(adddate) &nbsp;adddate &nbsp;from &nbsp;table &nbsp;where &nbsp;table.title=a.title) &nbsp;b<BR>说明：外连接查询(表名1：a &nbsp;表名2：b)<BR>SQL: &nbsp;select &nbsp;a.a, &nbsp;a.b, &nbsp;a.c, &nbsp;b.c, &nbsp;b.d, &nbsp;b.f &nbsp;from &nbsp;a &nbsp;LEFT &nbsp;OUT &nbsp;JOIN &nbsp;b &nbsp;ON &nbsp;a.a &nbsp;= &nbsp;b.c<BR>说明：日程安排提前五分钟提醒<BR>SQL: &nbsp; &nbsp;select &nbsp;* &nbsp;from &nbsp;日程安排 &nbsp;where &nbsp;datediff('minute',f开始时间,getdate())&gt;5<BR>说明：两张关联表，删除主表中已经在副表中没有的信息<BR>SQL: &nbsp; &nbsp;<BR>delete &nbsp;from &nbsp;info &nbsp;where &nbsp;not &nbsp;exists &nbsp;( &nbsp;select &nbsp;* &nbsp;from &nbsp;infobz &nbsp;where &nbsp;info.infid=infobz.infid &nbsp;) &nbsp;<BR>说明：--<BR>SQL: &nbsp; &nbsp;<BR>SELECT &nbsp;A.NUM, &nbsp;A.NAME, &nbsp;B.UPD_DATE, &nbsp;B.PREV_UPD_DATE<BR>&nbsp; &nbsp;FROM &nbsp;TABLE1, &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp;(SELECT &nbsp;X.NUM, &nbsp;X.UPD_DATE, &nbsp;Y.UPD_DATE &nbsp;PREV_UPD_DATE<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FROM &nbsp;(SELECT &nbsp;NUM, &nbsp;UPD_DATE, &nbsp;INBOUND_QTY, &nbsp;STOCK_ONHAND<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FROM &nbsp;TABLE2<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WHERE &nbsp;TO_CHAR(UPD_DATE,'YYYY/MM') &nbsp;= &nbsp;TO_CHAR(SYSDATE, &nbsp;'YYYY/MM')) &nbsp;X, &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(SELECT &nbsp;NUM, &nbsp;UPD_DATE, &nbsp;STOCK_ONHAND<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FROM &nbsp;TABLE2<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WHERE &nbsp;TO_CHAR(UPD_DATE,'YYYY/MM') &nbsp;= &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TO_CHAR(TO_DATE(TO_CHAR(SYSDATE, &nbsp;'YYYY/MM') &nbsp;¦¦ &nbsp;'/01','YYYY/MM/DD') &nbsp;- &nbsp;1, &nbsp;'YYYY/MM') &nbsp;) &nbsp;Y, &nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WHERE &nbsp;X.NUM &nbsp;= &nbsp;Y.NUM &nbsp;（+）<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AND &nbsp;X.INBOUND_QTY &nbsp;+ &nbsp;NVL(Y.STOCK_ONHAND,0) &nbsp;&lt;&gt; &nbsp;X.STOCK_ONHAND &nbsp;) &nbsp;B<BR>WHERE &nbsp;A.NUM &nbsp;= &nbsp;B.NUM<BR>说明：--<BR>SQL: &nbsp; &nbsp;<BR>select &nbsp;* &nbsp;from &nbsp;studentinfo &nbsp;where &nbsp;not &nbsp;exists(select &nbsp;* &nbsp;from &nbsp;student &nbsp;where &nbsp;studentinfo.id=student.id) &nbsp;and &nbsp;系名称='"&amp;strdepartmentname&amp;"' &nbsp;and &nbsp;专业名称='"&amp;strprofessionname&amp;"' &nbsp;order &nbsp;by &nbsp;性别,生源地,高考总成绩<BR>说明：<BR>从数据库中去一年的各单位电话费统计(电话费定额贺电化肥清单两个表来源）<BR>SQL: &nbsp;<BR>SELECT &nbsp;a.userper, &nbsp;a.tel, &nbsp;a.standfee, &nbsp;TO_CHAR(a.telfeedate, &nbsp;'yyyy') &nbsp;AS &nbsp;telyear,<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SUM(decode(TO_CHAR(a.telfeedate, &nbsp;'mm'), &nbsp;'01', &nbsp;a.factration)) &nbsp;AS &nbsp;JAN,<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SUM(decode(TO_CHAR(a.telfeedate, &nbsp;'mm'), &nbsp;'02', &nbsp;a.factration)) &nbsp;AS &nbsp;FRI,<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SUM(decode(TO_CHAR(a.telfeedate, &nbsp;'mm'), &nbsp;'03', &nbsp;a.factration)) &nbsp;AS &nbsp;MAR,<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SUM(decode(TO_CHAR(a.telfeedate, &nbsp;'mm'), &nbsp;'04', &nbsp;a.factration)) &nbsp;AS &nbsp;APR,<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SUM(decode(TO_CHAR(a.telfeedate, &nbsp;'mm'), &nbsp;'05', &nbsp;a.factration)) &nbsp;AS &nbsp;MAY,<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SUM(decode(TO_CHAR(a.telfeedate, &nbsp;'mm'), &nbsp;'06', &nbsp;a.factration)) &nbsp;AS &nbsp;JUE,<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SUM(decode(TO_CHAR(a.telfeedate, &nbsp;'mm'), &nbsp;'07', &nbsp;a.factration)) &nbsp;AS &nbsp;JUL,<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SUM(decode(TO_CHAR(a.telfeedate, &nbsp;'mm'), &nbsp;'08', &nbsp;a.factration)) &nbsp;AS &nbsp;AGU,<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SUM(decode(TO_CHAR(a.telfeedate, &nbsp;'mm'), &nbsp;'09', &nbsp;a.factration)) &nbsp;AS &nbsp;SEP,<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SUM(decode(TO_CHAR(a.telfeedate, &nbsp;'mm'), &nbsp;'10', &nbsp;a.factration)) &nbsp;AS &nbsp;OCT,<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SUM(decode(TO_CHAR(a.telfeedate, &nbsp;'mm'), &nbsp;'11', &nbsp;a.factration)) &nbsp;AS &nbsp;NOV,<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SUM(decode(TO_CHAR(a.telfeedate, &nbsp;'mm'), &nbsp;'12', &nbsp;a.factration)) &nbsp;AS &nbsp;DEC<BR>FROM &nbsp;(SELECT &nbsp;a.userper, &nbsp;a.tel, &nbsp;a.standfee, &nbsp;b.telfeedate, &nbsp;b.factration<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FROM &nbsp;TELFEESTAND &nbsp;a, &nbsp;TELFEE &nbsp;b<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WHERE &nbsp;a.tel &nbsp;= &nbsp;b.telfax) &nbsp;a<BR>GROUP &nbsp;BY &nbsp;a.userper, &nbsp;a.tel, &nbsp;a.standfee, &nbsp;TO_CHAR(a.telfeedate, &nbsp;'yyyy')<BR>说明：四表联查问题：<BR>SQL: &nbsp;select &nbsp;* &nbsp;from &nbsp;a &nbsp;left &nbsp;inner &nbsp;join &nbsp;b &nbsp;on &nbsp;a.a=b.b &nbsp;right &nbsp;inner &nbsp;join &nbsp;c &nbsp;on &nbsp;a.a=c.c &nbsp; &nbsp;inner &nbsp;join &nbsp;d &nbsp;on &nbsp;a.a=d.d &nbsp;where &nbsp;.....<BR><BR>说明：得到表中最小的未使用的ID号<BR>SQL:<BR>SELECT &nbsp;(CASE &nbsp;WHEN &nbsp;EXISTS(SELECT &nbsp;* &nbsp;FROM &nbsp;Handle &nbsp;b &nbsp;WHERE &nbsp;b.HandleID &nbsp;= &nbsp;1) &nbsp;THEN &nbsp;MIN(HandleID) &nbsp;+ &nbsp;1 &nbsp;ELSE &nbsp;1 &nbsp;END) &nbsp;as &nbsp;HandleID<BR>FROM &nbsp; &nbsp;Handle<BR>WHERE &nbsp;NOT &nbsp;HandleID &nbsp;IN &nbsp;(SELECT &nbsp;a.HandleID &nbsp;- &nbsp;1 &nbsp;FROM &nbsp;Handle &nbsp;a)</DIV><img src ="http://www.blogjava.net/mkchen/aggbug/32023.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/mkchen/" target="_blank">海思</a> 2006-02-22 22:04 <a href="http://www.blogjava.net/mkchen/archive/2006/02/22/32023.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>