﻿<?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-Zhu Jun's Tech Weblog-随笔分类-Database</title><link>http://www.blogjava.net/Johncn/category/5525.html</link><description /><language>zh-cn</language><lastBuildDate>Fri, 02 Mar 2007 02:37:24 GMT</lastBuildDate><pubDate>Fri, 02 Mar 2007 02:37:24 GMT</pubDate><ttl>60</ttl><item><title>[Oracle] Oracle中的dual表</title><link>http://www.blogjava.net/Johncn/archive/2005/12/26/25503.html</link><dc:creator>ZhuJun</dc:creator><author>ZhuJun</author><pubDate>Mon, 26 Dec 2005 14:28:00 GMT</pubDate><guid>http://www.blogjava.net/Johncn/archive/2005/12/26/25503.html</guid><wfw:comment>http://www.blogjava.net/Johncn/comments/25503.html</wfw:comment><comments>http://www.blogjava.net/Johncn/archive/2005/12/26/25503.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Johncn/comments/commentRss/25503.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Johncn/services/trackbacks/25503.html</trackback:ping><description><![CDATA[<P><FONT face=Arial size=2>Oracle中的有一个很特殊的表:Dual Table.这个表中只含有一行记录,并且我们可以用这个表来检测许多Oracle的功能,比如:</FONT></P>
<P><FONT face=Arial size=2></FONT></P>
<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"><FONT face=Arial><FONT size=2><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">SQL</SPAN><SPAN style="COLOR: #808080">&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">select</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #800000">789.65</SPAN><SPAN style="COLOR: #808080">*</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #800000">123</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">from</SPAN></FONT></FONT><FONT face=Arial><FONT size=2><SPAN style="COLOR: #000000">&nbsp;dual;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #800000">789.65</SPAN><SPAN style="COLOR: #808080">*</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #800000">123</SPAN></FONT></FONT><SPAN style="COLOR: #000000"><BR><FONT face=Arial size=2><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></FONT></SPAN><FONT face=Arial><FONT size=2><SPAN style="COLOR: #008080">--</SPAN><SPAN style="COLOR: #008080">--------</SPAN></FONT></FONT><SPAN style="COLOR: #008080"><BR><FONT face=Arial size=2><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></FONT></SPAN><FONT face=Arial><FONT size=2><SPAN style="COLOR: #000000">&nbsp;&nbsp;</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #800000">97126.95</SPAN></FONT></FONT><SPAN style="COLOR: #000000"><BR><FONT face=Arial size=2><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>SQL</FONT></SPAN><FONT face=Arial><FONT size=2><SPAN style="COLOR: #808080">&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">select</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #ff00ff">abs</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #808080">-</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #800000">890.98</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN style="COLOR: #0000ff">from</SPAN></FONT></FONT><FONT face=Arial><FONT size=2><SPAN style="COLOR: #000000">&nbsp;dual;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #ff00ff">ABS</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #808080">-</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #800000">890.98</SPAN></FONT></FONT><FONT face=Arial><FONT size=2><SPAN style="COLOR: #000000">)<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #008080">--</SPAN><SPAN style="COLOR: #008080">----------</SPAN></FONT></FONT><SPAN style="COLOR: #008080"><BR><FONT face=Arial size=2><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></FONT></SPAN><FONT face=Arial><FONT size=2><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #800000">890.98</SPAN></FONT></FONT></DIV>
<P><FONT face=Arial size=2>更特别的的是dual表中只含有一行记录,我们可以通过 desc 函数查看dual表的结构:</FONT></P>
<P><FONT face=Arial size=2></FONT></P>
<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"><FONT face=Arial><FONT size=2><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">SQL</SPAN><SPAN style="COLOR: #808080">&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">desc</SPAN></FONT></FONT><FONT face=Arial><FONT size=2><SPAN style="COLOR: #000000">&nbsp;dual;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;名称&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;是否为空?&nbsp;类型<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;</SPAN><SPAN style="COLOR: #008080">--</SPAN><SPAN style="COLOR: #008080">---------------------------------------&nbsp;--------&nbsp;------------------------</SPAN></FONT></FONT><SPAN style="COLOR: #008080"><BR><FONT face=Arial size=2><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></FONT></SPAN><SPAN style="COLOR: #000000"><BR><FONT face=Arial size=2><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;</FONT></SPAN><FONT face=Arial><FONT size=2><SPAN style="COLOR: #0000ff">DUMMY</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #000000">VARCHAR2</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1</SPAN><SPAN style="COLOR: #000000">)</SPAN></FONT></FONT></DIV>
<P><FONT face=Arial size=2>查询dual表中的数据:</FONT></P>
<P><FONT face=Arial size=2></FONT></P>
<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"><FONT face=Arial><FONT size=2><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">SQL</SPAN><SPAN style="COLOR: #808080">&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">select</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #808080">*</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">from</SPAN></FONT></FONT><SPAN style="COLOR: #000000"><FONT face=Arial size=2>&nbsp;dual&nbsp;;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>DU<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></FONT></SPAN><FONT face=Arial><FONT size=2><SPAN style="COLOR: #008080">--<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">X</SPAN></FONT></FONT></DIV>
<P><FONT face=Arial size=2>我们向dual中插入一条记录后再查询,看情况会如何:</FONT></P>
<P><FONT face=Arial size=2></FONT></P>
<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"><FONT face=Arial><FONT size=2><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">SQL</SPAN><SPAN style="COLOR: #808080">&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">select</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #808080">*</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">from</SPAN></FONT></FONT><SPAN style="COLOR: #000000"><FONT face=Arial size=2>&nbsp;dual;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>DU<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></FONT></SPAN><SPAN style="COLOR: #008080"><FONT face=Arial size=2>--<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></FONT></SPAN><FONT face=Arial><FONT size=2><SPAN style="COLOR: #000000">X<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>SQL</SPAN><SPAN style="COLOR: #808080">&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">insert</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">into</SPAN><SPAN style="COLOR: #000000">&nbsp;dual&nbsp;</SPAN><SPAN style="COLOR: #0000ff">values</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #ff0000">'</SPAN><SPAN style="COLOR: #ff0000">Y</SPAN><SPAN style="COLOR: #ff0000">'</SPAN></FONT></FONT><FONT face=Arial><FONT size=2><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>已创建&nbsp;</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1</SPAN></FONT></FONT><FONT face=Arial><FONT size=2><SPAN style="COLOR: #000000">&nbsp;行。<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>SQL</SPAN><SPAN style="COLOR: #808080">&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">select</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #808080">*</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">from</SPAN></FONT></FONT><SPAN style="COLOR: #000000"><FONT face=Arial size=2>&nbsp;dual;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>DU<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></FONT></SPAN><SPAN style="COLOR: #008080"><FONT face=Arial size=2>--<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></FONT></SPAN><FONT face=Arial><FONT size=2><SPAN style="COLOR: #000000">X<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>SQL</SPAN><SPAN style="COLOR: #808080">&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">select</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #ff00ff">count</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #808080">*</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN style="COLOR: #0000ff">from</SPAN></FONT></FONT><FONT face=Arial><FONT size=2><SPAN style="COLOR: #000000">&nbsp;dual;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #ff00ff">COUNT</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #808080">*</SPAN></FONT></FONT><FONT face=Arial><FONT size=2><SPAN style="COLOR: #000000">)<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #008080">--</SPAN><SPAN style="COLOR: #008080">--------</SPAN></FONT></FONT><SPAN style="COLOR: #008080"><BR><FONT face=Arial size=2><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></FONT></SPAN><FONT face=Arial><FONT size=2><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #800000">2</SPAN></FONT></FONT></DIV>
<P><FONT face=Arial size=2>可见尽管insert语句插入记录后dual中有两条记录,但只能select出第一条记录.</FONT></P>
<P><FONT face=Arial size=2>同样我们再试试update和delete语句:</FONT></P>
<P><FONT face=Arial size=2></FONT></P>
<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"><FONT face=Arial><FONT size=2><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">SQL</SPAN><SPAN style="COLOR: #808080">&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">update</SPAN><SPAN style="COLOR: #000000">&nbsp;dual&nbsp;</SPAN><SPAN style="COLOR: #0000ff">set</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">dummy</SPAN><SPAN style="COLOR: #808080">=</SPAN><SPAN style="COLOR: #ff0000">'</SPAN><SPAN style="COLOR: #ff0000">Z</SPAN><SPAN style="COLOR: #ff0000">'</SPAN></FONT></FONT><FONT face=Arial><FONT size=2><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>已更新&nbsp;</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1</SPAN></FONT></FONT><FONT face=Arial><FONT size=2><SPAN style="COLOR: #000000">&nbsp;行。<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>SQL</SPAN><SPAN style="COLOR: #808080">&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">select</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #808080">*</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">from</SPAN></FONT></FONT><SPAN style="COLOR: #000000"><FONT face=Arial size=2>&nbsp;dual;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>DU<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></FONT></SPAN><SPAN style="COLOR: #008080"><FONT face=Arial size=2>--<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></FONT></SPAN><FONT face=Arial><FONT size=2><SPAN style="COLOR: #000000">Z<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>SQL</SPAN><SPAN style="COLOR: #808080">&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">select</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #ff00ff">count</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #808080">*</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN style="COLOR: #0000ff">from</SPAN></FONT></FONT><FONT face=Arial><FONT size=2><SPAN style="COLOR: #000000">&nbsp;dual;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #ff00ff">COUNT</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #808080">*</SPAN></FONT></FONT><FONT face=Arial><FONT size=2><SPAN style="COLOR: #000000">)<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #008080">--</SPAN><SPAN style="COLOR: #008080">--------</SPAN></FONT></FONT><SPAN style="COLOR: #008080"><BR><FONT face=Arial size=2><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></FONT></SPAN><FONT face=Arial><FONT size=2><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #800000">2</SPAN></FONT></FONT><SPAN style="COLOR: #000000"><BR><FONT face=Arial size=2><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>SQL</FONT></SPAN><FONT face=Arial><FONT size=2><SPAN style="COLOR: #808080">&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">delete</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">from</SPAN></FONT></FONT><FONT face=Arial><FONT size=2><SPAN style="COLOR: #000000">&nbsp;dual;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>已删除&nbsp;</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1</SPAN></FONT></FONT><FONT face=Arial><FONT size=2><SPAN style="COLOR: #000000">&nbsp;行。<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>SQL</SPAN><SPAN style="COLOR: #808080">&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">select</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #808080">*</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">from</SPAN></FONT></FONT><SPAN style="COLOR: #000000"><FONT face=Arial size=2>&nbsp;dual;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>DU<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></FONT></SPAN><SPAN style="COLOR: #008080"><FONT face=Arial size=2>--<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></FONT></SPAN><FONT face=Arial><FONT size=2><SPAN style="COLOR: #000000">Y<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>SQL</SPAN><SPAN style="COLOR: #808080">&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">select</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #ff00ff">count</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #808080">*</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN style="COLOR: #0000ff">from</SPAN></FONT></FONT><FONT face=Arial><FONT size=2><SPAN style="COLOR: #000000">&nbsp;dual;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #ff00ff">COUNT</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #808080">*</SPAN></FONT></FONT><FONT face=Arial><FONT size=2><SPAN style="COLOR: #000000">)<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #008080">--</SPAN><SPAN style="COLOR: #008080">--------</SPAN></FONT></FONT><SPAN style="COLOR: #008080"><BR><FONT face=Arial size=2><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></FONT></SPAN><FONT face=Arial><FONT size=2><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #800000">1</SPAN></FONT></FONT></DIV>
<P><FONT face=Arial size=2>可见每次操作只能影响到第一条记录(尽管select , update , delete 所针对的范围是整个表).</FONT></P><img src ="http://www.blogjava.net/Johncn/aggbug/25503.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Johncn/" target="_blank">ZhuJun</a> 2005-12-26 22:28 <a href="http://www.blogjava.net/Johncn/archive/2005/12/26/25503.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[Oracle] Oracle exp/imp导出导入工具的使用 [转] </title><link>http://www.blogjava.net/Johncn/archive/2005/12/26/25502.html</link><dc:creator>ZhuJun</dc:creator><author>ZhuJun</author><pubDate>Mon, 26 Dec 2005 14:19:00 GMT</pubDate><guid>http://www.blogjava.net/Johncn/archive/2005/12/26/25502.html</guid><wfw:comment>http://www.blogjava.net/Johncn/comments/25502.html</wfw:comment><comments>http://www.blogjava.net/Johncn/archive/2005/12/26/25502.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Johncn/comments/commentRss/25502.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Johncn/services/trackbacks/25502.html</trackback:ping><description><![CDATA[<FONT face=Arial><FONT size=2><STRONG>导出工具 exp</STRONG> <BR><BR>1. 它是操作系统下一个可执行的文件 存放目录/ORACLE_HOME/bin<BR><BR>exp导出工具将数据库中数据备份压缩成一个二进制系统文件.可以在不同OS间迁移<BR><BR>它有三种模式：<BR>a. 用户模式： 导出用户所有对象以及对象中的数据；<BR>b. 表模式： 导出用户所有表或者指定的表；<BR>c. 整个数据库： 导出数据库中所有对象。<BR><BR>2. 导出工具exp交互式命令行方式的使用的例子<BR><BR>$exp test/test123@appdb<BR>Enter array fetch buffer size: 4096 &gt; 回车<BR>Export file: expdat.dmp &gt; m.dmp 生成导出的文件名 <BR>(1)E(ntire database), (2)U(sers), or (3)T(ables): (2)U &gt; 3<BR>Export table data (yes/no): yes &gt; 回车<BR>Compress extents (yes/no): yes &gt; 回车<BR>Export done in ZHS16GBK character set and ZHS16GBK NCHAR character set<BR>About to export specified tables via Conventional Path ...<BR>Table(T) or Partition(T<IMG alt=Razz src="http://www.cybercorlin.net/forum/images/smilies/razz.gif" align=middle>) to be exported: (RETURN to quit) &gt; cmamenu 要导出的表名<BR>. . exporting table CMAMENU 4336 rows exported<BR>Table(T) or Partition(T<IMG alt=Razz src="http://www.cybercorlin.net/forum/images/smilies/razz.gif" align=middle>) to be exported: (RETURN to quit) &gt;要导出的表名n<BR>Table(T) or Partition(T<IMG alt=Razz src="http://www.cybercorlin.net/forum/images/smilies/razz.gif" align=middle>) to be exported: (RETURN to quit) &gt; 回车<BR>Export terminated successfully without warnings. <BR><BR>3. 导出工具exp非交互式命令行方式的例子<BR><BR>$exp scott/tiger tables=(emp,dept) file=/directory/scott.dmp grants=y <BR><BR>说明:把scott用户里两个表emp,dept导出到文件/directory/scott.dmp<BR><BR>$exp scott/tiger tables=emp query="where job='salesman' and sal&lt;1600" file=/directory/scott2.dmp <BR><BR>说明:在exp里面加上导出emp的查询条件job='salesman' and sal&lt;1600<BR><BR>(但我个人很少这样用,还是把满足条件的记录生成临时表后,再exp会方便一些)<BR><BR>$exp parfile=username.par file=/directory1/username_1.dmp,/directory1/username_2.dmp filesize=2000M log=/directory2/username_exp.log<BR><BR>参数文件username.par内容<BR>userid=username/userpassword<BR>buffer=8192000<BR>compress=n<BR>grants=y<BR><BR>说明:username.par为导出工具exp用的参数文件,里面具体参数可以根据需要去修改<BR><BR>filesize指定生成的二进制备份文件的最大字节数<BR><BR>(可用来解决某些OS下2G物理文件的限制及加快压缩速度和方便刻历史数据光盘等)<BR><BR><STRONG>导入工具 imp</STRONG><BR><BR>1. 它是操作系统下一个可执行的文件 存放目录/ORACLE_HOME/bin<BR><BR>imp导入工具将EXP形成的二进制系统文件导入到数据库中.<BR><BR>它有三种模式：<BR>a. 用户模式： 导出用户所有对象以及对象中的数据；<BR>b. 表模式： 导出用户所有表或者指定的表；<BR>c. 整个数据库： 导出数据库中所有对象。<BR><BR>只有拥有IMP_FULL_DATABASE和DBA权限的用户才能做整个数据库导入<BR><BR>imp步骤：<BR>(1) create table (2) insert data (3) create index (4) create triggers,constraints<BR><BR>2.导入工具imp交互式命令行方式的例子<BR>$ imp<BR>Import: Release 8.1.6.0.0 - Production on 星期五 12月 7 17:01:08 2001<BR>(c) Copyright 1999 Oracle Corporation. All rights reserved.<BR>用户名: test<BR>口令:****<BR>连接到: Oracle8i Enterprise Edition Release 8.1.6.0.0 - bit Production<BR>With the Partitioning option<BR>JServer Release 8.1.6.0.0 - Production<BR>导入文件: expdat.dmp&gt; /tmp/m.dmp<BR>输入插入缓冲区大小（最小为 8192 ) 30720&gt;<BR>经由常规路径导出由EXPORT:V08.01.06创建的文件<BR>警告: 此对象由 TEST 导出, 而不是当前用户<BR>已经完成ZHS16GBK字符集和ZHS16GBK NCHAR 字符集中的导入<BR>只列出导入文件的内容(yes/no)：no&gt;<BR>由于对象已存在, 忽略创建错误(yes/no)：no&gt; yes<BR>导入权限(yes/no)：yes&gt;<BR>导入表数据(yes/no)：yes&gt;<BR>导入整个导出文件(yes/no)：no&gt; yes<BR>. 正在将TEST的对象导入到 SCOTT<BR>. . 正在导入表 "CMAMENU" 4336行被导入<BR>成功终止导入，但出现警告。 <BR><BR><BR>3.导入工具imp非交互式命令行方式的例子<BR><BR>$ imp system/manager fromuser=jones tables=(accts) <BR>$ imp system/manager fromuser=scott tables=(emp,dept) <BR>$ imp system/manager fromuser=scott touser=joe tables=emp <BR>$ imp scott/tiger file = expdat.dmp full=y <BR>$ imp scott/tiger file = /mnt1/t1.dmp show=n buffer=2048000 ignore=n commit=y grants=y full=y log=/oracle_backup/log/imp_scott.log<BR>$ imp system/manager parfile=params.dat <BR>params.dat 内容 <BR>file=dba.dmp show=n ignore=n grants=y fromuser=scott tables=(dept,emp) <BR><BR>4.导入工具imp可能出现的问题<BR><BR>(1) 数据库对象已经存在<BR>一般情况, 导入数据前应该彻底删除目标数据下的表, 序列, 函数/过程,触发器等; <BR>数据库对象已经存在, 按缺省的imp参数, 则会导入失败<BR>如果用了参数ignore=y, 会把exp文件内的数据内容导入<BR>如果表有唯一关键字的约束条件, 不合条件将不被导入<BR>如果表没有唯一关键字的约束条件, 将引起记录重复<BR><BR>(2) 数据库对象有主外键约束<BR>不符合主外键约束时, 数据会导入失败 <BR>解决办法: 先导入主表, 再导入依存表<BR>disable目标导入对象的主外键约束, 导入数据后, 再enable它们<BR>(3) 权限不够<BR>如果要把A用户的数据导入B用户下, A用户需要有imp_full_database权限<BR><BR>(4) 导入大表( 大于80M ) 时, 存储分配失败<BR>默认的EXP时, compress = Y, 也就是把所有的数据压缩在一个数据块上.<BR>导入时, 如果不存在连续一个大数据块, 则会导入失败.<BR>导出80M以上的大表时, 记得compress= N, 则不会引起这种错误.<BR><BR>(5) imp和exp使用的字符集不同<BR>如果字符集不同, 导入会失败, 可以改变unix环境变量或者NT注册表里NLS_LANG相关信息.<BR>导入完成后再改回来.<BR><BR>(6) imp和exp版本不能往上兼容<BR>imp可以成功导入低版本exp生成的文件, 不能导入高版本exp生成的文件<BR>根据情况我们可以用<BR>$ imp username/password@connect_string<BR>说明: connect_string 是在/ORACLE_HOME/network/admin/tnsnames.ora<BR>定义的本地或者远端数据库的名称<BR>注意事项:<BR>UNIX: /etc/hosts 要定义本地或者远端数据库服务器的主机名<BR>win98: windowshosts 和IP地址的对应关系 <BR><BR>win2000: winntsystem32driversetchosts </FONT></FONT><img src ="http://www.blogjava.net/Johncn/aggbug/25502.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Johncn/" target="_blank">ZhuJun</a> 2005-12-26 22:19 <a href="http://www.blogjava.net/Johncn/archive/2005/12/26/25502.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>ORACLE常用傻瓜问题1000问[转]</title><link>http://www.blogjava.net/Johncn/archive/2005/12/16/24224.html</link><dc:creator>ZhuJun</dc:creator><author>ZhuJun</author><pubDate>Fri, 16 Dec 2005 07:23:00 GMT</pubDate><guid>http://www.blogjava.net/Johncn/archive/2005/12/16/24224.html</guid><wfw:comment>http://www.blogjava.net/Johncn/comments/24224.html</wfw:comment><comments>http://www.blogjava.net/Johncn/archive/2005/12/16/24224.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Johncn/comments/commentRss/24224.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Johncn/services/trackbacks/24224.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 作者：&nbsp;&nbsp;CCBZZP&nbsp;&nbsp; (原文)&nbsp;1. Oracle安装完成后的初始口令? &nbsp; &nbsp;　internal/oracle 　　sys/change_on_install 　　system/manager 　　scott/tiger 　　sysman/oem_temp 2. ORACLE9IAS WEB CACHE的初始默认...&nbsp;&nbsp;<a href='http://www.blogjava.net/Johncn/archive/2005/12/16/24224.html'>阅读全文</a><img src ="http://www.blogjava.net/Johncn/aggbug/24224.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Johncn/" target="_blank">ZhuJun</a> 2005-12-16 15:23 <a href="http://www.blogjava.net/Johncn/archive/2005/12/16/24224.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>SQL Server各种日期计算方法[转]</title><link>http://www.blogjava.net/Johncn/archive/2005/12/08/22963.html</link><dc:creator>ZhuJun</dc:creator><author>ZhuJun</author><pubDate>Thu, 08 Dec 2005 04:40:00 GMT</pubDate><guid>http://www.blogjava.net/Johncn/archive/2005/12/08/22963.html</guid><wfw:comment>http://www.blogjava.net/Johncn/comments/22963.html</wfw:comment><comments>http://www.blogjava.net/Johncn/archive/2005/12/08/22963.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Johncn/comments/commentRss/22963.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Johncn/services/trackbacks/22963.html</trackback:ping><description><![CDATA[<P><FONT face=Arial size=2>(</FONT><A href="https://dream4ever.org/showthread.php?t=29463&amp;page=1"><FONT face=Arial size=2>原文</FONT></A><FONT face=Arial size=2>)<BR>
<HR>
<BR>　　通常，你需要获得当前日期和计算一些其他的日期，例如，你的程序可能需要判断一个月的第一天或者最后一天。你们大部分人大概都知道怎样把日期进行分割（年、月、日等），然后仅仅用分割出来的年、月、日等放在几个函数中计算出自己所需要的日期！在这篇文 章里，我将告诉你如何使用DATEADD和DATEDIFF函数来计算出在你的程序中可能你要用到的一些不同日期。 <BR><BR>　　在使用本文中的例子之前，你必须注意以下的问题。大部分可能不是所有例子在不同的机器上执行的结果可能不一样，这完全由哪一天是一个星期的第一天这个设置决定。第一天（DATEFIRST）设定决定了你的系统使用哪一天作为一周的第一天。所有以下的例 子都是以星期天作为一周的第一天来建立，也就是第一天设置为7。假如你的第一天设置不一样，你可能需要调整这些例子，使它和不同的第一天设置相符合。你可以通过@@DATEFIRST函数来检查第一天设置。 <BR>　　<BR>　　为了理解这些例子，我们先复习一下DATEDIFF和DATEADD函数。DATEDIFF函数计算两个日期之间的小时、天、周、月、年等时间间隔总数。DATEADD函数计算一个日期通过给时间间隔加减来获得一个新的日期。要了解更多的DATEDI FF和DATEADD函数以及时间间隔可以阅读微软联机帮助。 <BR>　　<BR>　　使用DATEDIFF和DATEADD函数来计算日期，和本来从当前日期转换到你需要的日期的考虑方法有点不同。你必须从时间间隔这个方面来考虑。比如，从当前日期到你要得到的日期之间有多少时间间隔，或者，从今天到某一天（比如1900-1-1）之间有多少时间间隔，等等。理解怎样着眼于时间间隔有助于你轻松的理解我的不同的日期计算例子。 <BR>　　<BR>　　<FONT color=red><B>一个月的第一天</B></FONT> <BR>　　<BR>　　第一个例子，我将告诉你如何从当前日期去这个月的最后一天。请注意：这个例子以及这篇文章中的其他例子都将只使用DATEDIFF和DATEADD函数来计算我们想要的日期。每一个例子都将通过计算但前的时间间隔，然后进行加减来得到想要计算的日期。 <BR>　　<BR>　　这是计算一个月第一天的SQL 脚本： <BR>　　SELECT DATEADD(mm, DATEDIFF(mm,0,getdate()), 0) <BR>　　<BR>　　我们把这个语句分开来看看它是如何工作的。最核心的函数是getdate()，大部分人都知道这个是返回当前的日期和时间的函数。下一个执行的函数DATEDIFF(mm,0,getdate())是计算当前日期和“1900-01-01 00:00:00.000”这个日期之间的月数。记住：时期和时间变量和毫秒一样是从“1900-01-01 00:00:00.000”开始计算的。这就是为什么你可以在DATEDIFF函数中指定第一个时间表达式为“0”。下一个函数是DATEADD，增加当前日期到“1900-01-01”的月数。通过增加预定义的日期“1900-01-01”和当前日期的月数，我们可以获得这个月的第一天。另外，计算出来的日期的时间部分将会是“00:00:00.000”。 <BR>　　<BR>　　这个计算的技巧是先计算当前日期到“1900-01-01”的时间间隔数，然后把它加到“1900-01-01”上来获得特殊的日期，这个技巧可以用来计算很多不同的日期。下一个例子也是用这个技巧从当前日期来产生不同的日期。<BR>本周的星期一 <BR>　　<BR>　　这里我是用周(wk)的时间间隔来计算哪一天是本周的星期一。 <BR>　　<BR>　　SELECT DATEADD(wk, DATEDIFF(wk,0,getdate()), 0) <BR>　　<BR>　　<B><FONT color=red>一年的第一天</FONT></B> <BR>　　<BR>　　现在用年(yy)的时间间隔来显示这一年的第一天。 <BR>　　<BR>　　SELECT DATEADD(yy, DATEDIFF(yy,0,getdate()), 0) <BR>　　<BR>　　<FONT color=red><B>季度的第一天</B> </FONT><BR>　　<BR>　　假如你要计算这个季度的第一天，这个例子告诉你该如何做。 <BR>　　<BR>　　SELECT DATEADD(qq, DATEDIFF(qq,0,getdate()), 0) <BR>　　<BR>　　<FONT color=red><B>当天的半夜</B></FONT> <BR>　　<BR>　　曾经需要通过getdate()函数为了返回时间值截掉时间部分，就会考虑到当前日期是不是在半夜。假如这样，这个例子使用DATEDIFF和DATEADD函数来获得半夜的时间点。 <BR>　　<BR>　　SELECT DATEADD(dd, DATEDIFF(dd,0,getdate()), 0) <BR>　　<BR>　　深入DATEDIFF和DATEADD函数计算 <BR>　　<BR>　　你可以明白，通过使用简单的DATEDIFF和DATEADD函数计算，你可以发现很多不同的可能有意义的日期。 <BR>　　<BR>　　目前为止的所有例子只是仅仅计算当前的时间和“1900-01-01”之间的时间间隔数量，然后把它加到“1900-01-01”的时间间隔上来计算出日期。假定你修改时间间隔的数量，或者使用不同的时间间隔来调用DATEADD函数，或者减去时间间隔而不是增加，那么通过这些小的调整你可以发现和多不同的日期。 <BR>　　<BR>　　这里有四个例子使用另外一个DATEADD函数来计算最后一天来分别替换DATEADD函数前后两个时间间隔。 <BR><B><FONT color=red>上个月的最后一天</FONT> </B><BR>　　<BR>　　这是一个计算上个月最后一天的例子。它通过从一个月的最后一天这个例子上减去3毫秒来获得。有一点要记住，在Sql Server中时间是精确到3毫秒。这就是为什么我需要减去3毫秒来获得我要的日期和时间。 <BR>　　<BR>　　SELECT dateadd(ms,-3,DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)) <BR>　　<BR>　　计算出来的日期的时间部分包含了一个Sql Server可以记录的一天的最后时刻(“23:59:59:997”)的时间。 <BR>　　<BR>　　<B><FONT color=red>去年的最后一天</FONT></B> <BR>　　<BR>　　连接上面的例子，为了要得到去年的最后一天，你需要在今年的第一天上减去3毫秒。 <BR>　　<BR>　　SELECT dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)) <BR>　　<BR>　　<FONT color=red><B>本月的最后一天</B></FONT> <BR>　　<BR>　　现在，为了获得本月的最后一天，我需要稍微修改一下获得上个月的最后一天的语句。修改需要给用DATEDIFF比较当前日期和“1900-01-01”返回的时间间隔上加1。通过加1个月，我计算出下个月的第一天，然后减去3毫秒，这样就计算出了这个月的最后一天。这是计算本月最后一天的SQL脚本。 <BR>　　<BR>　　SELECT dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())+1, 0)) <BR>　　<BR>　　<FONT color=red><B>本年的最后一天</B> </FONT>　　<BR>　　你现在应该掌握这个的做法，这是计算本年最后一天脚本 <BR>　　<BR>　　SELECT dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate())+1, 0))。 <BR>　　<BR>　　<FONT color=red><B>本月的第一个星期一</B></FONT> <BR>　　<BR>　　好了，现在是最后一个例子。这里我要计算这个月的第一个星期一。这是计算的脚本。 <BR>　　<BR>　　select DATEADD(wk, DATEDIFF(wk,0, <BR>　　dateadd(dd,6-datepart(day,getdate()),getdate()) <BR>　　), 0) <BR>　　<BR>　　在这个例子里，我使用了“本周的星期一”的脚本，并作了一点点修改。修改的部分是把原来脚本中“getdate()”部分替换成计算本月的第6天，在计算中用本月的第6天来替换当前日期使得计算可以获得这个月的第一个星期一。 <BR>　　<BR>　　总结 <BR>　　<BR>　　我希望这些例子可以在你用DATEADD和DATEDIFF函数计算日期时给你一点启发。通过使用这个计算日期的时间间隔的数学方法，我发现为了显示两个日期之间间隔的有用历法是有价值的。注意，这只是计算出这些日期的一种方法。要牢记，还有很多方法 可以得到相同的计算结果。假如你有其他的方法，那很不错，要是你没有，我希望这些例子可以给你一些启发，当你要用DATEADD和DATEDIFF函数计算你程序可能要用到的日期时。 <BR>附录，其他日期处理方法 <BR>　　<BR>　　1)<FONT color=red>去掉时分秒</FONT> <BR>　　declare @ datetime <BR>　　set @ = getdate() --'2003-7-1 10:00:00' <BR>　　SELECT @,DATEADD(day, DATEDIFF(day,0,@), 0) <BR>　　<BR>　　2）<FONT color=red>显示星期几</FONT> <BR>　　select datename(weekday,getdate()) <BR>　　<BR>　　3）如何<FONT color=red>取得某个月的天数</FONT> <BR>　　declare @m int <BR>　　set @m=2 --月份 <BR>　　select datediff(day,'2003-'+cast(@m as varchar)+'-15' ,'2003-'+cast(@m+1 as varchar)+'-15') <BR>　　另外，取得本月天数 <BR>　　select datediff(day,cast(month(GetDate()) as varchar)+'-'+cast(month(GetDate()) as varchar)+'-15' ,cast(month(GetDate()) as varchar)+'-'+cast(month(GetDate())+1 as varchar)+'-15') <BR>　　或者使用计算本月的最后一天的脚本，然后用DAY函数区最后一天 <BR>　　SELECT Day(dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())+1, 0))) <BR>　　<BR>　　4）<FONT color=red>判断是否闰年</FONT>： <BR>　　SELECT case day(dateadd(mm, 2, dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)))) when 28 then '平年' else '闰年' end <BR>　　或者 <BR>　　select case datediff(day,datename(year,getdate())+'-02-01',dateadd(mm,1,datename(year,getdate())+'-02-01')) <BR>　　when 28 then '平年' else '闰年' end <BR>　　<BR>　　5）<FONT color=red>一个季度多少天</FONT> <BR>　　declare @m tinyint,@time smalldatetime <BR>　　select @m=month(getdate()) <BR>　　select @m=case when @m between 1 and 3 then 1 <BR>　　when @m between 4 and 6 then 4 <BR>　　when @m between 7 and 9 then 7 <BR>　　else 10 end <BR>　　select @time=datename(year,getdate())+'-'+convert(varchar(10),@m)+'-01' <BR>　　select datediff(day,@time,dateadd(mm,3,@time))</FONT></P><!-- / message --><!-- edit note --><img src ="http://www.blogjava.net/Johncn/aggbug/22963.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Johncn/" target="_blank">ZhuJun</a> 2005-12-08 12:40 <a href="http://www.blogjava.net/Johncn/archive/2005/12/08/22963.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>