<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>108.bz &#187; SQL</title>
	<atom:link href="http://www.108.bz/posts/tag/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.108.bz</link>
	<description>Wandering futilities...</description>
	<lastBuildDate>Wed, 08 Sep 2010 13:45:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>From MS SQL to MySQL, realtime row copy</title>
		<link>http://www.108.bz/posts/it/from-ms-sql-to-mysql-realtime-row-copy/</link>
		<comments>http://www.108.bz/posts/it/from-ms-sql-to-mysql-realtime-row-copy/#comments</comments>
		<pubDate>Mon, 06 Sep 2010 13:26:42 +0000</pubDate>
		<dc:creator>Giuliano</dc:creator>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Interoperability]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.108.bz/?p=556</guid>
		<description><![CDATA[Or: &#8220;How to call a web server from a Microsoft SQL Server Stored Procedure&#8221;. Customer has got a VoIP software PBX (Swyx). It logs incoming calls (the CDR) in a MS SQL Server database. The CDR structure is straightforward: a single table where each row is a call, indexed by CallId (transferred calls get, eventually, [...]]]></description>
			<content:encoded><![CDATA[<p>Or: &#8220;How to call a web server from a Microsoft SQL Server Stored Procedure&#8221;.<br />
Customer has got a VoIP software PBX (<a href="http://www.swyx.com/">Swyx</a>). It logs incoming calls (the <abbr title="Call Detail Record">CDR</abbr>) in a MS SQL Server database. The CDR structure is straightforward: a single table where each row is a call, indexed by <span style="font-family: Bitstream Vera Sans Mono,Courier New,monospace;">CallId</span> (transferred calls get, eventually, a new row and a &#8220;child CallId&#8221;).<br />
I needed to process the CDR within these specs/restrictions:</p>
<ul>
<li>Each row has to be processed as soon as it is INSERTed</li>
<li>Rows must be filtered (depending on the called number)</li>
<li>Filtered rows must be &#8220;mirrored&#8221; to a MySQL DB</li>
<li>MS SQL machine is heavily loaded and mission critical; the row-copy mechanism must be light and fast</li>
</ul>
<p>The first and second specs imply the use of triggers/stored procedures.</p>
<p>I originally thought that the &#8220;DB link&#8221;-kind of functionality could be achieved natively on MS SQL. In theory it can, via <a href="http://msdn.microsoft.com/en-us/library/aa213778(SQL.80).aspx">Linked Servers</a> (<a href="http://www.ideaexcursion.com/2009/02/25/howto-setup-sql-server-linked-server-to-mysql/">bound</a> to ODBC Data Sources). There&#8217;s a catch though: you can SELECT stuff on linked servers at will, but as soon as you try to INSERT, you&#8217;ll hit error 7391<sup class='footnote'><a href='#fn-556-1' id='fnref-556-1'>1</a></sup>. MS SQL, can&#8217;t really blame it, would like to be able to rollback any change made, even on the linked MySQL. It needs to start a (implicit, distributed) transaction on MySQL, but that&#8217;s not supported and the write fails. <a href="http://forums.mysql.com/read.php?60,209856,256141#msg-256141">This</a> workaround (forcibly switch off implicit transactions) didn&#8217;t work for me. Apparently, the Oracle OLEDB Provider is able to ignore/disable distributing transactions when the parameter <span style="font-family: Bitstream Vera Sans Mono,Courier New,monospace;">DistribTX=0</span> is in the provider string. MySQL&#8217;s ODBC driver doesn&#8217;t provide a similar toggle.</p>
<p>The easiest way to push data &#8220;out&#8221; of MS SQL is (arguably) through HTTP. The DB GETs a full URL, passing key/value parameters to a Web Service that outputs to MySQL.</p>
<p>On with the code, starting with the &#8220;Web Service&#8221;. What follow is a mere Perl script, useful for testing. Depending on the expected load, you may want to use a proper application server, providing MySQL DB <a href="http://en.wikipedia.org/wiki/Connection_pool">connection pooling</a>. What you should <i>really</i> do, is serve the script through HTTPS and password protect it. Without SSL, a malicious user could sniff the cleartext requests sent by the source DB, forge similar ones and litter/DOS the MySQL instance. Of course, the Web Service could output to just any supported DB, not only to MySQL.</p>
<div class="codecolorer-container perl blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;height:300px;"><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl</span><br />
<br />
<span style="color: #000000; font-weight: bold;">use</span> DBI<span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> CGI<span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$DEBUG</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@FIELDS</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/qw.html"><span style="color: #000066;">qw</span></a><span style="color: #009900;">&#40;</span><br />
CallId<br />
OriginationNumber<br />
CalledNumber<br />
DestinationNumber<br />
StartTime<br />
ScriptConnectTime<br />
DeliveredTime<br />
ConnectTime<br />
TransferTime<br />
EndTime<br />
DisconnectReason<br />
TransferredToCallId<br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$q</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CGI<span style="color: #339933;">;</span><br />
<a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #0000ff;">$q</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">header</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>type <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'text/plain'</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span>charset <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'ISO-8859-1'</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span>expires <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'-1d'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># checks</span><br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$checkresult</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$checkmessage</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">''</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">sub</span> setcheck <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$$</span><span style="color: #0000ff;">$$</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$rrc</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$rc</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$rrs</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$rs</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$$rrc</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$rc</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$$rrs</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$rs</span><span style="color: #339933;">;</span>&nbsp; &nbsp; <br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">sub</span> isnumber <span style="color: #009900;">&#123;</span> <a href="http://perldoc.perl.org/functions/return.html"><span style="color: #000066;">return</span></a> <span style="color: #cc66cc;">1</span> <span style="color: #b1b100;">if</span> <span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/^[0-9]*$/i</span><span style="color: #339933;">;</span> <a href="http://perldoc.perl.org/functions/return.html"><span style="color: #000066;">return</span></a> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">sub</span> issane <span style="color: #009900;">&#123;</span> <a href="http://perldoc.perl.org/functions/return.html"><span style="color: #000066;">return</span></a> <span style="color: #cc66cc;">1</span> <span style="color: #b1b100;">if</span> <span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/^[a-z0-9%:\- ]*$/i</span><span style="color: #339933;">;</span> <a href="http://perldoc.perl.org/functions/return.html"><span style="color: #000066;">return</span></a> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><br />
setcheck<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">\$checkresult</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #0000ff;">\$checkmessage</span><span style="color: #339933;">,</span><span style="color: #ff0000;">'NULL CallId'</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">if</span> <span style="color: #0000ff;">$checkresult</span> <span style="color: #b1b100;">and</span> <span style="color: #b1b100;">not</span> <span style="color: #0000ff;">$q</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">param</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'CallId'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
setcheck<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">\$checkresult</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #0000ff;">\$checkmessage</span><span style="color: #339933;">,</span><span style="color: #ff0000;">'CallId must be a number'</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">if</span> <span style="color: #0000ff;">$checkresult</span> <span style="color: #b1b100;">and</span> <span style="color: #b1b100;">not</span> isnumber<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$q</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">param</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'CallId'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@FIELDS</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; setcheck<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">\$checkresult</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #0000ff;">\$checkmessage</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;$_ value contains invalid characters&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #0000ff;">$checkresult</span> <span style="color: #b1b100;">and</span> <span style="color: #b1b100;">not</span> issane<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$q</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">param</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$checkresult</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$dbh</span> <span style="color: #339933;">=</span> DBI<span style="color: #339933;">-&gt;</span><span style="color: #006600;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'DBI:mysql:database=dbname'</span><span style="color: #339933;">,</span><span style="color: #ff0000;">'dbuser'</span><span style="color: #339933;">,</span><span style="color: #ff0000;">'password'</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;KO: Error $DBI::err - $DBI::errstr<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <a href="http://perldoc.perl.org/functions/exit.html"><span style="color: #000066;">exit</span></a><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$values</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/join.html"><span style="color: #000066;">join</span></a> <span style="color: #ff0000;">','</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span> <a href="http://perldoc.perl.org/functions/map.html"><span style="color: #000066;">map</span></a> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">quote</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$q</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">param</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #0000ff;">$q</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">param</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #ff0000;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span> <span style="color: #0000ff;">@FIELDS</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sth</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;INSERT INTO callslog VALUES ($values)&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;KO: Error $DBI::err - $DBI::errstr<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <a href="http://perldoc.perl.org/functions/exit.html"><span style="color: #000066;">exit</span></a><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">execute</span> <span style="color: #b1b100;">or</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;KO: Error $DBI::err - $DBI::errstr<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <a href="http://perldoc.perl.org/functions/exit.html"><span style="color: #000066;">exit</span></a><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$DEBUG</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #0000ff;">$_</span><span style="color: #339933;">.</span><span style="color: #ff0000;">': '</span><span style="color: #339933;">.</span><span style="color: #0000ff;">$q</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">param</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #b1b100;">for</span> <span style="color: #0000ff;">@FIELDS</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;OK<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;KO: $checkmessage<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<a href="http://perldoc.perl.org/functions/exit.html"><span style="color: #000066;">exit</span></a><span style="color: #339933;">;</span></div></div>
<p>Next, the trigger code. It acts after each INSERT on the IpPbxCDR table. If a called number ends with the given digits, calls the Stored Procedure <span style="font-family: Bitstream Vera Sans Mono,Courier New,monospace;">spLogCall</span>, passing it the fields we&#8217;re interested in. I use the (commented) <span style="font-family: Bitstream Vera Sans Mono,Courier New,monospace;">raiserror</span>  call for debugging purposes.</p>
<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;height:300px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">USE</span> <span style="color: #66cc66;">&#91;</span>ippbxlog<span style="color: #66cc66;">&#93;</span><br />
GO<br />
<span style="color: #993333; font-weight: bold;">SET</span> ANSI_NULLS <span style="color: #993333; font-weight: bold;">ON</span><br />
GO<br />
<span style="color: #993333; font-weight: bold;">SET</span> QUOTED_IDENTIFIER <span style="color: #993333; font-weight: bold;">ON</span><br />
GO<br />
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TRIGGER</span> <span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>tr_ProcessCall<span style="color: #66cc66;">&#93;</span><br />
<span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>IpPbxCDR<span style="color: #66cc66;">&#93;</span><br />
AFTER <span style="color: #993333; font-weight: bold;">INSERT</span><br />
<span style="color: #993333; font-weight: bold;">AS</span><br />
BEGIN<br />
&nbsp; &nbsp; DECLARE<br />
&nbsp; &nbsp; &nbsp; &nbsp; @RightMatch nvarchar <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; @CallId int<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; @OriginationNumber nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; @CalledNumber nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; @DestinationNumber nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; @StartTime datetime<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; @ScriptConnectTime datetime<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; @DeliveredTime datetime<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; @ConnectTime datetime<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; @TransferTime datetime<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; @EndTime datetime<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; @DisconnectReason nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; @TransferredToCallId int<br />
&nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">SET</span> @RightMatch <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'12345678'</span><br />
&nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">SELECT</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; @CallId <span style="color: #66cc66;">=</span> CallId<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; @OriginationNumber <span style="color: #66cc66;">=</span> OriginationNumber<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; @CalledNumber <span style="color: #66cc66;">=</span> CalledNumber<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; @DestinationNumber <span style="color: #66cc66;">=</span> DestinationNumber<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; @StartTime <span style="color: #66cc66;">=</span> StartTime<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; @ScriptConnectTime <span style="color: #66cc66;">=</span> ScriptConnectTime<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; @DeliveredTime <span style="color: #66cc66;">=</span> DeliveredTime<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; @ConnectTime <span style="color: #66cc66;">=</span> ConnectTime<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; @TransferTime <span style="color: #66cc66;">=</span> TransferTime<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; @EndTime <span style="color: #66cc66;">=</span> EndTime<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; @DisconnectReason <span style="color: #66cc66;">=</span> DisconnectReason<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; @TransferredToCallId <span style="color: #66cc66;">=</span> TransferredToCallId<br />
&nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">FROM</span> INSERTED<br />
&nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">RIGHT</span><span style="color: #66cc66;">&#40;</span>@DestinationNumber<span style="color: #66cc66;">,</span>LEN<span style="color: #66cc66;">&#40;</span>@RightMatch<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> @RightMatch<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">RIGHT</span><span style="color: #66cc66;">&#40;</span>@CalledNumber<span style="color: #66cc66;">,</span>LEN<span style="color: #66cc66;">&#40;</span>@RightMatch<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> @RightMatch<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; BEGIN<br />
<span style="color: #808080; font-style: italic;">--raiserror('%s',16,1, @DestinationNumber)</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; EXEC spLogCall<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @CallId<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @OriginationNumber<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @CalledNumber<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @DestinationNumber<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @StartTime<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @ScriptConnectTime<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @DeliveredTime<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @ConnectTime<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @TransferTime<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @EndTime<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @DisconnectReason<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @TransferredToCallId<br />
&nbsp; &nbsp; END<br />
END</div></div>
<p>Lastly, the Web Service contacting Stored Procedure. I use <a href="http://msdn.microsoft.com/en-us/library/ms189763.aspx"><span style="font-family: Bitstream Vera Sans Mono,Courier New,monospace;">sp_OACreate</span></a> to create an OLE object of class <span style="font-family: Bitstream Vera Sans Mono,Courier New,monospace;">MSXML2.ServerXMLHTTP</span> passing it the contructed GET URL (address + parameters). Depending on MS SQL&#8217;s version, you may have to explicitly enable in-database OLE automation, this way:</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">exec sp_configure 'show advanced options', 1<br />
go<br />
reconfigure<br />
go<br />
exec sp_configure 'Ole Automation Procedures', 1<br />
go<br />
reconfigure<br />
go</div></div>
<p>Timeouts for various operations are set to reasonably low values, we don&#8217;t want the DB to &#8220;block&#8221; for too long. And again: <i>use HTTPS</i>. Get your certificates right (on MS SQL&#8217;s server, install the root certificate for the CA who issued the cert you&#8217;re using on the web/application server) and use HTTPS.</p>
<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;height:300px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">USE</span> <span style="color: #66cc66;">&#91;</span>ippbxlog<span style="color: #66cc66;">&#93;</span><br />
GO<br />
<span style="color: #993333; font-weight: bold;">SET</span> ANSI_NULLS <span style="color: #993333; font-weight: bold;">ON</span><br />
GO<br />
<span style="color: #993333; font-weight: bold;">SET</span> QUOTED_IDENTIFIER <span style="color: #993333; font-weight: bold;">ON</span><br />
GO<br />
<br />
<span style="color: #993333; font-weight: bold;">CREATE</span> PROCEDURE <span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>spLogCall<span style="color: #66cc66;">&#93;</span> <br />
&nbsp; &nbsp; @CallId int<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; @OriginationNumber nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; @CalledNumber nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; @DestinationNumber nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; @StartTime datetime<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; @ScriptConnectTime datetime<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; @DeliveredTime datetime<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; @ConnectTime datetime<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; @TransferTime datetime<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; @EndTime datetime<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; @DisconnectReason nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; @TransferredToCallId int<br />
<span style="color: #993333; font-weight: bold;">AS</span><br />
<br />
BEGIN<br />
DECLARE <br />
&nbsp; &nbsp; @Object int<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; @hr int<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; @openparams nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2048</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; @responsetext varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8000</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
EXEC @hr <span style="color: #66cc66;">=</span> sp_OACreate <span style="color: #ff0000;">'MSXML2.ServerXMLHTTP'</span><span style="color: #66cc66;">,</span> @Object out<br />
<span style="color: #993333; font-weight: bold;">IF</span> @hr <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span><br />
BEGIN<br />
&nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">SET</span> CONCAT_NULL_YIELDS_NULL OFF<br />
&nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">SET</span> @openparams <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'open(&quot;GET&quot;, &quot;http://10.1.1.123/ws/CDR.pl?'</span> <span style="color: #66cc66;">+</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">'CallId='</span> <span style="color: #66cc66;">+</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CAST<span style="color: #66cc66;">&#40;</span>@CallId <span style="color: #993333; font-weight: bold;">AS</span> varchar<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'&amp;'</span> <span style="color: #66cc66;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">'OriginationNumber='</span> <span style="color: #66cc66;">+</span> &nbsp; &nbsp;CAST<span style="color: #66cc66;">&#40;</span>@OriginationNumber <span style="color: #993333; font-weight: bold;">AS</span> varchar<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'&amp;'</span> <span style="color: #66cc66;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">'CalledNumber='</span> <span style="color: #66cc66;">+</span> &nbsp; &nbsp; &nbsp; &nbsp; CAST<span style="color: #66cc66;">&#40;</span>@CalledNumber <span style="color: #993333; font-weight: bold;">AS</span> varchar<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'&amp;'</span> <span style="color: #66cc66;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">'DestinationNumber='</span> <span style="color: #66cc66;">+</span> &nbsp; &nbsp;CAST<span style="color: #66cc66;">&#40;</span>@DestinationNumber <span style="color: #993333; font-weight: bold;">AS</span> varchar<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'&amp;'</span> <span style="color: #66cc66;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">'StartTime='</span> <span style="color: #66cc66;">+</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CONVERT<span style="color: #66cc66;">&#40;</span>varchar<span style="color: #66cc66;">,</span> @StartTime<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">120</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'&amp;'</span> <span style="color: #66cc66;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">'ScriptConnectTime='</span> <span style="color: #66cc66;">+</span> &nbsp; &nbsp;CONVERT<span style="color: #66cc66;">&#40;</span>varchar<span style="color: #66cc66;">,</span> @ScriptConnectTime<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">120</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'&amp;'</span> <span style="color: #66cc66;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">'DeliveredTime='</span> <span style="color: #66cc66;">+</span> &nbsp; &nbsp; &nbsp; &nbsp;CONVERT<span style="color: #66cc66;">&#40;</span>varchar<span style="color: #66cc66;">,</span> @DeliveredTime<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">120</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'&amp;'</span> <span style="color: #66cc66;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">'ConnectTime='</span> <span style="color: #66cc66;">+</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CONVERT<span style="color: #66cc66;">&#40;</span>varchar<span style="color: #66cc66;">,</span> @ConnectTime<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">120</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'&amp;'</span> <span style="color: #66cc66;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">'TransferTime='</span> <span style="color: #66cc66;">+</span> &nbsp; &nbsp; &nbsp; &nbsp; CONVERT<span style="color: #66cc66;">&#40;</span>varchar<span style="color: #66cc66;">,</span> @TransferTime<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">120</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'&amp;'</span> <span style="color: #66cc66;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">'EndTime='</span> <span style="color: #66cc66;">+</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CONVERT<span style="color: #66cc66;">&#40;</span>varchar<span style="color: #66cc66;">,</span> @EndTime<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">120</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'&amp;'</span> <span style="color: #66cc66;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">'DisconnectReason='</span> <span style="color: #66cc66;">+</span> &nbsp; &nbsp; CAST<span style="color: #66cc66;">&#40;</span>@DisconnectReason <span style="color: #993333; font-weight: bold;">AS</span> varchar<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'&amp;'</span> <span style="color: #66cc66;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">'TransferredToCallId='</span> <span style="color: #66cc66;">+</span> &nbsp;CAST<span style="color: #66cc66;">&#40;</span>@TransferredToCallId <span style="color: #993333; font-weight: bold;">AS</span> varchar<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">'&quot;, False)'</span><br />
&nbsp; &nbsp; EXEC @hr <span style="color: #66cc66;">=</span> sp_OAMethod @Object<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'setTimeouts(3000,3000,3000,3000)'</span><br />
&nbsp; &nbsp; EXEC @hr <span style="color: #66cc66;">=</span> sp_OAMethod @Object<span style="color: #66cc66;">,</span> @openparams<br />
&nbsp; &nbsp; EXEC @hr <span style="color: #66cc66;">=</span> sp_OAMethod @Object<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Send'</span><br />
&nbsp; &nbsp; EXEC @hr <span style="color: #66cc66;">=</span> sp_OAGetProperty @Object<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'responseText'</span><span style="color: #66cc66;">,</span> @responseText out<br />
END<br />
END</div></div>
<p>That&#8217;s it, the method performs and scales quite well. I think I&#8217;ll find other uses for it soon&#8230;
<div class='footnotes'>
<div class='footnotedivider'></div>
<ol>
<li id='fn-556-1'>The operation could not be performed because OLE DB provider &#8220;%ls&#8221; for linked server &#8220;%ls&#8221; was unable to begin a distributed transaction. <span class='footnotereverse'><a href='#fnref-556-1'>&#8617;</a></span></li>
</ol>
</div>
 <img src="http://www.108.bz/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=556" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://www.108.bz/posts/it/from-ms-sql-to-mysql-realtime-row-copy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>From SQL to Excel, with Perl</title>
		<link>http://www.108.bz/posts/it/from-sql-to-excel-with-perl/</link>
		<comments>http://www.108.bz/posts/it/from-sql-to-excel-with-perl/#comments</comments>
		<pubDate>Wed, 20 Jan 2010 12:24:39 +0000</pubDate>
		<dc:creator>Giuliano</dc:creator>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Excel]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Swyx]]></category>

		<guid isPermaLink="false">http://www.108.bz/?p=239</guid>
		<description><![CDATA[Quite often I&#8217;m asked to pull out some information from a database, process it and produce an Excel report. Here is a minimal Perl script that carries out the task. Define the column headings and their widths. @columns array. Handle the command line parameters. There are 5 in the example, assigned to the $p_* variables. [...]]]></description>
			<content:encoded><![CDATA[<p>Quite often I&#8217;m asked to pull out some information from a database, process it and produce an Excel report.<br />
Here is a minimal Perl script that carries out the task.</p>
<ul>
<li>Define the column headings and their widths. <span style="font-family: Bitstream Vera Sans Mono,Courier New,monospace;">@columns</span> array.</li>
<li>Handle the command line parameters. There are 5 in the example, assigned to the <span style="font-family: Bitstream Vera Sans Mono,Courier New,monospace;">$p_*</span> variables.</li>
<li>Prepare the Excel worksheet, defining cell formatting, &#8230;</li>
<li>Connect to the database.</li>
<li>Prepare the query, substituting the command line parameters.</li>
<li>Fetch rows, populate the sheet.</li>
</ul>
<div class="codecolorer-container perl blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;height:300px;"><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl </span><br />
<span style="color: #666666; font-style: italic;"># Giuliano - http://www.108.bz</span><br />
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> DBI<span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> Spreadsheet<span style="color: #339933;">::</span><span style="color: #006600;">WriteExcel</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">use</span> constant C_HEADING <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">use</span> constant C_WIDTH &nbsp; <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@columns</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'Date'</span><span style="color: #339933;">,</span> &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">22</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'Caller'</span><span style="color: #339933;">,</span> &nbsp; &nbsp;<span style="color: #cc66cc;">20</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'Called'</span><span style="color: #339933;">,</span> &nbsp; &nbsp;<span style="color: #cc66cc;">20</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'Connected'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">11</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'Duration'</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #cc66cc;">11</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'Reason'</span><span style="color: #339933;">,</span> &nbsp; &nbsp;<span style="color: #cc66cc;">24</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'XferExt'</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #cc66cc;">11</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'XferName'</span><span style="color: #339933;">,</span> &nbsp;<span style="color: #cc66cc;">22</span> <span style="color: #009900;">&#93;</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<a href="http://perldoc.perl.org/functions/die.html"><span style="color: #000066;">die</span></a> <span style="color: #339933;">&lt;&lt;</span>EOM <span style="color: #b1b100;">unless</span> <span style="color: #339933;">@</span><span style="color: #000000; font-weight: bold;">ARGV</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span><br />
usage<span style="color: #339933;">:</span><br />
<span style="color: #0000ff;">$0</span> year month day phonenumber file<span style="color: #339933;">.</span>xls<br />
EOM<br />
<span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$p_year</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$p_month</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$p_day</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$p_phnumber</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$p_filename</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #000000; font-weight: bold;">ARGV</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$workbook</span> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #339933;">=</span> Spreadsheet<span style="color: #339933;">::</span><span style="color: #006600;">WriteExcel</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$p_filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sheet</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #0000ff;">$workbook</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">add_worksheet</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Data&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$default_format</span> &nbsp;<span style="color: #339933;">=</span> <span style="color: #0000ff;">$workbook</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">add_format</span><span style="color: #009900;">&#40;</span>num_format <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'@'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$default_format</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">set_font</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'Verdana'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$default_format</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">set_border</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$bold_format</span> &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #0000ff;">$workbook</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">add_format</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$bold_format</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">set_font</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'Verdana'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$bold_format</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">set_bold</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$bold_format</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">set_border</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #0000ff;">$sheet</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">write</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$_</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$columns</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span>C_HEADING<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$bold_format</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">..</span><span style="color: #0000ff;">$#columns</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #0000ff;">$sheet</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">set_column</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$_</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$_</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$columns</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span>C_WIDTH<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">..</span><span style="color: #0000ff;">$#columns</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$dbh</span> <span style="color: #339933;">=</span> DBI<span style="color: #339933;">-&gt;</span><span style="color: #006600;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'dbi:Sybase:server=dsnname;database=dnbame'</span><span style="color: #339933;">,</span><span style="color: #ff0000;">'username'</span><span style="color: #339933;">,</span><span style="color: #ff0000;">'password'</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> <a href="http://perldoc.perl.org/functions/die.html"><span style="color: #000066;">die</span></a><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sth</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&lt;&lt;</span>EOQ<br />
SELECT IpPbxCDR<span style="color: #339933;">.</span>StartTime<span style="color: #339933;">,</span> IpPbxCDR<span style="color: #339933;">.</span>OriginationNumber<span style="color: #339933;">,</span> IpPbxCDR<span style="color: #339933;">.</span>CalledNumber<span style="color: #339933;">,</span> IpPbxCDR<span style="color: #339933;">.</span>DestinationNumber<span style="color: #339933;">,</span> DATEDIFF<span style="color: #009900;">&#40;</span>ss<span style="color: #339933;">,</span> IpPbxCDR<span style="color: #339933;">.</span>StartTime<span style="color: #339933;">,</span> IpPbxCDR<span style="color: #339933;">.</span>EndTime<span style="color: #009900;">&#41;</span> AS Duration<span style="color: #339933;">,</span> IpPbxCDR<span style="color: #339933;">.</span>DisconnectReason<span style="color: #339933;">,</span> IpPbxCDR_1<span style="color: #339933;">.</span>CalledNumber AS XferExt<span style="color: #339933;">,</span> <br />
IpPbxCDR_1<span style="color: #339933;">.</span>CalledName AS XferName<br />
FROM IpPbxCDR LEFT OUTER JOIN<br />
IpPbxCDR AS IpPbxCDR_1 ON IpPbxCDR<span style="color: #339933;">.</span>TransferredToCallId <span style="color: #339933;">=</span> IpPbxCDR_1<span style="color: #339933;">.</span>CallId<br />
WHERE <span style="color: #009900;">&#40;</span>IpPbxCDR<span style="color: #339933;">.</span>CalledNumber LIKE <span style="color: #ff0000;">'$p_phnumber'</span><span style="color: #009900;">&#41;</span> AND<br />
<span style="color: #009900;">&#40;</span>MONTH<span style="color: #009900;">&#40;</span>IpPbxCDR<span style="color: #339933;">.</span>StartTime<span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$p_month</span><span style="color: #009900;">&#41;</span> AND<br />
<span style="color: #009900;">&#40;</span>YEAR<span style="color: #009900;">&#40;</span>IpPbxCDR<span style="color: #339933;">.</span>StartTime<span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$p_year</span><span style="color: #009900;">&#41;</span> AND <br />
<span style="color: #009900;">&#40;</span>DAY<span style="color: #009900;">&#40;</span>IpPbxCDR<span style="color: #339933;">.</span>StartTime<span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$p_day</span><span style="color: #009900;">&#41;</span><br />
ORDER BY IpPbxCDR<span style="color: #339933;">.</span>StartTime<br />
EOQ<br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$row</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$row</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">fetchrow_arrayref</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$sheet</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">write_string</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$i</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$_</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$default_format</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">..</span><span style="color: #0000ff;">$#</span><span style="color: #0000ff;">$row</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$i</span><span style="color: #339933;">++;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #0000ff;">$sheet</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">activate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<a href="http://perldoc.perl.org/functions/exit.html"><span style="color: #000066;">exit</span></a><span style="color: #339933;">;</span></div></div>
<p>Actually, the example does something useful. It connects to a Swyx Call Detail Record database, selecting phone calls placed to a given number on a given day. The generated report also contains call duration and transfer status/destination, if any. Here&#8217;s what it looks like (some data has been obfuscated, to protect the innocent &#8211; click to see all the columns):</p>
<p><a href="http://www.108.bz/wp-content/uploads/2010/01/callreport.gif"><img class="alignnone size-medium wp-image-246" title="Call Report" src="http://www.108.bz/wp-content/uploads/2010/01/callreport_cut.gif" alt="" width="579" height="83" /></a></p>
<p>And here&#8217;s the command that produces it:</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">./callreport.pl 2010 1 19 '+39%10123123' x.xls</div></div>
 <img src="http://www.108.bz/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=239" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://www.108.bz/posts/it/from-sql-to-excel-with-perl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
