??xml version="1.0" encoding="utf-8" standalone="yes"?>欧美日韩国产综合草草,www.黄在线观看,欧洲亚洲免费视频http://www.aygfsteel.com/junky/category/18619.htmlzh-cnFri, 13 Jul 2007 10:38:23 GMTFri, 13 Jul 2007 10:38:23 GMT60SQL SERVER 关于外联?Outer Join)?qing)其?/title><link>http://www.aygfsteel.com/junky/archive/2007/07/12/129852.html</link><dc:creator>junky</dc:creator><author>junky</author><pubDate>Thu, 12 Jul 2007 07:44:00 GMT</pubDate><guid>http://www.aygfsteel.com/junky/archive/2007/07/12/129852.html</guid><wfw:comment>http://www.aygfsteel.com/junky/comments/129852.html</wfw:comment><comments>http://www.aygfsteel.com/junky/archive/2007/07/12/129852.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/junky/comments/commentRss/129852.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/junky/services/trackbacks/129852.html</trackback:ping><description><![CDATA[<p><strong>一 使用外联?/strong></p> <p>  以前在Oracle中用=(+)?+)=来进行左外联接和叛_联接Q后来用SQL Server时用*=?*q行外连接左外联接和叛_联接Q?br>现在军_用SQL-92的标准方法:(x)[OUTER] JOINQOUTER是可以省略的?/p> <p>  LEFT OUTER JOIN ?nbsp;LEFT JOIN        表示左外联接 </p> <p>  RIGHT OUTER JOIN ?nbsp;RIGHT JOIN   表示左外联接 </p> <p>  FULL OUTER JOIN ?nbsp;FULL JOIN        表示左外联接</p> <p>  外联接的意思不用多_(d)我们都懂Q但是JOIN到底怎么用呢Q没有找到很好的资料Q只能从例子中学?fn)?jin)Q?/p> <p>  1、这个例子也许没有实际意义,只是Z(jin)说明问题Q?/p> <div style="SCROLLBAR-HIGHLIGHT-COLOR: buttonhighlight; OVERFLOW: auto; WIDTH: 500px"> <pre style="BORDER-RIGHT: black 1px solid; PADDING-RIGHT: 4px; BORDER-TOP: black 1px solid; PADDING-LEFT: 4px; PADDING-BOTTOM: 4px; BORDER-LEFT: black 1px solid; PADDING-TOP: 4px; BORDER-BOTTOM: black 1px solid; BACKGROUND-COLOR: #ededed"> <div><span style="COLOR: #000000">CREATE TABLE orders(order_id </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">, firm_id </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">, p_id </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">) CREATE TABLE firms (firm_id </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">, f_name </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">) CREATE TABLE products(p_id </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">, p_name </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">) select a.order_id, b.f_name, c.p_name from orders a left join firms b on a.firm_id </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> b.firm_id left join products c on a.p_id </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> c.p_id</span></div> </pre> </div> <p>  说明Qorders表是主表Q先和从表firmsq行左联接,再和从表productsq行左联接?/p> <p>  判断是外联接中的主表q是从表主要看from从句中各个表在LEFT JOIN或RIGHT JOIN两边的位|:(x)LEFT JOIN左边的表是主表,RIGHT JOIN双的表是主表;</p> <p>  ON表达?jin)两个表q接的条Ӟ一般外联接是等D接,不等D接意义不大;</p> <p>  在多个表的连接中Q一个表既可以做主表又同时可以做从表Qؓ(f)?jin)说明这个问题,我们修改以上SQL为:(x)</p> <div style="SCROLLBAR-HIGHLIGHT-COLOR: buttonhighlight; OVERFLOW: auto; WIDTH: 500px"> <pre style="BORDER-RIGHT: black 1px solid; PADDING-RIGHT: 4px; BORDER-TOP: black 1px solid; PADDING-LEFT: 4px; PADDING-BOTTOM: 4px; BORDER-LEFT: black 1px solid; PADDING-TOP: 4px; BORDER-BOTTOM: black 1px solid; BACKGROUND-COLOR: #ededed"> <div><span style="COLOR: #000000">select a.order_id, b.f_name, c.p_name from orders a left join firms b on a.firm_id </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> b.firm_id right join products c on a.order_id </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> c.p_id</span></div> </pre> </div> <p>  q个SQL没有什么意义,但从中可以看出a表既是b的主表又是c的从表;到底怎么用,q是要根据实际情冉|军_是左联接q是双接;</p> <p>  那天Q看C(jin)q样一个例子:(x)</p> <div style="SCROLLBAR-HIGHLIGHT-COLOR: buttonhighlight; OVERFLOW: auto; WIDTH: 500px"> <pre style="BORDER-RIGHT: black 1px solid; PADDING-RIGHT: 4px; BORDER-TOP: black 1px solid; PADDING-LEFT: 4px; PADDING-BOTTOM: 4px; BORDER-LEFT: black 1px solid; PADDING-TOP: 4px; BORDER-BOTTOM: black 1px solid; BACKGROUND-COLOR: #ededed"> <div><span style="COLOR: #000000">create table tab1 (c1 </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">, c2 </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">, c3 </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">) create table tab2 (c1 </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">, c2 </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">, c3 </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">) create table tab3 (c1 </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">, c2 </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">, c3 </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">) create table tab4 (c1 </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">, c2 </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">, c3 </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">) SELECT </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000"> FROM tab1 LEFT OUTER JOIN tab2 ON tab1.c3 </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> tab2.c3 left OUTER JOIN tab3 right OUTER JOIN tab4 ON tab3.c1 </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> tab4.c1 ON tab2.c3 </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> tab4.c3</span></div> </pre> </div> <p>  q种用法q真见Q具体怎么个意思,q在理解?..我把它改写成Q?/p> <div style="SCROLLBAR-HIGHLIGHT-COLOR: buttonhighlight; OVERFLOW: auto; WIDTH: 500px"> <pre style="BORDER-RIGHT: black 1px solid; PADDING-RIGHT: 4px; BORDER-TOP: black 1px solid; PADDING-LEFT: 4px; PADDING-BOTTOM: 4px; BORDER-LEFT: black 1px solid; PADDING-TOP: 4px; BORDER-BOTTOM: black 1px solid; BACKGROUND-COLOR: #ededed"> <div><span style="COLOR: #000000">SELECT </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000"> FROM tab1 left JOIN tab2 ON tab1.c3 </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> tab2.c3 LEFT OUTER JOIN tab4 ON tab2.c3 </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> tab4.c3 RIGHT OUTER JOIN tab3  ON tab3.c1 </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> tab4.c1</span></div> </pre> </div> <p>  也许它们是一个意思。我发现加个括号Q看的更清楚一些(它是个嵌套)(j)</p> <div style="SCROLLBAR-HIGHLIGHT-COLOR: buttonhighlight; OVERFLOW: auto; WIDTH: 500px"> <pre style="BORDER-RIGHT: black 1px solid; PADDING-RIGHT: 4px; BORDER-TOP: black 1px solid; PADDING-LEFT: 4px; PADDING-BOTTOM: 4px; BORDER-LEFT: black 1px solid; PADDING-TOP: 4px; BORDER-BOTTOM: black 1px solid; BACKGROUND-COLOR: #ededed"> <div><span style="COLOR: #000000">SELECT </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000"> FROM tab1 LEFT OUTER JOIN tab2 ON tab1.c3 </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> tab2.c3 left OUTER JOIN (tab3 right OUTER JOIN tab4 ON tab3.c1 </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> tab4.c1) ON tab2.c3 </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> tab4.c3</span></div> </pre> </div> <p>  <strong>?nbsp;外联接中 "ON + AND" ?nbsp;"ON + WHERE" 的区?/strong></p> <p>  1、on条g是外联接时在生成临时表时使用的联l条Ӟ不论从表是确定D是NULLQ主表所有的值都?x)出玎ͼ?/p> <p>  如果再加上and条gQ?nbsp;如果and条g引用的是主表的列Q则对结果毫无媄(jing)响,主表的所有纪录依然会(x)全部出现Q如果and条g引用的是从表的列Q则不符合条件的从表U录昄NULLQ?/p> <p>  2、where条g是在临时表生成后Q再对(f)时表q行qo(h)的条件。(f)时表中的所有纪录都受媄(jing)响,不符合条件的U录被过滤出l果集;</p> <p>  3、示例:(x)</p> <div style="SCROLLBAR-HIGHLIGHT-COLOR: buttonhighlight; OVERFLOW: auto; WIDTH: 500px"> <pre style="BORDER-RIGHT: black 1px solid; PADDING-RIGHT: 4px; BORDER-TOP: black 1px solid; PADDING-LEFT: 4px; PADDING-BOTTOM: 4px; BORDER-LEFT: black 1px solid; PADDING-TOP: 4px; BORDER-BOTTOM: black 1px solid; BACKGROUND-COLOR: #ededed"> <div><span style="COLOR: #000000">select a.module_id, a.name, b.module_name from fb_autocoding a left join fb_app_module b on a.module_id </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> b.module_id and b.module_internal_label </span><span style="COLOR: #000000"><></span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">LO</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">; select a.module_id, a.name, b.module_name from fb_autocoding a left join fb_app_module b on a.module_id </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> b.module_id where b.module_internal_label </span><span style="COLOR: #000000"><></span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">LO</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">; </span></div> </pre> </div> <p>  <strong>?nbsp;其他Joinq算</strong></p> <p>  merge joinQ在处理其他联结之前Q先把相关两个表联结在一P</p> <p>  hash joinQ把一个表join到已l被执行qjoin的结果上Q?/p> <p>  用括h变join的顺序:(x)</p> <div style="SCROLLBAR-HIGHLIGHT-COLOR: buttonhighlight; OVERFLOW: auto; WIDTH: 500px"> <pre style="BORDER-RIGHT: black 1px solid; PADDING-RIGHT: 4px; BORDER-TOP: black 1px solid; PADDING-LEFT: 4px; PADDING-BOTTOM: 4px; BORDER-LEFT: black 1px solid; PADDING-TOP: 4px; BORDER-BOTTOM: black 1px solid; BACKGROUND-COLOR: #ededed"> <div><span style="COLOR: #000000">select catalog.item, catalog.item_color, product.item, color.color_name from catalog full outer join (product cross join color) on catalog.item </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> product.item and catalog.item_color </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> color.color_name;</span></div> </pre> </div> <div style="DISPLAY: none">1</div> <img src ="http://www.aygfsteel.com/junky/aggbug/129852.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/junky/" target="_blank">junky</a> 2007-07-12 15:44 <a href="http://www.aygfsteel.com/junky/archive/2007/07/12/129852.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>利用 JdbcTemplate 自动q回 MS SQL SERVER 2005 自增主键?/title><link>http://www.aygfsteel.com/junky/archive/2007/07/11/129604.html</link><dc:creator>junky</dc:creator><author>junky</author><pubDate>Wed, 11 Jul 2007 05:54:00 GMT</pubDate><guid>http://www.aygfsteel.com/junky/archive/2007/07/11/129604.html</guid><wfw:comment>http://www.aygfsteel.com/junky/comments/129604.html</wfw:comment><comments>http://www.aygfsteel.com/junky/archive/2007/07/11/129604.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/junky/comments/commentRss/129604.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/junky/services/trackbacks/129604.html</trackback:ping><description><![CDATA[JDBC3 中可以直接获取当前插入记录的 ID |具体的调用方式如下:(x)<br><br> <div style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: rgb(230,230,230) 0% 50%; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 95%; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid; moz-background-clip: -moz-initial; moz-background-origin: -moz-initial; moz-background-inline-policy: -moz-initial"> <div><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top><span style="COLOR: rgb(0,0,0)">Statement stmt </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> conn.createStatement();<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top>stmt.executeUpdate(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">INSERT INTO authors (first_name, last_name) values<br> (′George′, ′Orwell′)</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">, Statement.RETURN_GENERATED_KEYS);<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align=top>ResultSet rs </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> stmt.getGeneratedKeys();<br><img id=_226_252_Open_Image onclick="this.style.display='none'; document.getElementById('_226_252_Open_Text').style.display='none'; document.getElementById('_226_252_Closed_Image').style.display='inline'; document.getElementById('_226_252_Closed_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=_226_252_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('_226_252_Closed_Text').style.display='none'; document.getElementById('_226_252_Open_Image').style.display='inline'; document.getElementById('_226_252_Open_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" align=top></span><span style="COLOR: rgb(0,0,255)">if</span><span style="COLOR: rgb(0,0,0)"> ( rs.next() ) </span><span id=_226_252_Closed_Text style="BORDER-RIGHT: rgb(128,128,128) 1px solid; BORDER-TOP: rgb(128,128,128) 1px solid; DISPLAY: none; BORDER-LEFT: rgb(128,128,128) 1px solid; BORDER-BOTTOM: rgb(128,128,128) 1px solid; BACKGROUND-COLOR: rgb(255,255,255)">...</span><span id=_226_252_Open_Text><span style="COLOR: rgb(0,0,0)">{<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: rgb(0,0,255)">int</span><span style="COLOR: rgb(0,0,0)"> key </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> rs.getInt();<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span></div> </div> <br>׃实际与数据库交互采用的是 JdbcTemplateQ因而需要找到它对这U方式的支持。经q实际的查看 Spring ?API 发现其本w提供相应的Ҏ(gu)支持Q经q多ơ的实验后得到如下的实现Ҏ(gu)Q?br><br> <div style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: rgb(230,230,230) 0% 50%; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 95%; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid; moz-background-clip: -moz-initial; moz-background-origin: -moz-initial; moz-background-inline-policy: -moz-initial"> <div><img id=_42_848_Open_Image onclick="this.style.display='none'; document.getElementById('_42_848_Open_Text').style.display='none'; document.getElementById('_42_848_Closed_Image').style.display='inline'; document.getElementById('_42_848_Closed_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=_42_848_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('_42_848_Closed_Text').style.display='none'; document.getElementById('_42_848_Open_Image').style.display='inline'; document.getElementById('_42_848_Open_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" align=top><span style="COLOR: rgb(0,0,255)">private</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">void</span><span style="COLOR: rgb(0,0,0)"> insert(</span><span style="COLOR: rgb(0,0,255)">final</span><span style="COLOR: rgb(0,0,0)"> Profile profile)</span><span id=_42_848_Closed_Text style="BORDER-RIGHT: rgb(128,128,128) 1px solid; BORDER-TOP: rgb(128,128,128) 1px solid; DISPLAY: none; BORDER-LEFT: rgb(128,128,128) 1px solid; BORDER-BOTTOM: rgb(128,128,128) 1px solid; BACKGROUND-COLOR: rgb(255,255,255)">...</span><span id=_42_848_Open_Text><span style="COLOR: rgb(0,0,0)">{<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>    </span><span style="COLOR: rgb(0,0,255)">final</span><span style="COLOR: rgb(0,0,0)"> String _save </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">insert into Newsletter_Profile (user_id, publication_id, last_update) values (?, ?, getdate())</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>    JdbcTemplate template </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">this</span><span style="COLOR: rgb(0,0,0)">.getJdbcTemplate();<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>    KeyHolder keyHolder </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">new</span><span style="COLOR: rgb(0,0,0)"> GeneratedKeyHolder();<br><img id=_310_769_Open_Image onclick="this.style.display='none'; document.getElementById('_310_769_Open_Text').style.display='none'; document.getElementById('_310_769_Closed_Image').style.display='inline'; document.getElementById('_310_769_Closed_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=_310_769_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('_310_769_Closed_Text').style.display='none'; document.getElementById('_310_769_Open_Image').style.display='inline'; document.getElementById('_310_769_Open_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" align=top>    template.update(</span><span style="COLOR: rgb(0,0,255)">new</span><span style="COLOR: rgb(0,0,0)"> PreparedStatementCreator() </span><span id=_310_769_Closed_Text style="BORDER-RIGHT: rgb(128,128,128) 1px solid; BORDER-TOP: rgb(128,128,128) 1px solid; DISPLAY: none; BORDER-LEFT: rgb(128,128,128) 1px solid; BORDER-BOTTOM: rgb(128,128,128) 1px solid; BACKGROUND-COLOR: rgb(255,255,255)">...</span><span id=_310_769_Open_Text><span style="COLOR: rgb(0,0,0)">{<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>        </span><span style="COLOR: rgb(0,0,255)">public</span><span style="COLOR: rgb(0,0,0)"> PreparedStatement createPreparedStatement(Connection con)<br><img id=_402_755_Open_Image onclick="this.style.display='none'; document.getElementById('_402_755_Open_Text').style.display='none'; document.getElementById('_402_755_Closed_Image').style.display='inline'; document.getElementById('_402_755_Closed_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=_402_755_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('_402_755_Closed_Text').style.display='none'; document.getElementById('_402_755_Open_Image').style.display='inline'; document.getElementById('_402_755_Open_Text').style.display='inline';" alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" align=top>         </span><span style="COLOR: rgb(0,0,255)">throws</span><span style="COLOR: rgb(0,0,0)"> SQLException </span><span id=_402_755_Closed_Text style="BORDER-RIGHT: rgb(128,128,128) 1px solid; BORDER-TOP: rgb(128,128,128) 1px solid; DISPLAY: none; BORDER-LEFT: rgb(128,128,128) 1px solid; BORDER-BOTTOM: rgb(128,128,128) 1px solid; BACKGROUND-COLOR: rgb(255,255,255)">...</span><span id=_402_755_Open_Text><span style="COLOR: rgb(0,0,0)">{<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>                        </span><span style="COLOR: rgb(0,0,255)">int</span><span style="COLOR: rgb(0,0,0)"> i </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,0)">0</span><span style="COLOR: rgb(0,0,0)">;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>                        PreparedStatement ps </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> con.prepareStatement(_save,<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>             <span style="BACKGROUND-COLOR: rgb(255,255,0)">Statement.RETURN_GENERATED_KEYS</span>);<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>                        ps.setInt(</span><span style="COLOR: rgb(0,0,0)">++</span><span style="COLOR: rgb(0,0,0)">i, profile.getCustomerId().intValue());<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>                        ps.setInt(</span><span style="COLOR: rgb(0,0,0)">++</span><span style="COLOR: rgb(0,0,0)">i, profile.getPublication().getId());<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>                        </span><span style="COLOR: rgb(0,0,255)">return</span><span style="COLOR: rgb(0,0,0)"> ps;<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>                  }</span></span><span style="COLOR: rgb(0,0,0)"><br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>            }</span></span><span style="COLOR: rgb(0,0,0)">, keyHolder);<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align=top>            profile.setId(keyHolder.getKey().intValue());<br><img alt="" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" align=top>      }</span></span><span style="COLOR: rgb(0,0,0)"> </span></div> </div> <br>特别需要注意的地方?span id=_42_848_Open_Text><span id=_310_769_Open_Text><span id=_402_755_Open_Text><span style="COLOR: rgb(0,0,0)"><span style="BACKGROUND-COLOR: rgb(255,255,0)">Statement.RETURN_GENERATED_KEYS</span></span></span></span></span>Q在使用MS SQL Server 2005 提供?JDBC Driver 中上面的部分是必ȝ。之所以这么说是因?google 出来的所有资料都是没有该部分的,甚至 Spring 自n?document 中也是没有该参数的。我现在不知道那些代码是否能够真正的获取?KeyQ但是现在我 suppose 它们是可?run 的?br><br>如果没有加入 <span id=_42_848_Open_Text><span id=_310_769_Open_Text><span id=_402_755_Open_Text><span style="COLOR: rgb(0,0,0)"><span style="BACKGROUND-COLOR: rgb(255,255,0)">Statement.RETURN_GENERATED_KEYS  </span></span></span></span></span>Q在实际q行数据库操作时?x)出现如下的异常Q?br>PreparedStatementCallback; uncategorized SQLException for SQL []; SQL state [null]; error code [0]; The statement must be executed before any results can be obtained.; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: The statement must be executed before any results can be obtained.<br>caused by : com.microsoft.sqlserver.jdbc.SQLServerException: The statement must be executed before any results can be obtained.<br><span>org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL []; SQL state [null]; error code [0]; The statement must be executed before any results can be obtained.; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: The statement must be executed before any results can be obtained. </span><br>Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The statement must be executed before any results can be obtained. <br>at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source) <br>at com.microsoft.sqlserver.jdbc.SQLServerStatement.getGeneratedKeys(Unknown Source) <br>at weblogic.jdbc.wrapper.PreparedStatement_com_microsoft_sqlserver_jdbc_SQLServerPreparedStatement.getGeneratedKeys(Unknown Source) <br>at org.springframework.jdbc.core.JdbcTemplate$3.doInPreparedStatement(JdbcTemplate.java:772) <br>at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:527) <br>at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:767) <br>at com.fdc.reports20.dao.NewsletterDAO.insert(NewsletterDAO.java:179) <br>at com.fdc.reports20.dao.NewsletterDAO.save(NewsletterDAO.java:153) <br>at com.fdc.reports20.dao.NewsletterDAO.update(NewsletterDAO.java:138) <br>at com.fdc.reports20.business.service.user.AlertServiceImpl.updateNewsletter(AlertServiceImpl.java:146) <br>at com.fdc.reports20.business.service.user.AlertServiceImpl$$FastClassByCGLIB$$52b80fbc.invoke() <br>at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149) <br>at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:674) <br>at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:154) <br>at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:52) <br>at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:176) <br>at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:53) <br>at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:176) <br>at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107) <br>at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:176) <br>at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89) <br>at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:176) <br>at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:615) <br>at com.fdc.reports20.business.service.user.AlertServiceImpl$$EnhancerByCGLIB$$a12ee5d8.updateNewsletter() <br>at com.fdc.reports20.web.delegate.AlertBD.updateNewsletter(AlertBD.java:78) <br>at com.fdc.reports20.web.jpf.um.workbench.WorkBenchController.editPublicationEmails(WorkBenchController.java:149) <br>at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) <br><span id=_42_848_Open_Text><span id=_310_769_Open_Text><span id=_402_755_Open_Text><span style="COLOR: rgb(0,0,0)"><span style="BACKGROUND-COLOR: rgb(255,255,0)"></span></span></span></span><br></span> <img src ="http://www.aygfsteel.com/junky/aggbug/129604.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/junky/" target="_blank">junky</a> 2007-07-11 13:54 <a href="http://www.aygfsteel.com/junky/archive/2007/07/11/129604.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>前触发器和后触发器简?downmoon)http://www.aygfsteel.com/junky/archive/2007/06/19/125047.htmljunkyjunkyTue, 19 Jun 2007 01:35:00 GMThttp://www.aygfsteel.com/junky/archive/2007/06/19/125047.htmlhttp://www.aygfsteel.com/junky/comments/125047.htmlhttp://www.aygfsteel.com/junky/archive/2007/06/19/125047.html#Feedback0http://www.aygfsteel.com/junky/comments/commentRss/125047.htmlhttp://www.aygfsteel.com/junky/services/trackbacks/125047.html前触发器和后触发器简?downmoon)

    触发器是一U特D的存储q程。当Insert Update 或者Delete 语句修改表中一个或者多个行时执行触发器。因为SQL Server 对特定表上的每一个指定操作调用一个触发器Q所以可以用触发器扩展SQL Sever 的内|完整性和数据操纵功能.
    注意:不像Delete 语句QTrancate Table 语句不激z触发器QW(xu)rite Text 语句也不Ȁz触发器?/span>
    在SQL Sever 2000 中支持两U类型的触发器,前触发器(Instead Of Trigger)和后触发?After Trigger)。前触发器就是在语句执行之前Ȁz触发器Q而后触发器就是在语句执行之后Ȁz触发器。可以通过FOR 子句来选择使用何种触发器?br>    当ؓ(f)每一U操作创Z个触发器Ӟ可以为所有三U操作创Z个触发器Qƈ且用相应的~程技术处理每一U操作。下面的CZ在For 子句中列Z(jin)三种语句cdqӞ且用条件语句将相应的跟t值插入到CustUpdLog 表中?br>

Create Trigger TrackCustomerUpdates
On AppDta.dbo.Customer
For Insert,Update,Delete
As
Declare @InsertedCount Int
Declare @DeletedCount Int
Set @InsertedCount=(Select Count(*)From inserted)
Set @DeletedCount=(Select Count(*)From deleted)
If ( @InsertedCount>0)Begin
Insert Into AppDta.dbo.CustUpdLog
( CustID,
Action,
UpdUser,
UpdDateTime)
Select CustId,
Case
When@DeletedCount>0)Then
'Update'
Else 'Insert'
End,
Current_User,
Current_TimeStamp
From inserted
End
Else If(@DeletedCount>0)Begin
Insert Into AppDta.dbo.CustUpdLog
( CustId,
Action,
UpdUser,
UpdDateTime)
select CustId,
'Delete',
Current_User,
Current_TimeStamp
From deleted
End


    正如本例所C,无论何时Insert 或者Update 语句影响一个或者多行时Qinserted 临时表都有记录行。无Z时Delete 或者Update 语句影响一个或者多行时Qdeleted 临时表都有记录行。对于一个Update 语句Qdeleted 临时表有旧行Qinserted 临时表有新行。这个示例还反映?jin)触发器的另一个重要方面:(x)对于某个表的Update 或者Delete 操作Q即使该语句没有影响到行Q也Ȁz触发器 (也就是说没有满Where 子句的行)?触发器的存储q程应该预测q种可能性?/span>


    不仅可以Z个表创徏多个触发器,而且q可以ؓ(f)一个表的同一个SQL 语句(例如Update 语句)创徏多个后触发器Q不能ؓ(f)同一个SQL 语句创徏多个前触发器。每一个新的Create Trigger 语句增加触发器到那些指定表和语句已有的触发器中。对于所创徏的多个触发器Q可以用pȝ存储q程sp_settriggerorder 来指定第一个被Ȁzȝ触发器和最后一个被Ȁzȝ触发器,而对于其他的触发器,则不能指定其Ȁz顺序,只能ql决定。这U触发器的特征不?x)引起Q何特D的问题。因为L可以实现各种动作作ؓ(f)正常的存储过E,q且按照要求的顺序从一个触发器中调用它们?br>
    管触发器是一U存储过E,但是不能使用Execute 语句调用?/span>Q如果有希望׃n触发器和正常的存储过E的~码Q那么只需把共享代码放在存储过E中Q从触发器中调用它。如果一个触发器修改一个表Q那么这些修改可能会(x)Ȁzd一个触发器Q或者本w。在默认情况下,SQL Sever 允许q种嵌套的触发器调用深度?2层。虽然我们徏议允许嵌套的和叠代的触发器,但是可以使用pȝ存储q程止q么做。下面的语句在指定的数据库上防止叠代触发器:(x)
sp_dboption AppDta,`recursive triggers',`false'
    Z(jin)在所有数据库中防止嵌套触发器调用(包括叠代调用)Q可以用下面的语句Q?br>sp_configure `nested triggers',0

    前面以后触发器ؓ(f)例介l了(jin)触发器的基本内容Q下面再介绍一下前触发器的不同之处?span style="COLOR: rgb(255,0,0)">要创Z个前触发器必ȝInstead Of 昑ּ声明
Q如下面的例子:(x)

create Trigger TrackCustomerUpdates
On AppDta.dbo.Customer
Instead 
Of Update
As
Insert Into AppDta.dbo.CustUpdLog
(CustId,
Action,
UpdUser,
UpdDateTime)
Select CustId,
Update’,
Current_User,
Current_TimeStamp
From inserted


    与后触发器不同的是:(x)前触发器既可以在表又可以在视图上创徏Q但一条语句只能创Z个前触发器,因此Q前触发器不存在Ȁz顺序问?br>

触发器应用D例:(x)从当前数据库服务器的Shop表Insert操作同步到另一台服务器的Shop?br>

CREATE TRIGGER Trigger_SynShopForInsert1
ON dbo.Shop
FOR INSERT
AS
  
insert into OtherServer.dbo.shop
(
lngShopID,strShopCode,strName,strShopName,strDescription,lngIndex
)
select  lngShopID,strShopCode,strName,strShopName,strDescription,lngIndex
from shop where lngShopID in (select lngshopid from inserted)


或者:(x)

CREATE TRIGGER Trigger_SynShopForInsert2
ON dbo.Shop
FOR INSERT
AS
  
insert into OtherServer.dbo.shop
(
lngShopID,strShopCode,strName,strShopName,strDescription,lngIndex
)
select  lngShopID,strShopCode,strName,strShopName,strDescription,lngIndex
from  inserted



junky 2007-06-19 09:35 发表评论
]]>
q接SQL Server时报的异怹处理步骤http://www.aygfsteel.com/junky/archive/2006/12/22/89406.htmljunkyjunkyFri, 22 Dec 2006 01:06:00 GMThttp://www.aygfsteel.com/junky/archive/2006/12/22/89406.htmlhttp://www.aygfsteel.com/junky/comments/89406.htmlhttp://www.aygfsteel.com/junky/archive/2006/12/22/89406.html#Feedback0http://www.aygfsteel.com/junky/comments/commentRss/89406.htmlhttp://www.aygfsteel.com/junky/services/trackbacks/89406.html
 (tng) (tng)

See  (tng)com.borland.dx.dataset.DataSetException  (tng)error  (tng)code:  (tng)  (tng)BASE+66  (tng)
solaris  (tng)
com.borland.dx.dataset.DataSetException:  (tng)[Microsoft][SQLServer  (tng)2000  (tng)Driver  (tng)for  (tng)JDBC]Error  (tng)establishing  (tng)socket.  (tng)
 (tng)
 (tng) (tng)  (tng) (tng)  (tng) (tng)  (tng) (tng)at  (tng)com.borland.dx.dataset.DataSetException.a(Unknown  (tng)Source)  (tng)
 (tng)
 (tng) (tng)  (tng) (tng)  (tng) (tng)  (tng) (tng)at  (tng)com.borland.dx.dataset.DataSetException.throwException(Unknown  (tng)Source)  (tng)
 (tng)
 (tng) (tng)  (tng) (tng)  (tng) (tng)  (tng) (tng)at  (tng)com.borland.dx.dataset.DataSetException.SQLException(Unknown  (tng)Source)  (tng)
 (tng)
 (tng) (tng)  (tng) (tng)  (tng) (tng)  (tng) (tng)at  (tng)com.borland.dx.sql.dataset.Database.openConnection(Unknown  (tng)Source)  (tng)
 (tng)
 (tng) (tng)  (tng) (tng)  (tng) (tng)  (tng) (tng)at  (tng)com.borland.jdbcx.metadata.DatabaseInfo.openConnection(Unknown  (tng)Source)  (tng)
 (tng)
 (tng) (tng)  (tng) (tng)  (tng) (tng)  (tng) (tng)at  (tng)com.borland.jdbcx.metadata.e.run(Unknown  (tng)Source)  (tng)
 (tng)
Chained  (tng)exception:  (tng)
 (tng)
java.sql.SQLException:  (tng)[Microsoft][SQLServer  (tng)2000  (tng)Driver  (tng)for  (tng)JDBC]Error  (tng)establishing  (tng)socket.  (tng)
 (tng)
 (tng) (tng)  (tng) (tng)  (tng) (tng)  (tng) (tng)at  (tng)com.microsoft.jdbc.base.BaseExceptions.createException(Unknown  (tng)Source)  (tng)
 (tng)
 (tng) (tng)  (tng) (tng)  (tng) (tng)  (tng) (tng)at  (tng)com.microsoft.jdbc.base.BaseExceptions.getException(Unknown  (tng)Source)  (tng)
 (tng)
 (tng) (tng)  (tng) (tng)  (tng) (tng)  (tng) (tng)at  (tng)com.microsoft.jdbc.base.BaseExceptions.getException(Unknown  (tng)Source)  (tng)
 (tng)
 (tng) (tng)  (tng) (tng)  (tng) (tng)  (tng) (tng)at  (tng)com.microsoft.jdbc.sqlserver.tds.TDSConnection.<init>(Unknown  (tng)Source)  (tng)
 (tng)
 (tng) (tng)  (tng) (tng)  (tng) (tng)  (tng) (tng)at  (tng)com.microsoft.jdbc.sqlserver.SQLServerImplConnection.open(Unknown  (tng)Source)  (tng)
 (tng)
 (tng) (tng)  (tng) (tng)  (tng) (tng)  (tng) (tng)at  (tng)com.microsoft.jdbc.base.BaseConnection.getNewImplConnection(Unknown  (tng)Source)  (tng)
 (tng)
 (tng) (tng)  (tng) (tng)  (tng) (tng)  (tng) (tng)at  (tng)com.microsoft.jdbc.base.BaseConnection.open(Unknown  (tng)Source)  (tng)
 (tng)
 (tng) (tng)  (tng) (tng)  (tng) (tng)  (tng) (tng)at  (tng)com.microsoft.jdbc.base.BaseDriver.connect(Unknown  (tng)Source)  (tng)
 (tng)
 (tng) (tng)  (tng) (tng)  (tng) (tng)  (tng) (tng)at  (tng)java.sql.DriverManager.getConnection(DriverManager.java:512)  (tng)
 (tng)
 (tng) (tng)  (tng) (tng)  (tng) (tng)  (tng) (tng)at  (tng)java.sql.DriverManager.getConnection(DriverManager.java:171)  (tng)
 (tng)
 (tng) (tng)  (tng) (tng)  (tng) (tng)  (tng) (tng)at  (tng)com.borland.dx.sql.dataset.Database.openConnection(Unknown  (tng)Source)  (tng)
 (tng)
 (tng) (tng)  (tng) (tng)  (tng) (tng)  (tng) (tng)at  (tng)com.borland.jdbcx.metadata.DatabaseInfo.openConnection(Unknown  (tng)Source)  (tng)
 (tng)
 (tng) (tng)  (tng) (tng)  (tng) (tng)  (tng) (tng)at  (tng)com.borland.jdbcx.metadata.e.run(Unknown  (tng)Source)  (tng)
 (tng)
是什么原因啊Q? (tng)
---------------------------------------------------------------  (tng)
 (tng)
1、下载Microsoft  (tng)SQL  (tng)Server  (tng)2000  (tng)Service  (tng)Pack  (tng)3aq安装,SQL请选用混和安装模式Q!Q? (tng)
http://www.microsoft.com/downloads/details.aspx?FamilyId=90DCD52C-0488-4E46-AFBF-ACACE5369FA3&displaylang=zh-cn (tng) (tng)
 (tng) (tng) 解压~sp3的升U包?q要点击setup安装.才能成功升到sp3 (tng) (tng)


2、下载SQL  (tng)Server  (tng)2000  (tng)Driver  (tng)for  (tng)JDBC  (tng)Service  (tng)Pack  (tng)3  (tng)
http://www.microsoft.com/downloads/details.aspx?FamilyId=07287B11-0502-461A-B138-2AA54BFDC03A&displaylang=en  (tng)
 (tng)
3、运行时关闭防火?br />



junky 2006-12-22 09:06 发表评论
]]>
վ֩ģ壺 ɽ| | | | | | | | | | | ϲ| ϻ| Զ| | | | ̨ǰ| | | ϴ| | | | | ƴ| | | | | | | Ʊ| | | ɽ| | ƽ| | ͭ| ޶|