﻿<?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-&lt;DIV id="ddm_Title"&gt;&lt;H1&gt;大大毛&amp;nbsp;&lt;i class="little"&gt;的笔记&lt;/i&gt;&lt;/H1&gt;&lt;H2&gt;&amp;nbsp&amp;nbsp;DDM's Note&lt;/H2&gt;&lt;/DIV&gt;</title><link>http://www.blogjava.net/tw-ddm/</link><description>&lt;div id="ddm_subTitle"&gt;
&lt;h2 class="catchline"&gt;
        哪怕没有办法一定有说法,&lt;br/&gt;
        就算没有鸽子一定有乌鸦,&lt;br/&gt;
        固执无罪&amp;nbsp;梦想有价,&lt;br/&gt;
        让他们惊讶.
&lt;/h2&gt;
&lt;/div&gt;</description><language>zh-cn</language><lastBuildDate>Mon, 06 Apr 2026 10:23:37 GMT</lastBuildDate><pubDate>Mon, 06 Apr 2026 10:23:37 GMT</pubDate><ttl>60</ttl><item><title>Nifi同步数据的几种方法</title><link>http://www.blogjava.net/tw-ddm/articles/433715.html</link><dc:creator>大大毛</dc:creator><author>大大毛</author><pubDate>Thu, 11 Apr 2019 09:27:00 GMT</pubDate><guid>http://www.blogjava.net/tw-ddm/articles/433715.html</guid><wfw:comment>http://www.blogjava.net/tw-ddm/comments/433715.html</wfw:comment><comments>http://www.blogjava.net/tw-ddm/articles/433715.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/tw-ddm/comments/commentRss/433715.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/tw-ddm/services/trackbacks/433715.html</trackback:ping><description><![CDATA[<span style="font-family: Arial; font-size: 10pt;">经常会遇到将Table从一个DB同步到另一个DB的需求，不同需求下，可使用的处理方式会有不同：</span><br /><br /><span style="font-family: Arial; font-size: 10pt;">1.&nbsp;使用特定的Processor</span><br /><span style="font-family: Arial; font-size: 10pt;">比如Upsert或自行开发的Processor</span><br /><br /><span style="font-family: Arial; font-size: 10pt;">2.&nbsp;在Nifi流程中自行构建SQL及其绑定参数</span><br /><span style="font-family: Arial; font-size: 10pt;">比如借用ConvertJsonToSQL的二次加工</span><br /><br /><span style="font-family: Arial; font-size: 10pt;">3.&nbsp;RouteOnAttribute的分支</span><br /><span style="font-family: Arial; font-size: 10pt;">最多能分出Delete，Update是无能为力的，而且还要考量资料顺序</span><br /><br /><span style="font-family: Arial; font-size: 10pt;">4.&nbsp;仅新增 +&nbsp;DB层面的二次处理。这个其实是可以适用于所有情况，Nifi流程贼简单，但DB上的东西就多了</span><br /><span style="font-family: Arial; font-size: 10pt;">. 在收方加多一张tmp表，结构与正式表一致，但就是没有key。Nifi同步资料指到tmp表</span><br /><span style="font-family: Arial; font-size: 10pt;">.&nbsp;在tmp表上加多Trigger，在这里面Coding去控制Insert、Update和Delete. Trigger里面除了不能Truncate&nbsp;Table，其它啥都能搞</span><br /><span style="font-family: Arial; font-size: 10pt;">.&nbsp;加多一个Job或Event，定时去清tmp表，防止它爆炸</span><br /><img src ="http://www.blogjava.net/tw-ddm/aggbug/433715.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/tw-ddm/" target="_blank">大大毛</a> 2019-04-11 17:27 <a href="http://www.blogjava.net/tw-ddm/articles/433715.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>RouteOnAttribute的用法</title><link>http://www.blogjava.net/tw-ddm/articles/433714.html</link><dc:creator>大大毛</dc:creator><author>大大毛</author><pubDate>Thu, 11 Apr 2019 08:55:00 GMT</pubDate><guid>http://www.blogjava.net/tw-ddm/articles/433714.html</guid><wfw:comment>http://www.blogjava.net/tw-ddm/comments/433714.html</wfw:comment><comments>http://www.blogjava.net/tw-ddm/articles/433714.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/tw-ddm/comments/commentRss/433714.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/tw-ddm/services/trackbacks/433714.html</trackback:ping><description><![CDATA[<div style="display: inline-block;"><h5><span style="font-family: Arial; background-color: #ffffff; font-size: 10pt;">RouteOnAttribute</span></h5><span style="background-color: #ffffff; font-size: 10pt; font-family: Arial;">&nbsp; &nbsp; 这个组件的是用途是根据Attribute的值进行Route分流，从输入输出的角度来看，它可以把一个Input分成多个Output出来，它的分支不同于程序中的Switch语法，而等效于多条的IF语句，也就是说若Output的条件全部都符合，它是可以把1个输出Copy到多个输出的，所以它也可以用于条件复制的应用上。<br />&nbsp; &nbsp; 关于资料落地的文章里我有提到过Route的使用范围，它应该用在能够以Key做分支条件的场景，也就是说相同的Key一定会走固定的Output出来，这样才不会出现资料乱序的状况。下面有两个示例，第一个是典型的分支用法，第二个比较有意思，它的作用相当于Oracle中的Decode语法<br /><br /></span><h5><span style="background-color: #ffffff; font-size: 10pt; font-family: Arial;">示例1</span></h5></div><div><span style="font-size: 13.3333px;">&nbsp; &nbsp; 使用Route做流程的分支，根据一个叫SO的栏位是否为空决定走不同的流程(左右的UpdateAttribute可以把它们想象成两个完全不同的处理流程来看)<br /><br /><img src="http://www.blogjava.net/images/blogjava_net/tw-ddm/Route1.png" width="600" height="216" alt="" /><br /><br /><br /></span><span style="font-family: Arial; font-style: italic; background-color: #ffffff; font-size: 10pt;"><strong>&nbsp; &nbsp; RouteOnAttribute</strong>，</span><span style="background-color: #ffffff; font-size: 10pt; font-family: Arial;">作用是根据Attribute的Bool值来决定是否进入该分支</span><br /><span style="font-size: 13.3333px;"><img src="http://www.blogjava.net/images/blogjava_net/tw-ddm/RouteOnAttributeProcessor1.png" width="600" height="443" alt="" /><br /></span><ul style="font-size: 12px; margin-left: 1em; background-color: #ffffff;"><li><span style="font-size: 8pt;"><font face="Comic Sans MS"><em>HasSO / NonSO</em></font><font face="Trebuchet MS, 宋体">：这是我自行定义的两个Route名称 (不是属性名称)，Value是一个表达式，若它的值=true，则Output会进该Route</font></span></li><ul><li style="font-family: &quot;Trebuchet MS&quot;, 宋体;"><span style="font-size: 8pt;">Nifi的表达式语法不怎么好写，官方的文档上有些东西并不支持 (不确定是不是Nifi版本缘故)。这里还是可以看得出来就只是判断一个叫SO的Attribute的值是否为空 ---&nbsp;还记得有些Processor里面还有叫"Null Value Representation"的属性吧，若是那里配成"null"那这里也要与之匹配。</span></li></ul><li style="font-family: &quot;Trebuchet MS&quot;, 宋体;"><span style="font-size: 8pt;">经过该Processor处理后，SO为空的会走右端逻辑，而不为空的则会走左段逻辑，同时它会加多一个叫"RouteOnAttribute.Route"的Attribute，内容即为Route名称<br /></span></li></ul><span style="font-size: 13.3333px;"><br /></span><h5><span style="font-size: 13.3333px;">示例2：</span></h5><span style="font-size: 13.3333px;">&nbsp; &nbsp; 根据多个栏位是否有值(不为空)，让它们能够进入不同的Route，后面再根据Route名称去动态的取值，它的特点是Nifi流程并没有出现分支(Connection上是勾了所有的Route)，只是为不同的数据设上了一个变量名称<br /><br /><img src="http://www.blogjava.net/images/blogjava_net/tw-ddm/Route2.png" width="600" height="298" alt="" /><br /><br /></span><span style="font-family: Arial; font-style: italic; background-color: #ffffff; font-size: 10pt;"><strong>&nbsp; &nbsp; RouteOnAttribute</strong>，</span><span style="font-family: Arial; background-color: #ffffff; font-size: 10pt;">借用</span><span style="background-color: #ffffff; font-size: 10pt; font-family: Arial;">分支名称在后面搞事，这里相当于是给资料加上了一个变量名称</span><br /><span style="font-size: 13.3333px;"><img src="http://www.blogjava.net/images/blogjava_net/tw-ddm/RouteOnAttributeProcessor2.png" width="600" height="442" alt="" /><br /></span><ul style="margin-left: 1em; background-color: #ffffff;"><li style="font-size: 12px;"><span style="font-size: 8pt;"><font face="Comic Sans MS"><em>MO /&nbsp;MODELFAMILY / UPN / USN</em></font><font face="Trebuchet MS, 宋体">：这是定义的四个Route名称，判断条件都很简单，就只是不为空</font></span></li><ul><li style="font-size: 12px;"><span style="font-size: 8pt;"><font face="Trebuchet MS, 宋体">值得注意的是这4个条件并非是互斥条件，比如有一笔资料它的MO、USN都不为空，那么就会同时进入两个Route进行输出，所以<span style="color: red;">Output笔数会是2</span></font></span></li><span style="font-size: 13.3333px; font-size: 13.3333px;"></span><li><font face="Trebuchet MS, 宋体"><span style="font-size: 10.6667px;">下面这是进入UPN这个Route后的资料上的Attribute: "RouteOnAttribute.Route"<br /><br /><img src="http://www.blogjava.net/images/blogjava_net/tw-ddm/Route3.png" width="600" height="405" alt="" /></span></font></li></ul></ul><span style="font-size: 13.3333px;"><br /><br /></span><span style="font-family: Arial; font-style: italic; background-color: #ffffff; font-size: 10pt;"><strong>&nbsp; &nbsp; UpdateAttribute</strong>，</span><span style="background-color: #ffffff; font-size: 10pt; font-family: Arial;">比较精彩的用法</span><br /><span style="font-size: 13.3333px;"><img src="http://www.blogjava.net/images/blogjava_net/tw-ddm/UpdateAttributeProcessor4.png" width="600" height="442" alt="" /><br /></span><ul style="font-size: 12px; margin-left: 1em; background-color: #ffffff;"><li><span style="font-size: 8pt;"><font face="Comic Sans MS"><em>mqttTopic</em></font><font face="Trebuchet MS, 宋体">：这是定义的一个MQTT的Topic变量，它的内容是可变动的，会根据Route的不同产生不同的结果</font></span></li><ul><li><span style="font-size: 8pt;"><font face="Trebuchet MS, 宋体">MO/MODELFAMILY/UPN/USN这4个条件有任一不为空，则会要求推送Topic： xxxx/【Type】/【Value】/yyyy</font></span></li><span style="font-size: 13.3333px;"></span><ul><li><span style="font-size: 8pt;"><font face="Trebuchet MS, 宋体">【Type】:&nbsp;为"mo"、"modelfamily"、"upn"、"usn"这四个值之一</font></span></li><span style="font-size: 13.3333px;"></span><li><span style="font-size: 8pt;"><font face="Trebuchet MS, 宋体">【Value】:&nbsp;为MO/MODELFAMILY/UPN/USN这四个Attribute的取值 (即 ${MO} / ${MODELFAMILY}&nbsp;/ ${UPN}&nbsp;/ ${USN}&nbsp;的值)，这里使用了双层<span style="color: red;">${${"RouteOnAttribute.Route"}}</span>的取值方法来实现动态的取用变量值</font></span></li></ul></ul></ul><span style="font-size: 13.3333px;"><br /><br /><br /></span></div><img src ="http://www.blogjava.net/tw-ddm/aggbug/433714.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/tw-ddm/" target="_blank">大大毛</a> 2019-04-11 16:55 <a href="http://www.blogjava.net/tw-ddm/articles/433714.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Kafka资料落地至MariaDB （带Key的新增、修改和删除）</title><link>http://www.blogjava.net/tw-ddm/articles/433713.html</link><dc:creator>大大毛</dc:creator><author>大大毛</author><pubDate>Thu, 11 Apr 2019 07:40:00 GMT</pubDate><guid>http://www.blogjava.net/tw-ddm/articles/433713.html</guid><wfw:comment>http://www.blogjava.net/tw-ddm/comments/433713.html</wfw:comment><comments>http://www.blogjava.net/tw-ddm/articles/433713.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/tw-ddm/comments/commentRss/433713.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/tw-ddm/services/trackbacks/433713.html</trackback:ping><description><![CDATA[<h5>需求：</h5><span style="font-family: Arial; font-size: 10pt;">&nbsp; &nbsp; 比较前一篇文章来说，仅加多Delete的行为。例如我仅需要Status=1的资料，所以对于资料落地来讲，最合适的莫过于下面这样，Table也可以自动保持最少量的有效资料<br /></span><span style="font-family: Arial; font-size: 10pt;">&nbsp; &nbsp; 1.&nbsp;新资料Status=0，执行Insert;<br /></span><span style="font-family: Arial; font-size: 10pt;">&nbsp; &nbsp; 2.&nbsp;资料修改Status=0，执行Update;<br /></span><span style="font-family: Arial; font-size: 10pt;">&nbsp; &nbsp; 3.&nbsp;资料状态变更Status=1，执行Delete;<br /></span><span style="font-family: Arial; font-size: 10pt;">&nbsp; &nbsp; 4.&nbsp;若资料状态重新变更为0，则又会执行Insert;</span><br /><br /><h5><span style="font-family: Arial; font-size: 10pt;">思路：</span></h5><span style="font-family: Arial; font-size: 10pt;">&nbsp; </span><span style="font-family: Arial; font-size: 10pt;">&nbsp; 按理来说，只要通过分支Route就可以将Insert/Update与Delete作业分成两条Nifi支流(看网上确实有很多这么整法的)，但是用Route有一个问题处理不了，那就是资料顺序的正确性你是无法保证的。对于小数据量的场景来说，每笔Key的多次操作间隔可能会比较长，所以它不会有什么问题，但大数据量的情况下，两同相同Key值的资料走Route后被处理的顺序混乱就会造成最终资料结果的异常(比如应该是先Insert再Delete，结果却是发现资料还躺在Table中)。而大数据量在使用Kafka做为数据源时就不可避免会出现：即使业务数据量确实不大，但对于积累了好几天的数据再进行接收时，那一瞬间的数据量也会是很大的。<br /></span><span style="font-size: 10pt; font-family: Arial;">&nbsp; &nbsp;所以我们能做的就是动态决定执行Delete和Insert。</span><br /><br /><h5>解决方案：</h5><span style="font-family: Arial; font-size: 10pt;">虽然与<a href="http://www.blogjava.net/tw-ddm/articles/433711.html">前一篇</a>来说差异不大，但Nifi流程上却有很大不同，下面会详细描述为什么要这样做<br /><img src="http://www.blogjava.net/images/blogjava_net/tw-ddm/ReceiveData_Delete.png" alt="" /><br /><br /><br /></span><h5><span style="font-family: Arial; font-size: 10pt;">Processor及其设定：</span></h5><em style="font-family: &quot;Comic Sans MS&quot;; font-size: 13.3333px; background-color: #ffffff;"><strong>&nbsp; &nbsp; ConsumeKafkaRecord、SplitJson、Connection、EvaluateJsonPath</strong></em><span style="font-family: &quot;Trebuchet MS&quot;, 宋体; font-style: italic; background-color: #ffffff; font-size: 10pt;">，</span><span style="font-family: &quot;Trebuchet MS&quot;, 宋体; background-color: #ffffff; font-size: 10pt;">与前一章的一样，只是不同数据下解析的属性有所不同，这里不再详述。</span><span style="font-family: &quot;Trebuchet MS&quot;, 宋体; background-color: #ffffff; font-size: 10pt;"><br /></span><span style="font-family: Arial; font-size: 10pt;"><br /></span><em style="font-family: &quot;Comic Sans MS&quot;; font-size: 13.3333px; background-color: #ffffff;"><strong>&nbsp; &nbsp; UpdateAttribute</strong></em><span style="font-family: &quot;Trebuchet MS&quot;, 宋体; font-style: italic; background-color: #ffffff; font-size: 10pt;">，</span><span style="font-family: &quot;Trebuchet MS&quot;, 宋体; background-color: #ffffff; font-size: 10pt;">作用是从Kafka中Consume出资料(以Record的形态），这里使用Record是因为源数据就是以Record的方式存上去的 (Avro Schema)<br /><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><span style="font-family: &quot;Trebuchet MS&quot;, 宋体; background-color: #ffffff; font-size: 10pt;"><img src="http://www.blogjava.net/images/blogjava_net/tw-ddm/UpdateAttributeProcessor2.png" width="600" height="442" alt="" /></span></blockquote></span><ul style="font-size: 12px; font-family: &quot;Trebuchet MS&quot;, 宋体; margin-left: 1em; background-color: #ffffff;"><li><span style="font-size: 8pt;"><em>SQL1</em>：这才是这次花招的关键，在这里根据STATUS自行构建SQL语句</span></li><ul><li><span style="font-size: 8pt;"><em>Status=0</em>：构建的就是Delete&nbsp;From xxx Where Key1=? and Key2=? 这样的删除语句</span></li><li><span style="font-size: 8pt;"><em>Status!=0</em>：构建的就是Replace Into xxx (Key1,Key2,col3,col4) Values ( ?, ?, ?, ?) 这样的Insert or Update语句</span></li></ul><li>经此Processor处理后，资料落地所需的SQL就构建好了，后续的问题就是如何去绑定参数和执行</li></ul><br /><em style="font-family: &quot;Comic Sans MS&quot;; font-size: 13.3333px; background-color: #ffffff;"><strong>&nbsp; &nbsp; ReplaceText</strong></em><span style="font-family: &quot;Trebuchet MS&quot;, 宋体; font-style: italic; background-color: #ffffff; font-size: 10pt;">，</span><span style="font-family: &quot;Trebuchet MS&quot;, 宋体; background-color: #ffffff; font-size: 10pt;">作用是对FlowFile进行文本替换，这里使用它来直接产生我所需的JSON内容</span><br /><span style="font-family: Arial; font-size: 10pt;"><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><span style="font-family: Arial; font-size: 10pt;"><img src="http://www.blogjava.net/images/blogjava_net/tw-ddm/ReplaceTextProcessor2.png" width="600" height="442" alt="" /></span></blockquote></span><ul style="font-family: &quot;Trebuchet MS&quot;, 宋体; margin-left: 1em; background-color: #ffffff;"><li style="font-size: 12px;"><span style="font-size: 8pt;"><em>Search&nbsp;Value</em>：这里使用的是Default的</span>(?s)(^.*$)，作用就是把原先的整份文件全部换掉</li><li style="font-size: 12px;"><em style="font-size: 8pt;">Replacement Value</em><span style="font-size: 8pt;">：这里放的就是一个固定结构的JSON，可以看到里面的属性值都是使用的Attribute (它们的值来源于前面的EvaluateJsonPath从源JSON文件中的提取)</span></li><ul style="font-size: 12px;"></ul><li><span style="font-size: 10.6667px;">细心的朋友可以发现这里是与前一篇文章的最大不同，这里没有使用AttributeToJson去直接产生JSON文件，而使用的是更加笨拙的方式</span></li><ul><li><span style="font-size: 10.6667px;">前面的文章有提过，我们产生的Attribute以及AttributeToJson所生成JSON中各属性的顺序问题，结论是怎么搞它都不是我所想象到的顺序。但是ConvertJsonToSQL这个东东却很实在，它确确实实是按JSON中属性的顺序去生成的SQL以及参数名称(还记得参数名称sql.args.</span><span style="font-size: 10.6667px; color: red;">1</span><span style="font-size: 10.6667px;">.value中的这个顺序</span><span style="font-size: 10.6667px; color: red;">1</span><span style="font-size: 10.6667px;">么)，所以问题就来了：</span><br /></li><ul><li style="font-size: 12px;">SQL由于必须要有Delete和Replace，所以它们的参数个数一定是不同的，而Delete压的参数又是我们的Key，所以就必须要保证ConvertJsonToSQL生成属性的顺序，这样我们才能够保证我们的两个Key一定会是sql.args.1和sql.args.2</li></ul><span style="font-family: Arial; font-size: 10pt;"></span><ul><li style="font-size: 12px;">换句话说，如果AttributesToJson若是能够保证JSON属性顺序的话，那就不用这么费劲</li></ul></ul></ul><span style="font-family: Arial; font-size: 10pt;"><br /></span><em style="font-family: &quot;Comic Sans MS&quot;; font-size: 13.3333px; background-color: #ffffff;"><strong>&nbsp; &nbsp; ConvertJsonToSQL</strong></em><span style="font-family: &quot;Trebuchet MS&quot;, 宋体; font-style: italic; background-color: #ffffff; font-size: 10pt;">，</span><span style="font-family: &quot;Trebuchet MS&quot;, 宋体; font-size: 13.3333px; background-color: #ffffff;">与前文一样，以Insert的方式生成SQL和绑定参数即可</span><br /><span style="font-family: Arial; font-size: 10pt;"><br /></span><em style="font-family: &quot;Comic Sans MS&quot;; font-size: 13.3333px; background-color: #ffffff;"><strong>&nbsp; &nbsp; UpdateAttribute</strong></em><span style="font-family: &quot;Trebuchet MS&quot;, 宋体; font-style: italic; background-color: #ffffff; font-size: 10pt;">，</span><span style="background-color: #ffffff;"><font face="Trebuchet MS, 宋体"><span style="font-size: 10pt;">终于用到了它的Delete功能，作用是清除掉多余的SQL绑定参数</span></font><br /><blockquote style="font-family: &quot;Trebuchet MS&quot;, 宋体; font-size: 10pt; margin: 0px 0px 0px 40px; border: none; padding: 0px;"><span style="font-family: &quot;Trebuchet MS&quot;, 宋体; background-color: #ffffff; font-size: 10pt;"><img src="http://www.blogjava.net/images/blogjava_net/tw-ddm/UpdateAttributeProcessor3.png" width="600" height="441" alt="" /><br /></span></blockquote></span><ul style="font-family: &quot;Trebuchet MS&quot;, 宋体; margin-left: 1em; background-color: #ffffff;"><li style="font-size: 12px;"><span style="font-size: 8pt;"><em>Delete&nbsp;Attributes&nbsp;Expression</em>：这里我根据Delete的条件(STATUS=0)去删除多余的SQL绑定参数</span></li><ul><li style="font-size: 12px;"><span style="font-size: 8pt;">这里的写法比较死，我Hard-code删除掉大与2的其它所有参数("&nbsp;</span><span style="font-size: 8pt; color: red;">*</span><span style="font-size: 8pt;"> "</span><span style="font-size: 8pt;">是一个通配符，" </span><span style="font-size: 8pt; color: red;">|</span><span style="font-size: 8pt;"> "是一个多条件的间隔符)，感觉上还有更好的写法</span></li></ul><li style="font-size: 12px;"><span style="font-size: 8pt;">至此我们就可以保证绑定参数的数量与SQL语法参数个数一致 (不一致它死给你看)</span></li><ul style="font-size: 12px;"></ul></ul><span style="background-color: #ffffff;"></span><span style="background-color: #ffffff;"><span style="font-family: &quot;Trebuchet MS&quot;, 宋体; background-color: #ffffff; font-size: 10pt;"><br /></span></span><em style="font-family: &quot;Comic Sans MS&quot;; font-size: 13.3333px; background-color: #ffffff;"><strong>&nbsp; &nbsp; PutSQL</strong></em><span style="font-family: &quot;Trebuchet MS&quot;, 宋体; font-style: italic; background-color: #ffffff; font-size: 10pt;">，</span><span style="font-family: &quot;Trebuchet MS&quot;, 宋体; background-color: #ffffff; font-size: 10pt;">这里仍然是执行SQL，这里使用配置参数的形式让它执行我们的SQL</span><br /><span style="font-family: Arial; font-size: 10pt;"><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><span style="font-family: Arial; font-size: 10pt;"><img src="http://www.blogjava.net/images/blogjava_net/tw-ddm/PutSQLProcessor2.png" width="600" height="440" alt="" /></span></blockquote></span><span style="font-family: Arial; font-size: 10pt;"><br /></span><ul style="font-family: &quot;Trebuchet MS&quot;, 宋体; margin-left: 1em; background-color: #ffffff;"><li style="font-size: 12px;"><span style="font-size: 8pt;"><em>SQL&nbsp;Statement</em>：前面用UpdateAttribute产生的SQL1参数，它会根据STATUS=0去判断是使用DELETE还是REPLACE语法</span></li><ul><li style="font-size: 12px;"><span style="font-size: 8pt;">这个属性压上后，无论SQL1是不是为空，这个组件都不会再去管FlowFile的内容(空属性时是把FlowFile的内容当成SQL去执行的)</span></li></ul></ul><img src ="http://www.blogjava.net/tw-ddm/aggbug/433713.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/tw-ddm/" target="_blank">大大毛</a> 2019-04-11 15:40 <a href="http://www.blogjava.net/tw-ddm/articles/433713.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Kafka资料落地至MariaDB （带Key的新增、修改）</title><link>http://www.blogjava.net/tw-ddm/articles/433711.html</link><dc:creator>大大毛</dc:creator><author>大大毛</author><pubDate>Thu, 11 Apr 2019 06:14:00 GMT</pubDate><guid>http://www.blogjava.net/tw-ddm/articles/433711.html</guid><wfw:comment>http://www.blogjava.net/tw-ddm/comments/433711.html</wfw:comment><comments>http://www.blogjava.net/tw-ddm/articles/433711.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/tw-ddm/comments/commentRss/433711.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/tw-ddm/services/trackbacks/433711.html</trackback:ping><description><![CDATA[<h5>需求：</h5><span style="font-size: 10pt;">&nbsp; &nbsp; 接收Kafka资料，资料具有Key列(多列)，有新增、修改但无删除，需要同步落地至MariaDB</span><br /><h5>解决方案(仅新增、修改)：</h5><span style="font-size: 10pt;">&nbsp; &nbsp; 这个场景是最常见的，资料不会有被删除的状态，所有的更新就只有Insert Or Update这两种状态，先上实例的图 (两边的LogMessage是为了接收Fail，有感叹号是避免一起开启的时候它也被开启----这样failure的讯息就不会再卡在Connection中了)<br /><img src="http://www.blogjava.net/images/blogjava_net/tw-ddm/ReceiveData_NoDelete.png" width="1332" height="546" alt="" /><br /></span><br /><h6><span style="font-size: 10pt; font-family: Arial;">思路：</span></h6><span style="font-size: 10pt; font-family: Arial;">&nbsp; &nbsp; 因为记录只有新增和修改两种状态，理论上说这两种的SQL非常接近，所以可以做以下考量<br /></span><span style="font-size: 10pt; font-family: Arial;">&nbsp; &nbsp; 1. Processor层面是否支援Update&nbsp;Or Insert<br /></span><span style="font-size: 10pt;">&nbsp; &nbsp; &nbsp;</span><span style="font-size: 10pt; font-family: Arial;"> &gt;&nbsp;查网上讯息有个叫Upsert，不过在Nifi中查找，只有一个支援Mongo的组件具有这个功能</span><span style="font-size: 10pt;"><br /></span><span style="font-size: 10pt; font-family: Arial;">&nbsp; &nbsp; 2. DB层面是否支援<br /></span><span style="font-family: Arial; font-size: 10pt;">&nbsp; &nbsp; &nbsp; &gt; Maria DB有个 "REPLACE INTO"&nbsp;的语法是可以支持Insert&nbsp;Or&nbsp;Update，虽然简单看了下介绍说是会依主键或唯一索引去先做定位，如果定位到已经存在则先做删除再进行新增（伪Update），但确实可以达成我们的目的，不是吗？</span><br /><br /><h6><font face="Arial"><span style="font-size: 10pt; font-family: Arial;">Processor及其设定：</span></font></h6><div><em style="font-family: &quot;Comic Sans MS&quot;; font-size: 13.3333px; background-color: #ffffff;"><strong>&nbsp; &nbsp; ConsumeKafkaRecord</strong></em><span style="font-size: 10pt;">，作用是从Kafka中Consume出资料(以Record的形态），这里使用Record是因为源数据就是以Record的方式存上去的 (Avro Schema)</span></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><img src="http://www.blogjava.net/images/blogjava_net/tw-ddm/ConsumeKafkaProcessor.png" width="600" height="440" alt="" /></blockquote><ul style="font-size: 12px; margin-left: 1em; background-color: #ffffff;"><li><span style="font-size: 8pt;"><font face="Comic Sans MS"><em>Kafka&nbsp;Brokers</em></font><font face="Trebuchet MS, 宋体">：Kafka的Broker列表，多个Broker以逗号分隔，类似www.broker1.com:9193,www.broker2.com:9193这样的形式配置</font></span></li><li><span style="font-size: 8pt;"><font face="Comic Sans MS"><em>Topic&nbsp;Name</em></font><font face="Trebuchet MS, 宋体">：需要Consume的Kafka&nbsp;Topic名称</font></span></li><li style="font-family: &quot;Trebuchet MS&quot;, 宋体;">Record&nbsp;Reader/Writer：关于Record所需要设定的Reader和Writer，要先行在Configure中设定，当然也要设定好Schema&nbsp;&nbsp;<br /></li><li><span style="font-size: 8pt;"><font face="Comic Sans MS"><em>Group ID</em></font>：Consumer所要设定的ID，这个的设定要依Kafka的配置来，现在我们一般就只有单个的Partition，所以会要求每个Processor都设定有不同</span></li><li style="font-family: &quot;Trebuchet MS&quot;, 宋体;">Offset&nbsp;Reset：需要设定为"earliest"，这样就会依GroupID没有收过的资料来进行收取，否则就只会收新推上去的资料。第一次玩的兄弟经常坑在GroupID和Offset&nbsp;Reset这两项上，若是收不到资料则有&nbsp; 可能就是GroupID没有换成新的(旧的已经收过一次就不会重新再收)，或者是Offset Reset =&nbsp;latest又没有新资料推上去~~~<br /></li><li><span style="font-size: 8pt;"><font face="Comic Sans MS"><em>Max&nbsp;Poll&nbsp;Records和SCHEDULING中的Run&nbsp;Schedule</em></font>：需要根据实际接收的速度来进行调整。经过观察发现Consume的速度超快，但整个Nifi&nbsp;Flow的速度会卡在其它需要做解析或读写DB的Processor外 (通常解析JSON会是前面的关卡)，所以任由Consumer的高速读取就会造成整个Nifi流程在后段被卡住。造成这个的主要原因其实就在于kafka处理的高速上，所以当有新换GroupID或新流程时，Kafka上积累的海量资料就会在一瞬间被接收下来，然后就是各种红 (其实红了也没事，它会自动向上推，让前一个Processor停止处理)。<br /></span></li><ul><li style="font-family: &quot;Trebuchet MS&quot;, 宋体;">若是常态下的资料推送量就已经超过了你的Nifi处理速度，那么就要考量使用多个线程处理或者是从源头的Kafka上就把资料分割开来&nbsp;&nbsp;</li><li><span style="font-size: 8pt;"><font face="Comic Sans MS"><em>SCHEDULING的Cocurrent Tasks</em></font>：这个Default=1，就是当前Processor需要开起来的线程数。但是这个设置需要当心，你需要仔细考量过你的资料流是否允许乱序 (多线程时当然不可能还能保证资料处理的顺序)，所以它是仅适用于不Care资料处理顺序的场景，例如每笔Key就只会有一笔资料，而且哪笔资料先收后收无所谓</span></li></ul><br /></ul><div style="display: inline-block;"><em style="color: #333333; font-family: &quot;Comic Sans MS&quot;; font-size: 13.3333px; background-color: #ffffff;"><strong>&nbsp; &nbsp; SplitJson</strong></em><span style="color: #333333; font-size: 10pt;">，作用就只是简单的把一个JSON数组切开成单个的JSON。Consume出来的会是个数组，这跟你存放进去的单笔讯息是不是数组没什么关系。</span></div><div><span style="font-size: 13.3333px;"><br /></span><div><em style="font-family: &quot;Comic Sans MS&quot;; font-size: 13.3333px; background-color: #ffffff;"><strong>&nbsp; &nbsp; Connection</strong></em><span style="font-size: 10pt;">，就是Processor中的那根带箭头的连线，它的作用是连接不同的Processor并且它还具有缓存池的的一个用途，除了把数据从A导流向B外，还可以将B暂时处理不动的资料存放在自带的缓存池中，若是缓存池达到上限，则Nifi会自动让A暂停处理直至B缓过劲~~~</span></div></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><div><span style="font-size: 10pt;"><img src="http://www.blogjava.net/images/blogjava_net/tw-ddm/Connection.png" width="600" height="442" alt="" /></span></div></div></blockquote><div><ul style="font-size: 12px; margin-left: 1em; background-color: #ffffff;"><li><span style="font-size: 8pt;"><em>Back Pressure Object Threshold / Back Pressure Data Size Threshold</em>：最大缓存的消息笔数 /&nbsp;最大缓存消息的体积，两者任一超过就会让上游Processor处理暂停</span></li><li><span style="font-size: 8pt;"><em>Available Prioritizers</em>：出入缓存池的顺序控制，Default是空，通常来说都应该要设成FIFO先进先出的方式</span></li><span style="font-size: 13.3333px;"></span><ul><li><span style="font-size: 8pt;">不设定这个经常会造成Nifi资料处理丢失的假象，A1,A2,A3,A4，最后看到的不是A4而是A3，会让人以为A4被玩掉了，其实只是A4被先处理，而A3变成了最后一笔状态。而且这种错误很难被发现!!</span></li></ul></ul><span style="font-size: 13.3333px;"><br /><br /></span></div><div><div style="display: inline-block;"><em style="color: #333333; font-family: &quot;Comic Sans MS&quot;; font-size: 13.3333px; background-color: #ffffff;"><strong>&nbsp; &nbsp; EvaluateJsonPath1</strong></em><span style="color: #333333; font-size: 10pt;">，这个元件的作用是解析JSON，它也只能简单的解析，想在Value中对取出来的值做一些处理好象是不允许的....</span></div></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><span style="font-size: 13.3333px;"><img src="http://www.blogjava.net/images/blogjava_net/tw-ddm/EvaluateJsonProcessor1.png" width="600" height="441" alt="" /></span></div></blockquote><div><ul style="font-size: 12px; margin-left: 1em; background-color: #ffffff;"><li><span style="font-size: 8pt;"><em>Destination</em>：表示解析出来的内容是成为Attribute，还是直接替换Flow&nbsp;File内容，这里设定是做为属性，所以Processor处理后就可以在Flow&nbsp;File上看到多出自定义的那些属性以及它们的值</span></li><li><span style="font-size: 8pt;"><em>Return Type</em>：返回值的类型，这种简单从JSON中取值的可以使用Auto-detect即可</span></li><li><span style="font-size: 8pt;"><em>Path&nbsp;Not&nbsp;Found Behavior</em>：是说如果设定需要解析的JSON路径不存在时的处理行为</span></li><li style="font-family: &quot;Trebuchet MS&quot;, 宋体;">Null Value Representation：这个对于Null值的处理， "empty string"会将null设为空字符串(MO=)，另外一个"the string 'null'"则是会将null设为"null"这样的字符串 (MO="null")<br /></li><li><span style="font-size: 8pt;"><em>MO/MODELFAMILY/....</em>：这些是我手工添加的属性名称，需要根据JSON长样来设，对应Value设定的$.MO则是表示MO的值来源于JSON第一层的"MO"节点。</span></li><ul><li><span style="font-size: 8pt;">需要注意的一点是属性名称貌似是会区分大小写的，所以可以看到我全部使用的大写</span></li></ul><li style="font-family: &quot;Trebuchet MS&quot;, 宋体;">截图是运行时态的Procssor，停止运行时PROPERTIES上会有一个 +&nbsp;号，点它即可以新增自己的属性<br /></li><ul><li>有一点比较奇怪的地方，就是通过+号维护进去的多个属性，它们的排列顺序却不是你手工新增的顺序，这点引发另外一处的一个疑问，会在下面讲<br /><br /></li></ul></ul></div><div><div style="display: inline-block;"><em style="color: #333333; font-family: &quot;Comic Sans MS&quot;; font-size: 13.3333px; background-color: #ffffff;"><strong>&nbsp; &nbsp; EvaluateJsonPath2</strong></em><span style="color: #333333; font-size: 10pt;">，当然也是要从JSON中解析，只不过我是要把整个JSON的内容都保留下来，由于它们要求的设定不同，所以被迫要撕成两个元件来做</span></div></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><span style="font-size: 13.3333px;"><img src="http://www.blogjava.net/images/blogjava_net/tw-ddm/EvaluateJsonProcessor2.png" width="600" height="442" alt="" /></span></div></blockquote><div><ul style="font-size: 12px; margin-left: 1em; background-color: #ffffff;"><li><span style="font-size: 8pt;"><em>Destination</em>：这个设定仍然是属性</span></li><li><span style="font-size: 8pt;"><em>Return Type</em>：json，第一个解析元件虽然可以随意设置，但把这两种合并成一个元件并使用Auto时就会报错，所以看起来第一种简单属性实际上只支持Scalar吧...</span></li><li><span style="font-size: 8pt;"><em>JSONDATA</em>：我定义的一个属性名称，注意Value中设定的"@"符号，它表示整份FlowFile的内容(前面已经转成一个JSON)</span></li><li style="font-family: &quot;Trebuchet MS&quot;, 宋体;"><span style="font-size: 8pt;">这个JSONDATA是因为我的需求，因为Kafka上的资料来源于其它系统，而我其实只需要其中的少量几个栏位 (前一个EvaluateJsonPath解析的那些)，为了备查数据上的其它栏位以及在后续使用，所以才要把整份JSON都保留到DB中去 (说得这么高端，实际的原因却是他们的JSON属性是用程序硬拼字串拼出来的，有的东西实在是在Nifi中搞不出来......)</span></li></ul><span style="font-size: 13.3333px;"><br /></span></div><div><div><div style="display: inline-block;"><span style="color: #333333; font-size: 13.3333px; background-color: #ffffff;"><font face="Comic Sans MS"><strong><em>&nbsp; &nbsp; UpdateAttribute</em></strong></font></span><span style="color: #333333; font-size: 10pt;">，元件用途是对FlowFile的Attrubute进行修改，这里是拿来对解析出来的值进行再加工以及添加新属性</span></div></div></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><div><div style="display: inline-block;"><span style="color: #333333; font-size: 10pt;"><img src="http://www.blogjava.net/images/blogjava_net/tw-ddm/UpdateAttributeProcessor1.png" width="600" height="441" alt="" /></span></div></div></div></blockquote><div><div><div style="display: inline-block;"><ul style="font-size: 12px; margin-left: 1em; background-color: #ffffff;"><li><span style="font-size: 8pt;"><em>Delete&nbsp;Attributes&nbsp;Expression</em>：这个属性如果有设置就表示该Processor为Delete属性的状态，会忽略你新加的那些属性处理，只专心做好一件事"删除符合条件的属性"</span></li><li><span style="font-size: 8pt;"><em>PROVIDER</em>：这是一个新的属性，它并没有包含在JSON中，是为表示数据来源而新加的</span></li><li><span style="font-size: 8pt;"><em>SO</em>：这个就是前面<div style="display: inline-block;">EvaluateJsonPath1解析出来的某个值，那个元件无法直接加工，所以放在这里做的二次加工，去掉前导0</div></span></li></ul><span style="color: #333333; font-size: 10pt;"><br /></span></div></div></div><div><div><div style="display: inline-block;"><span style="color: #333333; font-size: 13.3333px; background-color: #ffffff;"><font face="Comic Sans MS"><strong><em>&nbsp; &nbsp; AttributesToJson</em></strong></font></span><span style="color: #333333; font-size: 10pt;">，作用是将一堆Attribute转换为Json，当然就只能是那种简单结构的Json，这里使用它是为了配合后面一个Processor的使用</span></div></div></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><div><div style="display: inline-block;"><span style="color: #333333; font-size: 10pt;"><img src="http://www.blogjava.net/images/blogjava_net/tw-ddm/AttributeToJsonProcessor.png" width="600" height="441" alt="" /></span></div></div></div></blockquote><div><div style="display: inline-block;"><ul style="margin-left: 1em; background-color: #ffffff;"><li style="font-size: 12px;"><span style="font-size: 8pt;"><em>Attributes&nbsp;List</em>：拿来生成JSON的属性列表，这里我其实把EvaluateJsonPath1、EvaluateJsonPath2和UpdateAttribute产生的属性都放上去了 (它们就是我落地MariaDB的Table列)</span></li><ul><li><span style="font-size: 10.6667px;">不得不说的一个灰常遗憾的结果：那就是生成的JSON属性顺序绝对不是你在List中写的属性顺序，我比较怀疑是在前面几个组件生成Attribute的顺序，但更让人遗憾的是它们的顺序也不会是你维护它们的顺序。这个结果会导致我们在另外的Case 2中会碰到一个不可逾越的障碍~~~~</span></li></ul><li style="font-size: 12px;"><span style="font-size: 8pt;"><em>Attributes Regular Expression</em>：符合条件的正则表达式</span></li><li style="font-size: 12px;"><span style="font-size: 8pt;"><em>Destination</em>： 这个属性在&nbsp;<div style="display: inline-block;">EvaluateJsonPath上</div><div style="display: inline-block;">就有， 它可以让结果成为一个新的属性还是直接替换FlowFile的内容， Default是直接换掉FlowFile的内容。</div></span></li></ul><span style="color: #333333; font-size: 10pt;"></span><span style="color: #333333; font-size: 10pt;"><br /></span></div></div><span style="color: #333333; font-size: 13.3333px; background-color: #ffffff;"><font face="Comic Sans MS"><strong><em>&nbsp; &nbsp; ConvertJsonToSQL</em></strong></font></span><span style="color: #333333; font-size: 10pt;">，作用是根据JSON内容转换成SQL语句以及语句所要的参数，经过这一关后FlowFile的内容就变成SQL语句，然后Attribute中多出一些参数</span><br /><blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px;"><div style="display: inline-block;"><span style="color: #333333; font-size: 10pt;"><img src="http://www.blogjava.net/images/blogjava_net/tw-ddm/ConvertJsonToSqlProcessor.png" width="600" height="441" alt="" /><br /></span></div></blockquote><div><div><div style="display: inline-block;"><ul style="font-size: 12px; margin-left: 1em; background-color: #ffffff;"><li><span style="font-size: 8pt;"><em>JDBC&nbsp;Connection&nbsp;Pool</em>：Configrue中指定的MariaDB连接字符串，那里直接有指定Schema</span></li><li><span style="font-size: 8pt;"><em>Statement Type：</em>这个有INSERT、UPDATE、DELETE这3个选项，若是Mongo的那个组件就会看到有UPSERT(Update or Insert)，其它各类的都木有~~~，这里我使用的是INSERT选项，后面通过玩的一点小花招把它再折腾为REPLACE&nbsp;INTO</span></li><li><span style="font-size: 8pt;"><em>Update&nbsp;Keys</em>： 这个属性可以不填，它是For&nbsp;Update时使用的<div style="display: inline-block;">。</div></span></li><li><span style="font-size: 8pt;"><em>SQL&nbsp;Parameter&nbsp;Attribute&nbsp;Prefix</em>： default =&nbsp;sql，它其实影响到组件处理后生成的SQL语句参数叫什么，设为sql，最后就会看到生成出来<div style="display: inline-block;">。如下图就是处理后的Attribute样式，它会产生sql.args.X.type和sql.args.X.value，这一组合起来就对应于SQL中第一个?参数的类型及值，&#8221;sql"就是我们这里设置的前缀名称 (充分考虑到大家会想要搞事)</div></span></li></ul><span style="color: #333333; font-size: 10pt;"></span></div></div></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><img src="http://www.blogjava.net/images/blogjava_net/tw-ddm/SQLParmeters.png" width="600" height="406" alt="" /><br /></blockquote><div><div><div style="display: inline-block;"><span style="color: #333333; font-size: 10pt;"><div style="display: inline-block;"></div></span></div></div></div><div><div><div style="display: inline-block;"><div><div style="display: inline-block;"><span style="color: #333333; font-size: 13.3333px; background-color: #ffffff;"><font face="Comic Sans MS"><strong><em>&nbsp; &nbsp; ReplaceText</em></strong></font></span><span style="color: #333333; font-size: 10pt;">，作用是文本替换，这里就是我们处理Update&nbsp;Or&nbsp;Insert的关键，直接把SQL语句换掉它</span></div></div></div></div></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><div><div style="display: inline-block;"><div><div style="display: inline-block;"><span style="color: #333333; font-size: 10pt;"><div style="display: inline-block;"><img src="http://www.blogjava.net/images/blogjava_net/tw-ddm/ReplaceTextProcessor.png" width="600" height="441" alt="" /></div></span></div></div></div></div></div></blockquote><div><div><div style="display: inline-block;"><div><div style="display: inline-block;"><ul style="font-size: 12px; margin-left: 1em; background-color: #ffffff;"><li><span style="font-size: 8pt;"><em>Search Value</em>：在FlowFile中查找的字符串，它支持正则</span></li><li><span style="font-size: 8pt;"><em>Replacement&nbsp;Value：</em>替换的值，这里就是简单的把Insert Into (x1,x2,x3) values (?,?,?)处理为Replace Into (x1,x2,x3) values (?,?,?)而已，Replace&nbsp;Or&nbsp;Insert的行为交给DB去做<br /><br /></span></li></ul><span style="color: #333333; font-size: 10pt;"><div style="display: inline-block;"></div></span></div></div></div></div></div><div><div><div style="display: inline-block;"><div><div style="display: inline-block;"><div><div style="display: inline-block;"><span style="color: #333333; font-size: 13.3333px; background-color: #ffffff;"><font face="Comic Sans MS"><strong><em>&nbsp; &nbsp; PutSQL</em></strong></font></span><span style="color: #333333; font-size: 10pt;">，作用是在DB上执行一段SQL语句</span></div></div></div></div></div></div></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><div><div style="display: inline-block;"><div style="display: inline-block;"><div><div style="display: inline-block;"><span style="color: #333333; font-size: 10pt;"><img src="http://www.blogjava.net/images/blogjava_net/tw-ddm/PutSQLProcessor.png" width="600" height="444" alt="" /></span></div></div></div></div></div></div></blockquote><div><div><div style="display: inline-block;"><div style="display: inline-block;"><ul style="font-size: 12px; margin-left: 1em; background-color: #ffffff;"><li><span style="font-size: 8pt;"><em>JDBC&nbsp;Connection&nbsp;Pool</em>：前面ConvertJsonToSQL转换SQL时就有用过，指定数据库的连接</span></li><li><span style="font-size: 8pt;"><em>SQL&nbsp;Statement：</em>需执行的SQL，为空时表示使用前面传递过来的FlowFile的内容(已经是一个SQL语句)<br /><br /></span></li></ul><div><br /><h5>总结：</h5></div></div></div></div><span style="font-size: 10pt;">&nbsp; &nbsp; 这是一个带有Key值(多个Key列)的无删除行为的资料接收，所以可以利用AttributeToJSON去将提取出来的有用属性重新生成JSON文件，并直接利用ConvertJsonToSQL转换为Insert语句及对应的绑定参数，这里借用了MariaDB提供的Replace&nbsp;Into机制去自动使用表上的Key键去做Update更新，所以整个Nifi&nbsp;Flow还是比较简单。在后续文章中会讲到带Delete行为的资料接收方法以及无Key更新的解决方案</span></div><img src ="http://www.blogjava.net/tw-ddm/aggbug/433711.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/tw-ddm/" target="_blank">大大毛</a> 2019-04-11 14:14 <a href="http://www.blogjava.net/tw-ddm/articles/433711.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Oracle资料推送MQTT</title><link>http://www.blogjava.net/tw-ddm/articles/433709.html</link><dc:creator>大大毛</dc:creator><author>大大毛</author><pubDate>Wed, 10 Apr 2019 07:25:00 GMT</pubDate><guid>http://www.blogjava.net/tw-ddm/articles/433709.html</guid><wfw:comment>http://www.blogjava.net/tw-ddm/comments/433709.html</wfw:comment><comments>http://www.blogjava.net/tw-ddm/articles/433709.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/tw-ddm/comments/commentRss/433709.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/tw-ddm/services/trackbacks/433709.html</trackback:ping><description><![CDATA[<h5><span style="font-size: 10pt;">需求</span></h5>
<blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><span style="font-size: 10pt; font-family: Arial;">将资料从Oracle推至MQTT，资料结果使用JSON格式</span><br />
<span style="font-size: 10pt; font-family: Arial;">场景1：直接推送</span><br />
<span style="font-size: 10pt; font-family: Arial;">场景2：仅当资料有变更时才推送</span></blockquote><br />
<h5><span style="font-size: 10pt;">解决方案</span></h5>
<span style="font-size: 10pt; font-family: &quot;Comic Sans MS&quot;;">QueryDatabaseTable --&gt; ConvertAvroToJSON --&gt; PublishMQTT</span><br />
<img src="http://www.blogjava.net/images/blogjava_net/tw-ddm/Oracle2MQTT.png" width="299" height="406" alt="" /><br />
<br />
<h6><span style="font-size: 10pt;">Processor及其设定：</span></h6>
<blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px;"><span style="font-size: 10pt; font-family: &quot;Comic Sans MS&quot;;"><em><strong>QueryDatabaseTable</strong></em></span><span style="font-size: 10pt;">，</span><span style="font-size: 10pt; font-family: Arial;">作用是从DB中捞取资料，可以想象Nifi把它转成一个Select语句在执行</span></blockquote><br />
<img src="http://www.blogjava.net/images/blogjava_net/tw-ddm/QueryDbProcessor.png" width="600" height="440" alt="" /><br />
<ul>
     <li><span style="font-size: 8pt; font-family: &quot;Comic Sans MS&quot;;"><em>Database&nbsp;Connection&nbsp;Pooling&nbsp;Service</em></span><span style="font-size: 8pt;">：在Configure中设定的数据库连接，可以在Processor&nbsp;Group中被共用</span></li>
     <li><span style="font-size: 8pt; font-family: &quot;Comic Sans MS&quot;;"><em>Database Type</em></span><span style="font-size: 8pt;">：这个设定的是Oracle。其实它与数据库连接设定有点重叠，那边已经有指定是哪一种类型的DB，这里需要再指定，我想会不会是利用它来生成不同的Select查询语法?</span></li>
     <li><span style="font-size: 8pt; font-family: &quot;Comic Sans MS&quot;;"><em>Table Name</em></span><span style="font-size: 8pt;">：表名，我这里使用的是View名称，View其实就已经对Table做出一些限定，可以挑选列及设定查询条件</span></li>
     <li><span style="font-size: 8pt; font-family: &quot;Comic Sans MS&quot;;"><em>Maximum-value Columns</em></span><span style="font-size: 8pt;">：这个属性很重要，如果不设定则Nifi在查询的时候会捞取所有的资料，如果有设定某个列，则Nifi仅会捞取&#8220;新&#8221;资料</span></li>
     <ul>
         <li><span style="font-size: 8pt;">例如图上设定&#8220;BATCHID&#8221;这个列。第一次Nifi启动时捞取资料中最大BATCHID = 10，则下一次Nifi再次启动时就只会捞取BATCHID&gt;10的资料，并且会自动记录下已经捞过的最大值</span></li>
         <li><span style="font-size: 8pt;">最大值保存在下面这里，Processor上右键选择&#8220;View&nbsp;State&#8221;<br />
         <img src="http://www.blogjava.net/images/blogjava_net/tw-ddm/QueryDb_ViewState1.png" width="440" height="226" alt="" /><br />
         <br />
         </span></li>
         <li><span style="font-size: 8pt;">下图可以看到当前最新的值，点&#8220;Clear&nbsp;State&#8221;则可以将保留值清空（Nifi下次启动时就会捞取所有资料）<br />
         <img src="http://www.blogjava.net/images/blogjava_net/tw-ddm/QueryDb_ViewState2.png" width="600" height="440" alt="" /><br />
         </span></li>
     </ul>
     <li><span style="font-size: 8pt;">对于仅需要简单拉取资料的场景1来说，&#8220;Maximum-value Columns</span><span style="font-size: 8pt;">&#8221;置空即可；而对于仅在资料有更新时才要拉取的场景2来说，则需要设定并且在View中做出一些调整才可以达成</span></li>
     <ul>
         <li><span style="font-size: 8pt;">当有资料更新时，则被更新资料的BatchID会是更新的值，所以只要在View中虚拟BatchID列 = Max(BatchID)，就可以达成有资料更新才要拉取的效果</span></li>
     </ul>
</ul>
<blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><span style="font-size: 10pt; font-family: &quot;Comic Sans MS&quot;;"><em><strong>ConvertAvroToJSON</strong></em></span><span style="font-size: 10pt;">，将Avro类型资料转换为JSON，这个可以不用改设定</span></blockquote><br />
<blockquote style="margin: 0px 0px 0px 40px; border: none; padding: 0px;"><span style="font-size: 10pt; font-family: &quot;Comic Sans MS&quot;;"><em><strong>PublishMQTT</strong></em></span><span style="font-size: 10pt;">，将资料Publish到MQTT指定Topic</span></blockquote>
<div><img src="http://www.blogjava.net/images/blogjava_net/tw-ddm/PublishMQTTProcessor.png" width="600" height="382" alt="" /></div>
<ul>
     <li><span style="font-size: 8pt; font-family: &quot;Lucida Console&quot;;"><em>Broker URI</em></span><span style="font-size: 8pt; font-family: &quot;Lucida Console&quot;;">：</span><span style="font-size: 8pt; font-family: Arial;">MQTT的Broker地址</span></li>
     <li><span style="font-size: 8pt; font-family: &quot;Comic Sans MS&quot;;"><em>Client&nbsp;ID</em>：</span><span style="font-size: 8pt; font-family: Arial;">发布MQTT的Client端ID（注意不要多个Processor使用相同的Client&nbsp;ID，这样Processor容易被卡死）</span></li>
     <li><span style="font-size: 8pt; font-family: &quot;Comic Sans MS&quot;;"><em>Topic</em>：</span><span style="font-size: 8pt; font-family: Arial;">发布至MQTT的Topic名称</span></li>
     <li><span style="font-size: 8pt;"><em style="font-family: &quot;Comic Sans MS&quot;;">Retain&nbsp;Message</em>：</span><span style="font-size: 8pt; font-family: Arial;">MQTT的遗言属性，即是否保留推送的消息（若设为false，则仅有当前连上MQTT的客户端才能收到这笔消息）</span></li>
</ul>
<br />
<img src ="http://www.blogjava.net/tw-ddm/aggbug/433709.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/tw-ddm/" target="_blank">大大毛</a> 2019-04-10 15:25 <a href="http://www.blogjava.net/tw-ddm/articles/433709.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>ADO提交多筆SQL的問題</title><link>http://www.blogjava.net/tw-ddm/articles/255166.html</link><dc:creator>大大毛</dc:creator><author>大大毛</author><pubDate>Tue, 17 Feb 2009 10:50:00 GMT</pubDate><guid>http://www.blogjava.net/tw-ddm/articles/255166.html</guid><wfw:comment>http://www.blogjava.net/tw-ddm/comments/255166.html</wfw:comment><comments>http://www.blogjava.net/tw-ddm/articles/255166.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/tw-ddm/comments/commentRss/255166.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/tw-ddm/services/trackbacks/255166.html</trackback:ping><description><![CDATA[
		<p>
				<font face="Georgia" size="2">使用ADO時經常都會遇到要利用事務批次提交SQL的情況，如果使用不當的提交方式會出現預料外的問題，現在先看下面的示例代碼：<br /></font>
				<br />
		</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">
				<span style="COLOR: #008080"> 1</span> <span style="COLOR: #0000ff">Private</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Sub</span><span style="COLOR: #000000"> cmdRun_Click()<br /></span><span style="COLOR: #008080"> 2</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">On</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Error</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">GoTo</span><span style="COLOR: #000000"> ErrHandle<br /></span><span style="COLOR: #008080"> 3</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">Dim</span><span style="COLOR: #000000"> con </span><span style="COLOR: #0000ff">As</span><span style="COLOR: #000000"> Connection<br /></span><span style="COLOR: #008080"> 4</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">Dim</span><span style="COLOR: #000000"> strSQL </span><span style="COLOR: #0000ff">As</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">String</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 5</span> <span style="COLOR: #000000">    strSQL </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">""</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 6</span> <span style="COLOR: #000000">    </span><span style="COLOR: #008000">'</span><span style="COLOR: #008000">第一筆SQL1</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080"> 7</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">    strSQL </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> strSQL </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Insert Into tb1 Values (...)</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> vbNewLine<br /></span><span style="COLOR: #008080"> 8</span> <span style="COLOR: #000000">    </span><span style="COLOR: #008000">'</span><span style="COLOR: #008000">第二筆SQL2</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080"> 9</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">    strSQL </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> strSQL </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Update tb1 Set C=1 Where ...</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> vbNewLine<br /></span><span style="COLOR: #008080">10</span> <span style="COLOR: #000000">    </span><span style="COLOR: #008000">'</span><span style="COLOR: #008000">...</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">11</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">    </span><span style="COLOR: #008000">'</span><span style="COLOR: #008000">第N筆SQLn</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">12</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">    strSQL </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> strSQL </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Delete tb1 Where ...</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> vbNewLine<br /></span><span style="COLOR: #008080">13</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">Set</span><span style="COLOR: #000000"> con </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> getCon()  </span><span style="COLOR: #008000">'</span><span style="COLOR: #008000">獲取/打開DB連線</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">14</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">    </span><span style="COLOR: #008000">'</span><span style="COLOR: #008000">準備事務</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">15</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">    con.BeginTrans<br /></span><span style="COLOR: #008080">16</span> <span style="COLOR: #000000">    </span><span style="COLOR: #008000">'</span><span style="COLOR: #008000">一批一起執行</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">17</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">    con.Execute strSQL<br /></span><span style="COLOR: #008080">18</span> <span style="COLOR: #000000">    </span><span style="COLOR: #008000">'</span><span style="COLOR: #008000">提交事務</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">19</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">    con.CommitTrans<br /></span><span style="COLOR: #008080">20</span> <span style="COLOR: #000000">ExitHandle:<br /></span><span style="COLOR: #008080">21</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">If</span><span style="COLOR: #000000"> con.State </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">1</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Then</span><span style="COLOR: #000000"> con.Close<br /></span><span style="COLOR: #008080">22</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">Set</span><span style="COLOR: #000000"> con </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Nothing</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">23</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">Exit</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Function</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">24</span> <span style="COLOR: #000000">ErrHandle:<br /></span><span style="COLOR: #008080">25</span> <span style="COLOR: #000000">    con.RollbackTrans<br /></span><span style="COLOR: #008080">26</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">GoTo</span><span style="COLOR: #000000"> ExitHandle<br /><span style="COLOR: #008080">27</span><font style="BACKGROUND-COLOR: #eeeeee"> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff"><font color="#0000ff">MsgBox</font></span><span style="COLOR: #000000"> Err.Description<br /></span></font></span><span style="COLOR: #008080">28</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">End Sub</span></div>
		<br />
		<font face="Georgia" size="2">   上面這段程式<strong>利用換行符(分號也可)來將多條SQL合成一個字符串，最後在事務中執行一次</strong>就OK，既簡潔又漂亮的想法。<br />   看似OK，但實際卻是埋下了禍端。從事務的機制來看，在事務中提交的SQL應該是一起成功/失敗的，而且這裏也有利用異常捕獲機制來實現RollBack，那麽在使用時到底是會出現什麽情況呢?<br />   (0) SQL1,SQL2,...SQLn其中有語句含有非執行時的語法錯誤，比如說嵌套語句少掉一個右括號等；<br />      <strong><font color="#0000ff">OK</font></strong>。這種情況下，可以發現執行結果如預期，SQL1 -&gt; SQLn都不會被執行，且會提示錯誤訊息。<br />   (1) SQL1,SQL2,...SQLn都執行成功；<br />      <font color="#0000ff"><strong>OK</strong></font>。這種情況下，可以發現執行結果如預期，SQL1 -&gt; SQLn都會被執行，可以正確提交事務。<br />   (2) SQL1,SQL2,...SQLn其中有語句會執行失敗；<br />      (2.1) SQL1可以執行，而SQL2會發生執行時期錯誤(比如UPDATE上去欄位超過寬度)；<br />         <strong><font color="#ff0000">NO</font></strong>。Execute執行成功，查詢結果會發現SQL1的結果有提交，而SQL2以及之後的SQL都沒有執行。<br />      (2.2) SQL1會發生執行時期錯誤(比如UPDATE上去欄位超過寬度)，而SQL2及其後的SQL可以被執行；<br />         <font color="#0000ff"><strong>OK</strong></font>。這種情況下，可以發現有提示錯誤訊息，事務會被RollBack，SQL1 -&gt; SQLn都不會被執行。<br /><br />   通過上面的幾種情況可以得到結論，如果是以一整個字串(中間用換行或分號分隔)來提交多筆SQL命令時，在所有SQL語法檢查OK後，執行的成功與否取決於第1筆SQL的執行狀態，因此就是說<strong>如果第一筆SQL執行OK，那麽該事務就一定會被Commit</strong>。<br /><br />   所以說，在使用事務提交多筆SQL時不能使用上面的提交方式，而必須逐筆的提交才行。<br /><br /></font>
<img src ="http://www.blogjava.net/tw-ddm/aggbug/255166.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/tw-ddm/" target="_blank">大大毛</a> 2009-02-17 18:50 <a href="http://www.blogjava.net/tw-ddm/articles/255166.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>VB程式分工及內部架構</title><link>http://www.blogjava.net/tw-ddm/articles/255162.html</link><dc:creator>大大毛</dc:creator><author>大大毛</author><pubDate>Tue, 17 Feb 2009 09:52:00 GMT</pubDate><guid>http://www.blogjava.net/tw-ddm/articles/255162.html</guid><wfw:comment>http://www.blogjava.net/tw-ddm/comments/255162.html</wfw:comment><comments>http://www.blogjava.net/tw-ddm/articles/255162.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/tw-ddm/comments/commentRss/255162.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/tw-ddm/services/trackbacks/255162.html</trackback:ping><description><![CDATA[
		<br />
		<font face="Georgia" size="2">VB程式的結構比較自由，通常做法就是新建一個工程，然後建幾個FORM畫面並在其中放上代碼就OK，但是當一個工程的結構略顯複雜的時候就會造成整個程式結構上的沉重感，修改及添加功能時會造成力不從心的感覺，如果能夠維持一個比較簡潔的架構，程式就會在可讀性及靈活性上得到極大的提高，下面就如何定義並維持這樣的架構做一個簡單地講解。<br /><br />1. 利用不同類型的工程文件來實現分割：<br />   做過VB的都應該知道，常用的工程文件有FORM，MODULES，CLASS以及USERCONTROLS這四種，應該如何去使用呢？<br />   (1) Forms(窗體)<br />      Form是最常見的文件，利用它可以用來設計功能畫面。因此在劃分上應該將全部與實際功能相關的CODE都放在這裏。<br />   (2) Modules(模組)<br />      這個是純粹的代碼塊文件，由於它本身的PUBLIC特性，因此應該是放置公用函式。這裏所說的“公用函式”並不是說裏面放的都是一些PUBLIC的FUN/SUB，而是說從功能上來講是與整個Project相關的處理，比如說DB連線，讀寫配置檔，寫LOG等。而且裏面的FUN/SUB應該是PRIVTE與PUBLIC相互的一個結合(Private應該是對多個Public的一個實現上的抽象)。<br />   (3) Class(類)<br />      Class因為可以被單獨的NEW，因此它承擔的角色應該是對一個單獨流程的抽象，例如將讀寫EXCEL/發MAIL設計成一個類。<br />   (4) UserControls(用戶控件)<br />      這個東東我的理解是Class的擴充，當需要利用到現成控件進行擴充以及事件響應時就用它好了。<br /><br />2.  工程內部分工：<br />   (1) 要養成從Module.Main來做工程入口/出口的習慣，避免直接將某個Form設為啟動窗體和任意地方的End。<br />      用Module.Main做工程入口可以在固定的地方實現入口時的參數初始化及一及設定，避免出現設Form為啟動窗體後在添加對等功能塊時難於修改及初始化的問題。例如需求1先是將Form1設為啟動畫面，而後續需求要求設Form2為啟動畫面或是要將整個EXE做成無須顯示畫面直接運行等。<br />      在Module放置整個工程的出口(指結束時的處理Sub)，能夠保證工程中引用的釋放。<br />   (2) 將全部的外部引用及定義都放在Module中。<br />      例如VB程式中經常會用到WindowsAPI，那麽這些API的定義應該是放到Module之中(UserControls中用到除外)<br /><br />3.  單獨模塊(指單一Form/Module/Class)內結構：<br />   (1) 一定要使用Option Explicit來實施強制定義檢查，完全避免未定義就使用變量這種情況的發生。<br />   (2) Public的節制使用，能夠定義為Private的方法就一定不要使用Public，避免出現引用混亂及閱讀困難。<br />         <br />4.  方法內結構：<br />   (1) 異常處理<br /><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"><img id="Code_Closed_Image_171038" onclick="this.style.display='none'; Code_Closed_Text_171038.style.display='none'; Code_Open_Image_171038.style.display='inline'; Code_Open_Text_171038.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top" /><img id="Code_Open_Image_171038" style="DISPLAY: none" onclick="this.style.display='none'; Code_Open_Text_171038.style.display='none'; Code_Closed_Image_171038.style.display='inline'; Code_Closed_Text_171038.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top" /><span id="Code_Closed_Text_171038" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">方法內結構示例</span><span id="Code_Open_Text_171038" style="DISPLAY: none"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="COLOR: #008080"> 1</span> <span style="COLOR: #0000ff">Public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Function</span><span style="COLOR: #000000"> ReadTemplate(<strong>ByVal</strong> strTemplatePath </span><span style="COLOR: #0000ff">As</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">String</span><span style="COLOR: #000000">, <strong>ByRef</strong> strTemplateContent </span><span style="COLOR: #0000ff">As</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">String</span><span style="COLOR: #000000">) </span><span style="COLOR: #0000ff">As</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Boolean</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 2</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">On</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Error</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">GoTo</span><span style="COLOR: #000000"> ErrHandle<br /></span><span style="COLOR: #008080"> 3</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">Dim</span><span style="COLOR: #000000"> lngFileHandle </span><span style="COLOR: #0000ff">As</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Integer</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 4</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">Dim</span><span style="COLOR: #000000"> strLine </span><span style="COLOR: #0000ff">As</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">String</span><span style="COLOR: #000000">, strContent </span><span style="COLOR: #0000ff">As</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">String</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 5</span> <span style="COLOR: #000000">    lngFileHandle </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 6</span> <span style="COLOR: #000000">    </span><span style="COLOR: #008000">'</span><span style="COLOR: #008000">檢查模板文件是否存在</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080"> 7</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">If</span><span style="COLOR: #000000"> Dir(strTemplatePath) </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">""</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Then</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 8</span> <span style="COLOR: #000000">        Err.Raise </span><span style="COLOR: #000000">32001</span><span style="COLOR: #000000">, </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">ReadTemplate</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">, </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">模板文件不存在</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 9</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">End</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">If</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">10</span> <span style="COLOR: #000000">    </span><span style="COLOR: #008000">'</span><span style="COLOR: #008000">載入模板文件內容</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">11</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">    strContent </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">""</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">12</span> <span style="COLOR: #000000">    lngFileHandle </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> FreeFile()<br /></span><span style="COLOR: #008080">13</span> <span style="COLOR: #000000">    Open strTemplatePath </span><span style="COLOR: #0000ff">For</span><span style="COLOR: #000000"> Input </span><span style="COLOR: #0000ff">As</span><span style="COLOR: #000000"> #lngFileHandle<br /></span><span style="COLOR: #008080">14</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">Do</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">While</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Not</span><span style="COLOR: #000000"> EOF(lngFileHandle)<br /></span><span style="COLOR: #008080">15</span> <span style="COLOR: #000000">        Line Input #lngFileHandle, strLine<br /></span><span style="COLOR: #008080">16</span> <span style="COLOR: #000000">        strContent </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> strContent </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> strLine </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> vbCrLf<br /></span><span style="COLOR: #008080">17</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">Loop</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">18</span> <span style="COLOR: #000000">    strTemplateContent </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> strContent<br /></span><span style="COLOR: #008080">19</span> <span style="COLOR: #000000">    ReadTemplate </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">True</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">20</span> <span style="COLOR: #000000">ExitHandle:<br /></span><span style="COLOR: #008080">21</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">If</span><span style="COLOR: #000000"> lngFileHandle </span><span style="COLOR: #000000">&lt;&gt;</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Then</span><span style="COLOR: #000000"> Close #lngFileHandle<br /></span><span style="COLOR: #008080">22</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">Exit</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Function</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">23</span> <span style="COLOR: #000000">ErrHandle:<br /></span><span style="COLOR: #008080">24</span> <span style="COLOR: #000000">    ReadTemplate </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">False</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">25</span> <span style="COLOR: #000000">    WriteErrLog gStrAppLogPath, TheMdlName, Err.Source, </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">加載模板[</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> strTemplatePath </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">]失敗</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">, Err.Number, Err.Description<br /></span><span style="COLOR: #008080">26</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">GoTo</span><span style="COLOR: #000000"> ExitHandle<br /></span><span style="COLOR: #008080">27</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">End Function</span></span></div>   可以看到上面的結構包含了異常處理，在VB程式中最容易出現的問題就是異常的處理，由於VB並不會象JAVA/NET那樣要求強制處理異常，因此常常都會忽略掉程式中的異常處理問題，造成一旦出錯就會整個OVER的嚴重後果。<br />   (2) 不要忽視FUN的返回<br />      VB中Function/Sub都可以一樣直接使用(例如 Call Sub1/Function1...)，在這樣隨意使用Function的同時，其返回值必定會被丟棄，極容易造成程式處理的邏輯分支被忽略。<br />   (3) Sub/Function中參數要顯式聲明ByVal / ByRef 及參數形態<br />      對於簡單類型的參數來講，顯式聲明ByVal / ByRef 是表明該參數是否可以被修改，而聲明參數形態則可以從一定程度上避免VB自動轉型時的錯誤。<br /></font>
<img src ="http://www.blogjava.net/tw-ddm/aggbug/255162.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/tw-ddm/" target="_blank">大大毛</a> 2009-02-17 17:52 <a href="http://www.blogjava.net/tw-ddm/articles/255162.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>利用TN5250NF下載檔案的自動化處理</title><link>http://www.blogjava.net/tw-ddm/articles/254262.html</link><dc:creator>大大毛</dc:creator><author>大大毛</author><pubDate>Wed, 11 Feb 2009 07:51:00 GMT</pubDate><guid>http://www.blogjava.net/tw-ddm/articles/254262.html</guid><wfw:comment>http://www.blogjava.net/tw-ddm/comments/254262.html</wfw:comment><comments>http://www.blogjava.net/tw-ddm/articles/254262.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/tw-ddm/comments/commentRss/254262.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/tw-ddm/services/trackbacks/254262.html</trackback:ping><description><![CDATA[
		<p>
				<font size="2">
						<font face="Georgia">
								<strong>問題</strong> <br /><br />   現在有遇到一個需求，是想直接利用TN5250來實現400端資料的下載：<br />   1. 僅通過TN5250來完成，不要使用其他的元件；<br />   2. 匯出資料格式為TXT檔案，應該是要能夠放到局域網路上；<br />   3. 匯出資料的動作需要做成排程方式(例如每天匯出)；<br />   4. 需求相當的急。<br /><br />   使用TN5250NF可以直接抓400端資料到PC端，匯出成TXT/XLS等格式的文件。匯出功能實際由TN4002PC來完成，該功能使用起來非常方便。通過簡單設定連線/設定參數即可實現資料的下載。<br /><br />   如果不考慮到需要做成排程方式來自動進行，則完全可以以人工方式來實現1-2。<br /><br /><br /><strong>思考</strong><br /><br />   要做直接利用程式的功能來實現自動化，首先應先弄清楚作業的方式是怎樣，先來看看手工是怎樣下載的。<br />   1. 點選TN5250上的菜單“傳送”-&gt; “從主電腦接收檔案”即可打開功能畫面(<strong>圖1</strong>)：<br /><br /><img title="TN4002PC功能畫面" height="462" alt="o_TN4002PC_01.GIF" src="http://www.blogjava.net/images/blogjava_net/tw-ddm/10051/o_TN4002PC_01.GIF" width="486" border="0" /><br /><br />   2. 需要配置參數(<strong>圖2</strong>)：<br /><img title="TN4002PC設定畫面" height="462" alt="o_TN4002PC_02.GIF" src="http://www.blogjava.net/images/blogjava_net/tw-ddm/10051/o_TN4002PC_02.GIF" width="486" border="0" /><br />   &gt;&gt; 注意紅色框部分，輸出必須要設為“檔案”，而筆數=0則表示將該檔案中全部資料都DOWN，&gt;0則表示最大下載筆數(如設為20則僅下載20筆資料)。其他部分根據需要自行配置。<br />   &gt;&gt; 檔案名稱處定義需要存放匯出檔案的名稱及路徑，如果需要在網路路徑上匯出文檔則必須先將網絡路徑映射為本地磁盤(通過“我的電腦”上右鍵“連線網路磁碟機”即可將一個網絡路徑映射成本地盤符)，然後再使用，否則會提示錯誤。<br /><br />   3. 點選上圖的“開始接收(R)”即可以開始從400下載資料到PC，接收完成後會跳出訊息提示框(<strong>圖3</strong>)：<br /><img title="TN4002PC作業訊息" height="121" alt="o_TN4002PC_03.GIF" src="http://www.blogjava.net/images/blogjava_net/tw-ddm/10051/o_TN4002PC_03.GIF" width="236" border="0" /><br /><br />   另外，還可以利用圖2上的“檔案”菜單來實現對參數配置的保存以及載入(Default是存成.tt0檔案)。<br /></font>
						<br />
				</font>
				<font face="Georgia" size="2">   通過查看保存配置後生成的快捷方式，可以發現其快捷方式內容實際為(<strong>圖4</strong>)：<br /><img title="TN4002PC作業快捷方式" height="473" alt="o_TN4002PC_04.GIF" src="http://www.blogjava.net/images/blogjava_net/tw-ddm/10051/o_TN4002PC_04.GIF" width="367" border="0" /><br />   &gt;&gt; 紅色部分即為我保存的配置檔--配置檔實際是一個INI文件，可使用記事本打開<br /><br />   因此，使用“TN4002PC.EXE /f=配置檔名稱”這樣的形式即可從命令行實際對此功能的調用(先要將參數都配置好存在配置檔中)。OK，現在剩下就是要將這個命令行能夠被放在“計劃任務”中。<br /><br />   通過手工測試可以發現有一些問題還待解決：<br /><strong><font color="#ff0000">   1. 在匯出完成後一定會有訊息框跳出(圖3)，這個是相當於MSGBOX的窗口，它會將整個程式阻塞在那裏，如果不將它消掉(回車或空格)就會造成多次處理出錯。<br />   2. 配置參數時無法為匯出的TXT文檔指定一個動態名稱，因為是要自動將匯出資料放在一個固定路徑下，所以每回匯出的文檔名稱都應該有所不同，例如保存成帶日期格式。</font></strong><br /><br /><br /><strong>解決方法</strong><br /><br />   因為是需要動態生成匯出文檔名以及消掉作業完成後的MSGBOX訊息框，所以是必須要有程序來協助解決，程序使用VBS來實現(先前有想到用BAT，但是無法解決MSGBOX的問題)。<br />   程式的處理方法：<br />   1. 利用VBS來動態生成TTO配置文件，只要每次修改掉保存的文件名稱就好，這樣就可以每次生成的TXT都有所不同。<br />         解析保存配置的TTO檔案，提取PC_FILENAME設定，在文件名後加上處理的日期和時間(路徑/文件類型保持不變)，最後再存成一個新的TTO配置檔。<br />   2. 利用VBS來執行CMD命令(命令格式見圖4)，完成對匯出功能的調用。<br />         為TN4002PC指定新生成的TTO配置檔，實現功能的調用。<br />   3. 在匯出完成後發送鍵盤指令關掉MSGBOX框。<br />         因為VBS中看似沒法能夠直接獲取TN4002PC的窗口，所以是添加處理--在程式處理過程中每間隔一段時間就去檢查生成的TXT檔案大小是否有變化，如果沒有變化則認定可能已匯出完成，卡在MSGBOX窗口，VBS程式就要激活MSGBOX窗口並發送鍵盤的空格。<br />   4. 在“計劃任務”中設置對VBS程序的調用。<br />         利用計劃任務精靈可以完成對VBS程序的調用，因為我這裏設計是要將配置的TTO文件名做為參數傳給VBS程序。而XP下試過幾次都無法能在計劃任務中直接將參數傳遞過去(2003卻可以)，所以就只有多做一只BAT(放置對VBS程式的傳參調用)放在計劃任務中。<br /><br /><strong>BAT批處理程序</strong>(藍色即為VBS程序，紅色為保存的TTO配置檔)</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">
				<span style="COLOR: #000000">
						<font face="Georgia">
								<font size="2">start <font color="#0000ff">RunVBS.vbs</font> <font color="#ff0000">C:\FGPPOLR.tto</font></font>
						</font>
				</span>
		</div>
		<p>
				<strong>
						<font face="Georgia" size="2">VBS程序</font>
				</strong>
		</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="Georgia">
						<font size="2">
								<img id="Code_Closed_Image_153821" onclick="this.style.display='none'; Code_Closed_Text_153821.style.display='none'; Code_Open_Image_153821.style.display='inline'; Code_Open_Text_153821.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top" />
								<img id="Code_Open_Image_153821" style="DISPLAY: none" onclick="this.style.display='none'; Code_Open_Text_153821.style.display='none'; Code_Closed_Image_153821.style.display='inline'; Code_Closed_Text_153821.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top" />
								<span id="Code_Closed_Text_153821" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">VBS程序</span>
						</font>
				</font>
				<span id="Code_Open_Text_153821" style="DISPLAY: none">
						<br />
						<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
						<font face="Georgia">
								<font size="2">
										<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
										<span style="COLOR: #0000ff">Dim</span>
								</font>
						</font>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #000000"> ws,fso<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
										<span style="COLOR: #0000ff">Dim</span>
								</font>
						</font>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #000000"> ttoFile,tmpFile,sTmpfileName,sTxtfileName<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
										<span style="COLOR: #0000ff">Dim</span>
								</font>
						</font>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #000000"> line,sDate<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
										<span style="COLOR: #008000">'</span>
										<span style="COLOR: #008000">當前的日期/時間</span>
								</font>
						</font>
						<span style="COLOR: #008000">
								<br />
								<font face="Georgia" size="2">
										<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
								</font>
						</span>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #000000">sDate </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Year</span>
										<span style="COLOR: #000000">(</span>
										<span style="COLOR: #0000ff">Now</span>
										<span style="COLOR: #000000">()) </span>
										<span style="COLOR: #000000">&amp;</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Right</span>
										<span style="COLOR: #000000">(</span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000">0</span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000">&amp;</span>
										<span style="COLOR: #0000ff">Month</span>
										<span style="COLOR: #000000">(</span>
										<span style="COLOR: #0000ff">Now</span>
										<span style="COLOR: #000000">()),</span>
										<span style="COLOR: #000000">2</span>
										<span style="COLOR: #000000">) </span>
										<span style="COLOR: #000000">&amp;</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Right</span>
										<span style="COLOR: #000000">(</span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000">0</span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000">&amp;</span>
										<span style="COLOR: #0000ff">Day</span>
										<span style="COLOR: #000000">(</span>
										<span style="COLOR: #0000ff">Now</span>
										<span style="COLOR: #000000">()),</span>
										<span style="COLOR: #000000">2</span>
										<span style="COLOR: #000000">) </span>
										<span style="COLOR: #000000">&amp;</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Right</span>
										<span style="COLOR: #000000">(</span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000">0</span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">&amp;</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Hour</span>
										<span style="COLOR: #000000">(</span>
										<span style="COLOR: #0000ff">Now</span>
										<span style="COLOR: #000000">()),</span>
										<span style="COLOR: #000000">2</span>
										<span style="COLOR: #000000">) </span>
										<span style="COLOR: #000000">&amp;</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Right</span>
										<span style="COLOR: #000000">(</span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000">0</span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">&amp;</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Minute</span>
										<span style="COLOR: #000000">(</span>
										<span style="COLOR: #0000ff">Now</span>
										<span style="COLOR: #000000">()),</span>
										<span style="COLOR: #000000">2</span>
										<span style="COLOR: #000000">) </span>
										<span style="COLOR: #000000">&amp;</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Right</span>
										<span style="COLOR: #000000">(</span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000">0</span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">&amp;</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Second</span>
										<span style="COLOR: #000000">(</span>
										<span style="COLOR: #0000ff">Now</span>
										<span style="COLOR: #000000">()),</span>
										<span style="COLOR: #000000">2</span>
								</font>
						</font>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #000000">)<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
										<span style="COLOR: #0000ff">Set</span>
										<span style="COLOR: #000000"> ws </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> WScript.CreateObject(</span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000">WScript.Shell</span>
										<span style="COLOR: #000000">"</span>
								</font>
						</font>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #000000">)<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
										<span style="COLOR: #0000ff">Set</span>
										<span style="COLOR: #000000"> fso </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">CreateObject</span>
										<span style="COLOR: #000000">(</span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000">Scripting.FileSystemObject</span>
										<span style="COLOR: #000000">"</span>
								</font>
						</font>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #000000">)<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
										<span style="COLOR: #008000">'</span>
										<span style="COLOR: #008000">新生成TTO配置檔,放在系統TEMP路徑中</span>
								</font>
						</font>
						<span style="COLOR: #008000">
								<br />
								<font face="Georgia" size="2">
										<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
								</font>
						</span>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #000000">sTmpfileName </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> fso.getspecialfolder(</span>
										<span style="COLOR: #000000">2</span>
										<span style="COLOR: #000000">) </span>
										<span style="COLOR: #000000">&amp;</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000">\</span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">&amp;</span>
										<span style="COLOR: #000000"> sDate </span>
										<span style="COLOR: #000000">&amp;</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000">.tto</span>
										<span style="COLOR: #000000">"</span>
								</font>
						</font>
						<span style="COLOR: #000000">
								<br />
								<font face="Georgia" size="2">
										<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
								</font>
						</span>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #0000ff">If</span>
										<span style="COLOR: #000000"> WScript.Arguments.Count</span>
										<span style="COLOR: #000000">&gt;</span>
										<span style="COLOR: #000000">0</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Then</span>
								</font>
						</font>
						<span style="COLOR: #000000">
								<br />
								<font face="Georgia" size="2">
										<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </font>
						</span>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #0000ff">Set</span>
										<span style="COLOR: #000000"> ttoFile </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> fso.openTextFile(WScript.Arguments(</span>
										<span style="COLOR: #000000">0</span>
										<span style="COLOR: #000000">),</span>
										<span style="COLOR: #000000">1</span>
										<span style="COLOR: #000000">,</span>
										<span style="COLOR: #0000ff">False</span>
								</font>
						</font>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #000000">)<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span>
										<span style="COLOR: #0000ff">Set</span>
										<span style="COLOR: #000000"> tmpFile </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> fso.openTextFile(sTmpfileName,</span>
										<span style="COLOR: #000000">2</span>
										<span style="COLOR: #000000">,</span>
										<span style="COLOR: #0000ff">True</span>
								</font>
						</font>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #000000">)<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span>
										<span style="COLOR: #0000ff">While</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Not</span>
								</font>
						</font>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #000000"> ttoFile.AtEndOfStream<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        line </span>
										<span style="COLOR: #000000">=</span>
								</font>
						</font>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #000000"> ttoFile.ReadLine()<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span>
										<span style="COLOR: #008000">'</span>
										<span style="COLOR: #008000">查找到PC_FILENAME參數設定,並在文件名上加入日期/時間</span>
								</font>
						</font>
						<span style="COLOR: #008000">
								<br />
								<font face="Georgia" size="2">
										<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
								</font>
						</span>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #000000">        </span>
										<span style="COLOR: #0000ff">If</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">InStr</span>
										<span style="COLOR: #000000">(</span>
										<span style="COLOR: #000000">1</span>
										<span style="COLOR: #000000">,line,</span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000">PC_FILENAME=</span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000">)</span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000">1</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Then</span>
								</font>
						</font>
						<span style="COLOR: #000000">
								<br />
								<font face="Georgia" size="2">
										<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />            </font>
						</span>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #0000ff">If</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">InstrRev</span>
										<span style="COLOR: #000000">(line,</span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000">.</span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000">)</span>
										<span style="COLOR: #000000">&gt;</span>
										<span style="COLOR: #000000">0</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Then</span>
								</font>
						</font>
						<span style="COLOR: #000000">
								<br />
								<font face="Georgia" size="2">
										<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />                line </font>
						</span>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Mid</span>
										<span style="COLOR: #000000">(line,</span>
										<span style="COLOR: #000000">1</span>
										<span style="COLOR: #000000">,</span>
										<span style="COLOR: #0000ff">InstrRev</span>
										<span style="COLOR: #000000">(line,</span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000">.</span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000">)</span>
										<span style="COLOR: #000000">-</span>
										<span style="COLOR: #000000">1</span>
										<span style="COLOR: #000000">) </span>
										<span style="COLOR: #000000">&amp;</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000">_</span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">&amp;</span>
										<span style="COLOR: #000000"> sDate </span>
										<span style="COLOR: #000000">&amp;</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Right</span>
										<span style="COLOR: #000000">(line,</span>
										<span style="COLOR: #0000ff">Len</span>
										<span style="COLOR: #000000">(line)</span>
										<span style="COLOR: #000000">-</span>
										<span style="COLOR: #0000ff">InstrRev</span>
										<span style="COLOR: #000000">(line,</span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000">.</span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000">)</span>
										<span style="COLOR: #000000">+</span>
										<span style="COLOR: #000000">1</span>
								</font>
						</font>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #000000">)<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />            </span>
										<span style="COLOR: #0000ff">Else</span>
								</font>
						</font>
						<span style="COLOR: #000000">
								<br />
								<font face="Georgia" size="2">
										<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />                line </font>
						</span>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> line </span>
										<span style="COLOR: #000000">&amp;</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000">_</span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">&amp;</span>
								</font>
						</font>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #000000"> sDate<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />            </span>
										<span style="COLOR: #0000ff">End</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">If</span>
								</font>
						</font>
						<span style="COLOR: #000000">
								<br />
								<font face="Georgia" size="2">
										<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />            sTxtfileName </font>
						</span>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Mid</span>
										<span style="COLOR: #000000">(line,</span>
										<span style="COLOR: #000000">13</span>
								</font>
						</font>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #000000">)<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span>
										<span style="COLOR: #0000ff">End</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">If</span>
								</font>
						</font>
						<span style="COLOR: #000000">
								<br />
								<font face="Georgia" size="2">
										<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        tmpFile.WriteLine(line)<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </font>
						</span>
						<span style="COLOR: #0000ff">
								<font face="Georgia" size="2">Wend</font>
						</span>
						<span style="COLOR: #000000">
								<br />
								<font face="Georgia" size="2">
										<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    tmpFile.close<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    ttoFile.close<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </font>
						</span>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #0000ff">If</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">True</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Then</span>
								</font>
						</font>
						<span style="COLOR: #000000">
								<br />
								<font face="Georgia" size="2">
										<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </font>
						</span>
						<span style="COLOR: #0000ff">
								<font face="Georgia" size="2">Dim</font>
						</span>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #000000"> oExec,lastSize,newSize,blnFlag1<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    blnFlag1 </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">False</span>
								</font>
						</font>
						<span style="COLOR: #000000">
								<br />
								<font face="Georgia" size="2">
										<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    lastSize </font>
						</span>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">-</span>
										<span style="COLOR: #000000">2</span>
								</font>
						</font>
						<span style="COLOR: #000000">
								<br />
								<font face="Georgia" size="2">
										<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </font>
						</span>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #008000">'</span>
										<span style="COLOR: #008000">調用TN4002PC來完成匯出操作</span>
								</font>
						</font>
						<span style="COLOR: #008000">
								<br />
								<font face="Georgia" size="2">
										<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
								</font>
						</span>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #000000">    </span>
										<span style="COLOR: #0000ff">Set</span>
										<span style="COLOR: #000000"> oExec </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> ws.exec(</span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000">C:\TN5250NF\TN4002PC.EXE /f=</span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">&amp;</span>
								</font>
						</font>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #000000"> sTmpfileName)<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span>
										<span style="COLOR: #0000ff">Do</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">While</span>
										<span style="COLOR: #000000"> oExec.Status </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">0</span>
								</font>
						</font>
						<span style="COLOR: #000000">
								<br />
								<font face="Georgia" size="2">
										<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        WScript.Sleep </font>
						</span>
						<span style="COLOR: #000000">
								<font face="Georgia" size="2">5000</font>
						</span>
						<span style="COLOR: #000000">
								<br />
								<font face="Georgia" size="2">
										<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </font>
						</span>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #0000ff">If</span>
										<span style="COLOR: #000000"> blnFlag1 </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">False</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Then</span>
								</font>
						</font>
						<span style="COLOR: #000000">
								<br />
								<font face="Georgia" size="2">
										<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />            </font>
						</span>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #0000ff">If</span>
										<span style="COLOR: #000000"> fso.FileExists(sTxtfileName) </span>
										<span style="COLOR: #0000ff">Then</span>
								</font>
						</font>
						<span style="COLOR: #000000">
								<br />
								<font face="Georgia" size="2">
										<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />                newSize </font>
						</span>
						<span style="COLOR: #000000">
								<font face="Georgia" size="2">=</font>
						</span>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #000000"> fso.GetFile(sTxtfileName).Size<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />                </span>
										<span style="COLOR: #0000ff">If</span>
										<span style="COLOR: #000000"> newSize </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> lastSize </span>
										<span style="COLOR: #0000ff">Then</span>
								</font>
						</font>
						<span style="COLOR: #000000">
								<br />
								<font face="Georgia" size="2">
										<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />                    blnFlag1 </font>
						</span>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">True</span>
								</font>
						</font>
						<span style="COLOR: #000000">
								<br />
								<font face="Georgia" size="2">
										<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />                </font>
						</span>
						<span style="COLOR: #0000ff">
								<font face="Georgia" size="2">Else</font>
						</span>
						<span style="COLOR: #000000">
								<br />
								<font face="Georgia" size="2">
										<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />                    lastSize </font>
						</span>
						<span style="COLOR: #000000">
								<font face="Georgia" size="2">=</font>
						</span>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #000000"> newSize<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />                </span>
										<span style="COLOR: #0000ff">End</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">If</span>
								</font>
						</font>
						<span style="COLOR: #000000">
								<br />
								<font face="Georgia" size="2">
										<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />            </font>
						</span>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #0000ff">End</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">If</span>
								</font>
						</font>
						<span style="COLOR: #000000">
								<br />
								<font face="Georgia" size="2">
										<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </font>
						</span>
						<span style="COLOR: #0000ff">
								<font face="Georgia" size="2">Else</font>
						</span>
						<span style="COLOR: #000000">
								<br />
								<font face="Georgia" size="2">
										<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />            </font>
						</span>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #008000">'</span>
										<span style="COLOR: #008000">如果一段時間內匯出檔案大小無變化,則激活窗口並發送空格</span>
								</font>
						</font>
						<span style="COLOR: #008000">
								<br />
								<font face="Georgia" size="2">
										<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
								</font>
						</span>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #000000">            ws.AppActivate </span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000">TN4002PC</span>
										<span style="COLOR: #000000">"</span>
								</font>
						</font>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #000000"> <br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />            ws.SendKeys </span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #000000">"</span>
								</font>
						</font>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #000000">  <br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span>
										<span style="COLOR: #0000ff">End</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">If</span>
								</font>
						</font>
						<span style="COLOR: #000000">
								<br />
								<font face="Georgia" size="2">
										<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </font>
						</span>
						<span style="COLOR: #0000ff">
								<font face="Georgia" size="2">Loop</font>
						</span>
						<span style="COLOR: #000000">
								<br />
								<font face="Georgia" size="2">
										<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </font>
						</span>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #0000ff">End</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">If</span>
								</font>
						</font>
						<span style="COLOR: #000000">
								<br />
								<font face="Georgia" size="2">
										<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
								</font>
						</span>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #0000ff">End</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">If</span>
								</font>
						</font>
						<span style="COLOR: #000000">
								<br />
								<font face="Georgia" size="2">
										<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
								</font>
						</span>
						<font face="Georgia">
								<font size="2">
										<span style="COLOR: #0000ff">Set</span>
										<span style="COLOR: #000000"> ws </span>
										<span style="COLOR: #000000">=</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Nothing</span>
								</font>
						</font>
						<span style="COLOR: #000000">
								<font face="Georgia" size="2">  <br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />WScript.quit</font>
						</span>
				</span>
		</div>
		<p>
				<font face="Georgia" size="2">排程執行完後(每次都會有FDF--欄位定義和TXT--檔案內容兩個文件生成，多次RUN出文件名會不同):<br /><img title="執行完成" height="494" alt="o_RUNED.GIF" src="http://www.blogjava.net/images/blogjava_net/tw-ddm/10051/o_RUNED.GIF" width="713" border="0" /></font>
		</p>
		<p>
				<strong>
						<font face="Georgia" size="2">
								<br />下載</font>
				</strong>
				<br />      <font face="Georgia" size="2"><a href="/Files/tw-ddm/RUNBAT_VBS.zip">VBS文件</a></font></p>
<img src ="http://www.blogjava.net/tw-ddm/aggbug/254262.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/tw-ddm/" target="_blank">大大毛</a> 2009-02-11 15:51 <a href="http://www.blogjava.net/tw-ddm/articles/254262.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>大資料量SQL性能優化之轉換思路</title><link>http://www.blogjava.net/tw-ddm/articles/239935.html</link><dc:creator>大大毛</dc:creator><author>大大毛</author><pubDate>Tue, 11 Nov 2008 10:55:00 GMT</pubDate><guid>http://www.blogjava.net/tw-ddm/articles/239935.html</guid><wfw:comment>http://www.blogjava.net/tw-ddm/comments/239935.html</wfw:comment><comments>http://www.blogjava.net/tw-ddm/articles/239935.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/tw-ddm/comments/commentRss/239935.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/tw-ddm/services/trackbacks/239935.html</trackback:ping><description><![CDATA[
		<p dir="ltr" style="MARGIN-RIGHT: 0px">
				<font face="Georgia">
						<font size="2">
								<strong>問題</strong> <br /><br />      這一次有同事在做一個小CASE時遇到問題無法解決. 整個的需求比較簡單, 是需要從資料庫中撈資料出來進行列印, 需求如下:<br />      1. 從tbMain中找符合條件的客戶.限定條件:只有一筆資料的客戶<br />      2. 查詢tbForeclose, 剔除掉某些客戶.剔除條件:姓名存在於tbForeclose中的客戶即要從名單中剔除<br />      3. 從tbDetail中撈客戶的明細資料出來進行列印.關聯條件:依tbMain中客戶ID直接帶出即可<br /></font>
				</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">
				<span style="COLOR: #008080">--</span>
				<span style="COLOR: #008080">主檔(同一客戶會有多筆資料)</span>
				<span style="COLOR: #008080">
						<br />
				</span>
				<span style="COLOR: #0000ff">Create</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">Table</span>
				<span style="COLOR: #000000"> tbMain (<br />    aID </span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">int</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #ff00ff">identity</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">),<br />    pID </span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">varchar</span>
				<span style="COLOR: #000000">(</span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">10</span>
				<span style="COLOR: #000000">) </span>
				<span style="COLOR: #808080">not</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">null</span>
				<span style="COLOR: #000000">,    </span>
				<span style="COLOR: #008080">--</span>
				<span style="COLOR: #008080">客戶ID</span>
				<span style="COLOR: #008080">
						<br />
				</span>
				<span style="COLOR: #000000">    pName </span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">nvarchar</span>
				<span style="COLOR: #000000">(</span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">20</span>
				<span style="COLOR: #000000">) </span>
				<span style="COLOR: #808080">not</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">null</span>
				<span style="COLOR: #000000">,    </span>
				<span style="COLOR: #008080">--</span>
				<span style="COLOR: #008080">客戶姓名</span>
				<span style="COLOR: #008080">
						<br />
				</span>
				<span style="COLOR: #000000">    pDoing </span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">varchar</span>
				<span style="COLOR: #000000">(</span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">20</span>
				<span style="COLOR: #000000">) </span>
				<span style="COLOR: #808080">not</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">null</span>
				<span style="COLOR: #000000">
						<br />);<br /></span>
				<span style="COLOR: #008080">--</span>
				<span style="COLOR: #008080">剔除客戶檔(僅存放客戶姓名)</span>
				<span style="COLOR: #008080">
						<br />
				</span>
				<span style="COLOR: #0000ff">Create</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">Table</span>
				<span style="COLOR: #000000"> tbForeclose (<br />    pName </span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">nvarchar</span>
				<span style="COLOR: #000000">(</span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">20</span>
				<span style="COLOR: #000000">) </span>
				<span style="COLOR: #808080">not</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">null</span>
				<span style="COLOR: #000000">    </span>
				<span style="COLOR: #008080">--</span>
				<span style="COLOR: #008080">剔除客戶姓名</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: #0000ff">Create</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">Table</span>
				<span style="COLOR: #000000"> tbDetail (<br />    pID </span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">varchar</span>
				<span style="COLOR: #000000">(</span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">10</span>
				<span style="COLOR: #000000">) </span>
				<span style="COLOR: #0000ff">primary</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">key</span>
				<span style="COLOR: #000000">,    </span>
				<span style="COLOR: #008080">--</span>
				<span style="COLOR: #008080">客戶ID(KEY)</span>
				<span style="COLOR: #008080">
						<br />
				</span>
				<span style="COLOR: #000000">    pSex </span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">char</span>
				<span style="COLOR: #000000">(</span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
				<span style="COLOR: #000000">) </span>
				<span style="COLOR: #808080">not</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">null</span>
				<span style="COLOR: #000000">        </span>
				<span style="COLOR: #008080">--</span>
				<span style="COLOR: #008080">客戶其他詳細資料<img src="http://www.blogjava.net/images/dot.gif" /></span>
				<span style="COLOR: #008080">
						<br />
				</span>
				<span style="COLOR: #000000">);</span>
		</div>
		<p dir="ltr" style="MARGIN-RIGHT: 0px"> </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">
				<img id="Code_Closed_Image_183100" onclick="this.style.display='none'; Code_Closed_Text_183100.style.display='none'; Code_Open_Image_183100.style.display='inline'; Code_Open_Text_183100.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top" />
				<img id="Code_Open_Image_183100" style="DISPLAY: none" onclick="this.style.display='none'; Code_Open_Text_183100.style.display='none'; Code_Closed_Image_183100.style.display='inline'; Code_Closed_Text_183100.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top" />
				<span id="Code_Closed_Text_183100" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">示例數據</span>
				<span id="Code_Open_Text_183100" style="DISPLAY: none">
						<br />
						<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
						<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
						<span style="COLOR: #0000ff">SET</span>
						<span style="COLOR: #000000"> NOCOUNT </span>
						<span style="COLOR: #0000ff">ON</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
						</span>
						<span style="COLOR: #0000ff">Declare</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@iRowCount</span>
						<span style="COLOR: #000000"> </span>
						<span style="FONT-WEIGHT: bold; COLOR: #000000">int</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
						</span>
						<span style="COLOR: #0000ff">Declare</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@pID</span>
						<span style="COLOR: #000000"> </span>
						<span style="FONT-WEIGHT: bold; COLOR: #000000">varchar</span>
						<span style="COLOR: #000000">(</span>
						<span style="FONT-WEIGHT: bold; COLOR: #800000">10</span>
						<span style="COLOR: #000000">),</span>
						<span style="COLOR: #008000">@pName</span>
						<span style="COLOR: #000000"> </span>
						<span style="FONT-WEIGHT: bold; COLOR: #000000">nvarchar</span>
						<span style="COLOR: #000000">(</span>
						<span style="FONT-WEIGHT: bold; COLOR: #800000">20</span>
						<span style="COLOR: #000000">),</span>
						<span style="COLOR: #008000">@pDoing</span>
						<span style="COLOR: #000000"> </span>
						<span style="FONT-WEIGHT: bold; COLOR: #000000">varchar</span>
						<span style="COLOR: #000000">(</span>
						<span style="FONT-WEIGHT: bold; COLOR: #800000">20</span>
						<span style="COLOR: #000000">),</span>
						<span style="COLOR: #008000">@pSex</span>
						<span style="COLOR: #000000"> </span>
						<span style="FONT-WEIGHT: bold; COLOR: #000000">char</span>
						<span style="COLOR: #000000">(</span>
						<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
						<span style="COLOR: #000000">)<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
						<span style="COLOR: #0000ff">Declare</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@i</span>
						<span style="COLOR: #000000"> </span>
						<span style="FONT-WEIGHT: bold; COLOR: #000000">int</span>
						<span style="COLOR: #000000">,</span>
						<span style="COLOR: #008000">@j</span>
						<span style="COLOR: #000000"> </span>
						<span style="FONT-WEIGHT: bold; COLOR: #000000">int</span>
						<span style="COLOR: #000000">,</span>
						<span style="COLOR: #008000">@k</span>
						<span style="COLOR: #000000"> </span>
						<span style="FONT-WEIGHT: bold; COLOR: #000000">int</span>
						<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">生成tbMain記錄條數</span>
						<span style="COLOR: #008080">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
						</span>
						<span style="COLOR: #0000ff">Set</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@iRowCount</span>
						<span style="COLOR: #808080">=</span>
						<span style="FONT-WEIGHT: bold; COLOR: #800000">1000</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
						</span>
						<span style="COLOR: #0000ff">Select</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@i</span>
						<span style="COLOR: #808080">=</span>
						<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
						</span>
						<span style="COLOR: #0000ff">While</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@i</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">&lt;</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@iRowCount</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
						</span>
						<span style="COLOR: #0000ff">Begin</span>
						<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">生成客戶ID</span>
						<span style="COLOR: #008080">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
						</span>
						<span style="COLOR: #000000">    </span>
						<span style="COLOR: #0000ff">Set</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@j</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">While</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@j</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">&gt;</span>
						<span style="COLOR: #000000"> </span>
						<span style="FONT-WEIGHT: bold; COLOR: #800000">0</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">Begin</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span>
						<span style="COLOR: #0000ff">Set</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@pID</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #ff00ff">Left</span>
						<span style="COLOR: #000000">(</span>
						<span style="COLOR: #ff00ff">NewID</span>
						<span style="COLOR: #000000">(),</span>
						<span style="FONT-WEIGHT: bold; COLOR: #800000">10</span>
						<span style="COLOR: #000000">)<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span>
						<span style="COLOR: #0000ff">Select</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@j</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #ff00ff">Count</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"> tbDetail </span>
						<span style="COLOR: #0000ff">Where</span>
						<span style="COLOR: #000000"> pID</span>
						<span style="COLOR: #808080">=</span>
						<span style="COLOR: #008000">@pID</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">End</span>
						<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>
						<span style="COLOR: #008080">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
						</span>
						<span style="COLOR: #000000">    </span>
						<span style="COLOR: #0000ff">Set</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@k</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="FONT-WEIGHT: bold; COLOR: #800000">2</span>
						<span style="COLOR: #808080">+</span>
						<span style="COLOR: #ff00ff">Round</span>
						<span style="COLOR: #000000">(</span>
						<span style="FONT-WEIGHT: bold; COLOR: #800000">3</span>
						<span style="COLOR: #808080">*</span>
						<span style="COLOR: #ff00ff">Rand</span>
						<span style="COLOR: #000000">(),</span>
						<span style="FONT-WEIGHT: bold; COLOR: #800000">0</span>
						<span style="COLOR: #000000">)<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">Select</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@j</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
						<span style="COLOR: #000000">,</span>
						<span style="COLOR: #008000">@pName</span>
						<span style="COLOR: #808080">=</span>
						<span style="COLOR: #ff0000">''</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">While</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@j</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">&lt;=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@k</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">Begin</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span>
						<span style="COLOR: #0000ff">Set</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@pName</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@pName</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">+</span>
						<span style="COLOR: #000000"> </span>
						<span style="FONT-WEIGHT: bold; COLOR: #000000">NChar</span>
						<span style="COLOR: #000000">(</span>
						<span style="FONT-WEIGHT: bold; COLOR: #800000">31000</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">+</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #ff00ff">Round</span>
						<span style="COLOR: #000000">(</span>
						<span style="FONT-WEIGHT: bold; COLOR: #800000">3000</span>
						<span style="COLOR: #808080">*</span>
						<span style="COLOR: #ff00ff">Rand</span>
						<span style="COLOR: #000000">(),</span>
						<span style="FONT-WEIGHT: bold; COLOR: #800000">0</span>
						<span style="COLOR: #000000">))<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span>
						<span style="COLOR: #0000ff">Set</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@j</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@j</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">+</span>
						<span style="COLOR: #000000"> </span>
						<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">End</span>
						<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>
						<span style="COLOR: #008080">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
						</span>
						<span style="COLOR: #000000">    </span>
						<span style="COLOR: #0000ff">If</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@k</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">%</span>
						<span style="COLOR: #000000"> </span>
						<span style="FONT-WEIGHT: bold; COLOR: #800000">2</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="FONT-WEIGHT: bold; COLOR: #800000">0</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span>
						<span style="COLOR: #0000ff">Set</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@pSex</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #ff0000">'</span>
						<span style="COLOR: #ff0000">T</span>
						<span style="COLOR: #ff0000">'</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">Else</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span>
						<span style="COLOR: #0000ff">Set</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@pSex</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #ff0000">'</span>
						<span style="COLOR: #ff0000">F</span>
						<span style="COLOR: #ff0000">'</span>
						<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>
						<span style="COLOR: #008080">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
						</span>
						<span style="COLOR: #000000">    </span>
						<span style="COLOR: #008080">--</span>
						<span style="COLOR: #008080">主檔添加筆數(1-5筆)</span>
						<span style="COLOR: #008080">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
						</span>
						<span style="COLOR: #000000">    </span>
						<span style="COLOR: #0000ff">Set</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@k</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">+</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #ff00ff">Round</span>
						<span style="COLOR: #000000">(</span>
						<span style="FONT-WEIGHT: bold; COLOR: #800000">5</span>
						<span style="COLOR: #808080">*</span>
						<span style="COLOR: #ff00ff">Rand</span>
						<span style="COLOR: #000000">(),</span>
						<span style="FONT-WEIGHT: bold; COLOR: #800000">0</span>
						<span style="COLOR: #000000">)<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">Set</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@j</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">While</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@j</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">&lt;=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@k</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">Begin</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span>
						<span style="COLOR: #0000ff">Set</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@i</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@i</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">+</span>
						<span style="COLOR: #000000"> </span>
						<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
						<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">生成Doing欄位</span>
						<span style="COLOR: #008080">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
						</span>
						<span style="COLOR: #000000">        </span>
						<span style="COLOR: #0000ff">Set</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@pDoing</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #ff00ff">Cast</span>
						<span style="COLOR: #000000">(</span>
						<span style="COLOR: #008000">@j</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="FONT-WEIGHT: bold; COLOR: #800000">2</span>
						<span style="COLOR: #000000">)) </span>
						<span style="COLOR: #808080">+</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #ff0000">'''</span>
						<span style="COLOR: #ff0000">th Doing<img src="http://www.blogjava.net/images/dot.gif" /></span>
						<span style="COLOR: #ff0000">'</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span>
						<span style="COLOR: #0000ff">Insert</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">Into</span>
						<span style="COLOR: #000000"> tbMain </span>
						<span style="COLOR: #0000ff">Values</span>
						<span style="COLOR: #000000"> (</span>
						<span style="COLOR: #008000">@pID</span>
						<span style="COLOR: #000000">,</span>
						<span style="COLOR: #008000">@pName</span>
						<span style="COLOR: #000000">,</span>
						<span style="COLOR: #008000">@pDoing</span>
						<span style="COLOR: #000000">)<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span>
						<span style="COLOR: #0000ff">Set</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@j</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@j</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">+</span>
						<span style="COLOR: #000000"> </span>
						<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">End</span>
						<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>
						<span style="COLOR: #008080">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
						</span>
						<span style="COLOR: #000000">    </span>
						<span style="COLOR: #0000ff">Insert</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">Into</span>
						<span style="COLOR: #000000"> tbDetail </span>
						<span style="COLOR: #0000ff">Values</span>
						<span style="COLOR: #000000"> (</span>
						<span style="COLOR: #008000">@pID</span>
						<span style="COLOR: #000000">,</span>
						<span style="COLOR: #008000">@pSex</span>
						<span style="COLOR: #000000">)<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
						<span style="COLOR: #0000ff">End</span>
						<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">隨機向剔除客戶檔添加1/10的客戶姓名</span>
						<span style="COLOR: #008080">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
						</span>
						<span style="COLOR: #0000ff">Set</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@k</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #ff00ff">Round</span>
						<span style="COLOR: #000000">(</span>
						<span style="COLOR: #008000">@iRowCount</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">/</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">0</span>
						<span style="COLOR: #000000">)<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
						<span style="COLOR: #0000ff">SET</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">ROWCOUNT</span>
						<span style="COLOR: #000000">  </span>
						<span style="COLOR: #008000">@k</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
						</span>
						<span style="COLOR: #0000ff">Insert</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">Into</span>
						<span style="COLOR: #000000"> tbForeclose </span>
						<span style="COLOR: #0000ff">Select</span>
						<span style="COLOR: #000000"> pName </span>
						<span style="COLOR: #0000ff">From</span>
						<span style="COLOR: #000000"> tbMain </span>
						<span style="COLOR: #0000ff">Group</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">By</span>
						<span style="COLOR: #000000"> pID,pName </span>
						<span style="COLOR: #0000ff">Order</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">By</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #ff00ff">NewID</span>
						<span style="COLOR: #000000">()<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span>
						<span style="COLOR: #0000ff">SET</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">ROWCOUNT</span>
						<span style="COLOR: #000000"> </span>
						<span style="FONT-WEIGHT: bold; COLOR: #800000">0</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />
						</span>
						<span style="COLOR: #0000ff">SET</span>
						<span style="COLOR: #000000"> NOCOUNT </span>
						<span style="COLOR: #0000ff">OFF</span>
				</span>
		</div>
		<p dir="ltr" style="MARGIN-RIGHT: 0px">
				<strong>嘗試及解決</strong>
				<br />      就此需求表面來看, 實現起來很是簡單, 只要一條SQL就好:</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">
				<span style="COLOR: #0000ff">Select</span>
				<span style="COLOR: #000000">
						<br />        </span>
				<span style="COLOR: #808080">*</span>
				<span style="COLOR: #000000">
						<br />    </span>
				<span style="COLOR: #0000ff">From</span>
				<span style="COLOR: #000000">
						<br />        tbDetail<br />    </span>
				<span style="COLOR: #0000ff">Inner</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">Join</span>
				<span style="COLOR: #000000"> (<br />        </span>
				<span style="COLOR: #0000ff">Select</span>
				<span style="COLOR: #000000">
						<br />                pID,pName<br />            </span>
				<span style="COLOR: #0000ff">From</span>
				<span style="COLOR: #000000">
						<br />                tbMain<br />            </span>
				<span style="COLOR: #0000ff">Where</span>
				<span style="COLOR: #000000">
						<br />                </span>
				<span style="COLOR: #808080">Not</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #808080">Exists</span>
				<span style="COLOR: #000000"> (</span>
				<span style="COLOR: #0000ff">Select</span>
				<span style="COLOR: #000000"> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">From</span>
				<span style="COLOR: #000000"> tbForeclose </span>
				<span style="COLOR: #0000ff">Where</span>
				<span style="COLOR: #000000"> tbForeclose.pName</span>
				<span style="COLOR: #808080">=</span>
				<span style="COLOR: #000000">tbMain.pName)<br />            </span>
				<span style="COLOR: #0000ff">Group</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">By</span>
				<span style="COLOR: #000000">
						<br />                pID,pName<br />            </span>
				<span style="COLOR: #0000ff">Having</span>
				<span style="COLOR: #000000">
						<br />                </span>
				<span style="COLOR: #ff00ff">Count</span>
				<span style="COLOR: #000000">(</span>
				<span style="COLOR: #808080">*</span>
				<span style="COLOR: #000000">)</span>
				<span style="COLOR: #808080">=</span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
				<span style="COLOR: #000000">
						<br />    ) tbMain1 </span>
				<span style="COLOR: #0000ff">On</span>
				<span style="COLOR: #000000"> tbMain1.pID</span>
				<span style="COLOR: #808080">=</span>
				<span style="COLOR: #000000">tbDetail.pID</span>
		</div>
		<br />
		<font face="Georgia" size="2">      但此需求在開發時卻有遇到一些限制條件, 不能這樣去實現.<br />      <u><font color="#000000">限制條件:<br /></font></u>         1. tbMain,tbForeclose,tbDetail三表分別存在於不同的DB, 因DB所屬系統各不相同, 線上環境則會有可能布署在不同DBServer上.<br />         2. tbMain記錄筆數極大,索引效率低下.<br />         3. 三張TABLE是屬於其他的系統, 本次需求不允許進行UPDATE的操作.<br />      因此, 需要對應解決.<br /><br />      對於限制條件1, 資料處理必須依3個步驟進行(見圖上A,B,C), 如圖1:<br />                        <img height="540" alt="o_1.jpg" src="http://www.blogjava.net/images/blogjava_net/tw-ddm/10051/o_1.jpg" width="373" border="0" /><br /><br />      對於限制條件2, 主要卡在步驟A的超時上(其後都僅處理到少量資料), 實測時在本機的查詢分析器RUN步驟A的SQL就要10分鍾,符合的資料筆數為百萬左右.除非是將ADO連線設為不超時, 否則一定會查詢失敗.<br /><br />      我首先的想法就是想要提高查詢效率. <br />         . 經檢查tbMain發現pID列上有索引而pName上卻無, 所以將步驟A進行分解, 僅查詢符合的pID, 實測步驟A1可以在10分鍾以內完成, 見圖2.<br />                           <img height="587" alt="o_2.jpg" src="http://www.blogjava.net/images/blogjava_net/tw-ddm/10051/o_2.jpg" width="373" border="0" /><br />         . 嘗試減少步驟A1的處理筆數, 比如先依pID的前兩碼進行分組, 再進行多次查詢, 實測資料量最大的一組pID約在2-3分鍾, 單次資料量減少到十萬.<br />      通過測試的結果可以看到該問題僅通過改善/分解查詢已經不能實現解決. <br /><br />      那麽是否能夠將步驟A的筆數減少到一筆呢?<br />         . 因三檔都不允許進行UPDATE的操作, 在處理完TOP 1的一筆後因無法對處理過的pID進行標識, 無法獲取下一筆的資料.<br /><br />      既然多種方法都不可行, 那麽就只有完全改變處理的流程, 通過多添加一張表(專為此開發而新增的TABLE)並將消耗最大的步驟A交由DB內部來完成, <strong>最終解決方法</strong>步驟如下:<br />      1. 先手工新增臨時TABLE<br />       
<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: #008080">--</span><span style="COLOR: #008080">新增臨時檔</span><span style="COLOR: #008080"><br /></span><span style="COLOR: #0000ff">Create</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Table</span><span style="COLOR: #000000"> tbTemp (<br />    aID </span><span style="FONT-WEIGHT: bold; COLOR: #000000">int</span><span style="COLOR: #000000"> </span><span style="COLOR: #ff00ff">identity</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">),<br />    pID </span><span style="FONT-WEIGHT: bold; COLOR: #000000">varchar</span><span style="COLOR: #000000">(</span><span style="FONT-WEIGHT: bold; COLOR: #800000">10</span><span style="COLOR: #000000">)<br />);</span></div><p>      2. 如果是需要多次RUN的話, 則要先Truncate Table tbTemp, 然後通過Insert Into tbTemp Select....在DB內部完成資料的篩選, 由於在客戶端不需要撈一筆資料, 所以耗時非常的短, 達到秒級.<br />      3. 由於新增檔案是有KEY值(Identity), 所以只要拿到批次添加的最大/最小ID值即可通過循環來逐筆處理了.<br />      <strong>最終解決方案</strong>如圖3<br />                              <br />                           <img height="812" alt="o_3.jpg" src="http://www.blogjava.net/images/blogjava_net/tw-ddm/10051/o_3.jpg" width="522" border="0" /></p></font>
<img src ="http://www.blogjava.net/tw-ddm/aggbug/239935.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/tw-ddm/" target="_blank">大大毛</a> 2008-11-11 18:55 <a href="http://www.blogjava.net/tw-ddm/articles/239935.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>實例解析如何實現行間運算</title><link>http://www.blogjava.net/tw-ddm/articles/142572.html</link><dc:creator>大大毛</dc:creator><author>大大毛</author><pubDate>Tue, 04 Sep 2007 03:37:00 GMT</pubDate><guid>http://www.blogjava.net/tw-ddm/articles/142572.html</guid><wfw:comment>http://www.blogjava.net/tw-ddm/comments/142572.html</wfw:comment><comments>http://www.blogjava.net/tw-ddm/articles/142572.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/tw-ddm/comments/commentRss/142572.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/tw-ddm/services/trackbacks/142572.html</trackback:ping><description><![CDATA[
		<font face="Georgia">
				<br />
				<font size="2">
						<strong>問題</strong>
						<br />
						<br />      通常來說實現表級的關聯以及在列之間實現運算還算是比較容易，可是利用SQL來實現行間的關聯運算就顯得比較困難，這裏就一個實例來解析如何來思考及解決此類的問題。<br /><br /></font>
				<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 size="2">
								<img id="Code_Closed_Image_094427" onclick="this.style.display='none'; Code_Closed_Text_094427.style.display='none'; Code_Open_Image_094427.style.display='inline'; Code_Open_Text_094427.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top" />
								<img id="Code_Open_Image_094427" style="DISPLAY: none" onclick="this.style.display='none'; Code_Open_Text_094427.style.display='none'; Code_Closed_Image_094427.style.display='inline'; Code_Closed_Text_094427.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top" />
								<span id="Code_Closed_Text_094427" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">表結構</span>
						</font>
						<span id="Code_Open_Text_094427" style="DISPLAY: none">
								<br />
								<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
								<font size="2">
										<span style="COLOR: #0000ff">Create</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Table</span>
								</font>
								<font size="2">
										<span style="COLOR: #000000"> tb_ExampleA (<br />  kID     </span>
										<span style="FONT-WEIGHT: bold; COLOR: #000000">Char</span>
										<span style="COLOR: #000000">(</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">10</span>
										<span style="COLOR: #000000">),   </span>
										<span style="COLOR: #008080">--</span>
										<span style="COLOR: #008080">合約ID,主KEY</span>
								</font>
								<span style="COLOR: #008080">
										<br />
								</span>
								<font size="2">
										<span style="COLOR: #000000">  iYear   </span>
										<span style="FONT-WEIGHT: bold; COLOR: #000000">Int</span>
										<span style="COLOR: #000000">,        </span>
										<span style="COLOR: #008080">--</span>
										<span style="COLOR: #008080">第幾年度</span>
								</font>
								<span style="COLOR: #008080">
										<br />
								</span>
								<font size="2">
										<span style="COLOR: #000000">  iTime   </span>
										<span style="FONT-WEIGHT: bold; COLOR: #000000">Int</span>
										<span style="COLOR: #000000">,        </span>
										<span style="COLOR: #008080">--</span>
										<span style="COLOR: #008080">第幾期</span>
								</font>
								<span style="COLOR: #008080">
										<br />
								</span>
								<font size="2">
										<span style="COLOR: #000000">  D1      </span>
										<span style="FONT-WEIGHT: bold; COLOR: #000000">Int</span>
										<span style="COLOR: #000000">,        </span>
										<span style="COLOR: #008080">--</span>
										<span style="COLOR: #008080">開始日期</span>
								</font>
								<span style="COLOR: #008080">
										<br />
								</span>
								<font size="2">
										<span style="COLOR: #000000">  PayMode </span>
										<span style="FONT-WEIGHT: bold; COLOR: #000000">Char</span>
										<span style="COLOR: #000000">(</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
										<span style="COLOR: #000000">),    </span>
										<span style="COLOR: #008080">--</span>
										<span style="COLOR: #008080">付款方式</span>
								</font>
								<span style="COLOR: #008080">
										<br />
								</span>
								<font size="2">
										<span style="COLOR: #000000">  cOther  </span>
										<span style="FONT-WEIGHT: bold; COLOR: #000000">Char</span>
										<span style="COLOR: #000000">(</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">100</span>
										<span style="COLOR: #000000">),  </span>
										<span style="COLOR: #008080">--</span>
										<span style="COLOR: #008080">內容</span>
								</font>
								<span style="COLOR: #008080">
										<br />
								</span>
								<font size="2">
										<span style="COLOR: #000000">  </span>
										<span style="COLOR: #0000ff">Primary</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Key</span>
								</font>
								<span style="COLOR: #000000">
										<font size="2">(kID,iYear,iMonth)<br />);</font>
								</span>
						</span>
				</div>
				<br />
				<font size="2">      表結構說明<br />         kID               主關鍵字，例如合約號<br />         iYear            第幾年度( &gt;= 1)，連續遞增<br />         iTime           第幾期[1, 12]，連續遞增，根據付款方式有所不同<br />         D1                  開始日期，數字類型的民國日期<br />         PayMode   付款方式，取值 A--年繳，S--半年繳，Q--季繳，M--月繳<br />      要求<br />         找到那些 D1 有問題的合約。因為根據PayMode來說相同 kID 的 D1 變化是存在有規律的，例如如果是A--年繳則應該是一年一跳，M--月繳則應該是一個月一跳，因此要找到那些跳的間隔有錯的合約。<br />      數據現狀<br />         iYear / iTime 兩列上的數據就很亂，有跳期的現象存在，例如月繳1年1期--1年3期。<br />         D1 列上有誤，例如月繳方式下  950505 -- 950718 (正確應該是950505 -- 959604)<br /><br />      表結構與 </font>
				<a href="/tw-ddm/articles/134308.html" target="_blank">
						<font size="2">我的上一篇</font>
				</a> <font size="2">示例2中所用表完全相同，只不過上一篇是為了找出年度+月份不連續的問題件，而這裏則是要解決數據行Row之間的對比，不過這一次還是要用到上回講述到的那些解決方案，只不過要更加複雜。<br /><br /><br /><strong>思考</strong><br /><br />      1. 要達到間隔的計算，就要實現行間數據的對比，因此大體的思路應該是使用自連接的方式來完成。<br />      2. 根據數據現狀可以看出，上一篇中提到的將 iYear + iTime 兩列合併的方案已經不可用，因為根據現狀這兩列雖然保持唯一和增加，但是增加的 Step(步長) 已經不可信任。根據自連接的特點，我們必須找到一個絕對可以信賴的遞增列，然後再在該列上實現自連接，問題是這一列該如何選取呢？<br />      3. D1 列是數字值的民國日期，要用日期的間隔來判斷需要先將該列轉換成正常的西元日期型列才可以實現。<br /><br /><br /><strong>解決方法<br /></strong><br />      1. 找到那個 Step 可以信賴的遞增列。<br />         既然 iYear + iTime 的 Step 不可信賴可是兩列唯一和增加還是可用的，因為可以用 <a href="/tw-ddm/articles/93929.html" target="_blank">自行構建Identity的方法</a> 來據此生成一個可用的 Identity 列。<br />      2. D1的西元日期型轉換，可以用 <a href="/tw-ddm/articles/142418.html" target="_blank">幾個用於SQL的日期轉換函數(vb)</a> 中功能2所示的 parseDate(strSQL) 來實現。<br />      3. 問題日期的間隔這裏只簡單的按大於32天/月來判斷。<br /><br /><strong>SQL語句</strong><br /><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"><img id="Code_Closed_Image_112528" onclick="this.style.display='none'; Code_Closed_Text_112528.style.display='none'; Code_Open_Image_112528.style.display='inline'; Code_Open_Text_112528.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top" /><img id="Code_Open_Image_112528" style="DISPLAY: none" onclick="this.style.display='none'; Code_Open_Text_112528.style.display='none'; Code_Closed_Image_112528.style.display='inline'; Code_Closed_Text_112528.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top" /><span id="Code_Closed_Text_112528" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">SQL語句</span><span id="Code_Open_Text_112528" style="DISPLAY: none"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="COLOR: #008080"> 1</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"><br /></span><span style="COLOR: #008080"> 2</span> <span style="COLOR: #000000">  (</span><span style="COLOR: #0000ff">Select</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 3</span> <span style="COLOR: #000000">        A0.</span><span style="COLOR: #808080">*</span><span style="COLOR: #000000">,<br /></span><span style="COLOR: #008080"> 4</span> <span style="COLOR: #000000">        (</span><span style="COLOR: #0000ff">Select</span><span style="COLOR: #000000"> <br /></span><span style="COLOR: #008080"> 5</span> <span style="COLOR: #000000">            </span><span style="COLOR: #ff00ff">Count</span><span style="COLOR: #000000">(</span><span style="COLOR: #808080">*</span><span style="COLOR: #000000">) <br /></span><span style="COLOR: #008080"> 6</span> <span style="COLOR: #000000">          </span><span style="COLOR: #0000ff">From</span><span style="COLOR: #000000"> <br /></span><span style="COLOR: #008080"> 7</span> <span style="COLOR: #000000">            LIB</span><span style="COLOR: #808080">/</span><span style="COLOR: #000000">tb_ExampleA subA<br /></span><span style="COLOR: #008080"> 8</span> <span style="COLOR: #000000">          </span><span style="COLOR: #0000ff">Where</span><span style="COLOR: #000000"> <br /></span><span style="COLOR: #008080"> 9</span> <span style="COLOR: #000000">            subA.kID </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> A0.kID<br /></span><span style="COLOR: #008080">10</span> <span style="COLOR: #000000">            </span><span style="COLOR: #808080">And</span><span style="COLOR: #000000"> subA.iYear</span><span style="COLOR: #808080">*</span><span style="FONT-WEIGHT: bold; COLOR: #800000">100</span><span style="COLOR: #000000"> </span><span style="COLOR: #808080">+</span><span style="COLOR: #000000"> subA.iTime </span><span style="COLOR: #808080">&lt;=</span><span style="COLOR: #000000"> A0.iYear</span><span style="COLOR: #808080">*</span><span style="FONT-WEIGHT: bold; COLOR: #800000">100</span><span style="COLOR: #000000"> </span><span style="COLOR: #808080">+</span><span style="COLOR: #000000"> A0.iTime<br /></span><span style="COLOR: #008080">11</span> <span style="COLOR: #000000">        ) Index1<br /></span><span style="COLOR: #008080">12</span> <span style="COLOR: #000000">      </span><span style="COLOR: #0000ff">From</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">13</span> <span style="COLOR: #000000">        LIB</span><span style="COLOR: #808080">/</span><span style="COLOR: #000000">tb_ExampleA A0<br /></span><span style="COLOR: #008080">14</span> <span style="COLOR: #000000">  ) AL<br /></span><span style="COLOR: #008080">15</span> <span style="COLOR: #000000">  </span><span style="COLOR: #0000ff">Inner</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Join</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">16</span> <span style="COLOR: #000000">  (</span><span style="COLOR: #0000ff">Select</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">17</span> <span style="COLOR: #000000">        A0.</span><span style="COLOR: #808080">*</span><span style="COLOR: #000000">,<br /></span><span style="COLOR: #008080">18</span> <span style="COLOR: #000000">        (</span><span style="COLOR: #0000ff">Select</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">19</span> <span style="COLOR: #000000">            </span><span style="COLOR: #ff00ff">Count</span><span style="COLOR: #000000">(</span><span style="COLOR: #808080">*</span><span style="COLOR: #000000">)<br /></span><span style="COLOR: #008080">20</span> <span style="COLOR: #000000">          </span><span style="COLOR: #0000ff">From</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">21</span> <span style="COLOR: #000000">            LIB</span><span style="COLOR: #808080">/</span><span style="COLOR: #000000">tb_ExampleA subA<br /></span><span style="COLOR: #008080">22</span> <span style="COLOR: #000000">          </span><span style="COLOR: #0000ff">Where</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">23</span> <span style="COLOR: #000000">            subA.kID </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> A0.kID<br /></span><span style="COLOR: #008080">24</span> <span style="COLOR: #000000">            </span><span style="COLOR: #808080">And</span><span style="COLOR: #000000"> subA.iYear</span><span style="COLOR: #808080">*</span><span style="FONT-WEIGHT: bold; COLOR: #800000">100</span><span style="COLOR: #000000"> </span><span style="COLOR: #808080">+</span><span style="COLOR: #000000"> subA.iTime </span><span style="COLOR: #808080">&lt;=</span><span style="COLOR: #000000"> A0.iYear</span><span style="COLOR: #808080">*</span><span style="FONT-WEIGHT: bold; COLOR: #800000">100</span><span style="COLOR: #000000"> </span><span style="COLOR: #808080">+</span><span style="COLOR: #000000"> A0.iTime<br /></span><span style="COLOR: #008080">25</span> <span style="COLOR: #000000">        ) Index1<br /></span><span style="COLOR: #008080">26</span> <span style="COLOR: #000000">      </span><span style="COLOR: #0000ff">From</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">27</span> <span style="COLOR: #000000">        LIB</span><span style="COLOR: #808080">/</span><span style="COLOR: #000000">tb_ExampleA A0<br /></span><span style="COLOR: #008080">28</span> <span style="COLOR: #000000">  ) AR<br /></span><span style="COLOR: #008080">29</span> <span style="COLOR: #000000">  </span><span style="COLOR: #0000ff">On</span><span style="COLOR: #000000"> AL.kID </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> AR.kID </span><span style="COLOR: #808080">And</span><span style="COLOR: #000000"> AL.Index1 </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> AR.Index1 </span><span style="COLOR: #808080">+</span><span style="COLOR: #000000"> </span><span style="FONT-WEIGHT: bold; COLOR: #800000">1</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">30</span> <span style="COLOR: #000000">  </span><span style="COLOR: #0000ff">Where</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">31</span> <span style="COLOR: #000000">    </span><span style="COLOR: #008000">@Date</span><span style="COLOR: #000000">(AR.D1) </span><span style="COLOR: #808080">+</span><span style="COLOR: #000000"> (<br /></span><span style="COLOR: #008080">32</span> <span style="COLOR: #000000">      </span><span style="COLOR: #ff00ff">CASE</span><span style="COLOR: #000000"> AR.PayMode<br /></span><span style="COLOR: #008080">33</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">WHEN</span><span style="COLOR: #000000"> </span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">A</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">THEN</span><span style="COLOR: #000000"> </span><span style="FONT-WEIGHT: bold; COLOR: #800000">12</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">34</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">WHEN</span><span style="COLOR: #000000"> </span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">S</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">THEN</span><span style="COLOR: #000000"> </span><span style="FONT-WEIGHT: bold; COLOR: #800000">6</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">35</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">WHEN</span><span style="COLOR: #000000"> </span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">Q</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">THEN</span><span style="COLOR: #000000"> </span><span style="FONT-WEIGHT: bold; COLOR: #800000">3</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">36</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">ELSE</span><span style="COLOR: #000000"> </span><span style="FONT-WEIGHT: bold; COLOR: #800000">1</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">37</span> <span style="COLOR: #000000">      </span><span style="COLOR: #0000ff">END</span><span style="COLOR: #000000"> </span><span style="COLOR: #808080">*</span><span style="COLOR: #000000"> </span><span style="FONT-WEIGHT: bold; COLOR: #800000">32</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">38</span> <span style="COLOR: #000000">    ) Days </span><span style="COLOR: #808080">&lt;</span><span style="COLOR: #000000"> </span><span style="COLOR: #008000">@Date</span><span style="COLOR: #000000">(AL.D1)</span></span></div><br />         從該SQL語句可以看到為了實現自連接，構建了2張表 AL(2-14行) 和 AR(16-28行)，兩表的內容完全一致，中間使用了自定義 Identity 列 Index1 (4-11行) ，然後再將AL 與 AR 實現錯值連接(29行)。<br />         對連接後表記錄的日期間隔運算，放在Where子句中(30-38行)，這裏寫的是僞語句，需要用前面提到的 VB函數轉換才可以運行，不過這樣看起來就會明子許多。<br /><br /><br />         可以看到，只要在處理裏靈活運用各種技巧就可以解決更加複雜的邏輯。</font></font>
<img src ="http://www.blogjava.net/tw-ddm/aggbug/142572.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/tw-ddm/" target="_blank">大大毛</a> 2007-09-04 11:37 <a href="http://www.blogjava.net/tw-ddm/articles/142572.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>