<?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>nazham.com &#187; SQL</title>
	<atom:link href="http://nazham.com/category/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://nazham.com</link>
	<description>Jarvis, sometimes you gotta run before you can walk.</description>
	<lastBuildDate>Tue, 01 Jun 2010 06:07:38 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Save MySQL Output to a Text File</title>
		<link>http://nazham.com/2008/09/19/save-mysql-output-to-a-text-file/</link>
		<comments>http://nazham.com/2008/09/19/save-mysql-output-to-a-text-file/#comments</comments>
		<pubDate>Fri, 19 Sep 2008 05:55:29 +0000</pubDate>
		<dc:creator>nazham</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://nazham.com/?p=263</guid>
		<description><![CDATA[I have just found out a nifty little trick regarding MySQL output the other day. I just had to put this up as a reminder to myself, and also to share with the viewers.
Let say, you want to have the output of your SELECT statement created to a text file. The MySQL database server could [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>I have just found out a nifty little trick regarding MySQL output the other day. I just had to put this up as a reminder to myself, and also to share with the viewers.</p>
<p>Let say, you want to have the output of your SELECT statement created to a text file. The MySQL database server could be sitting on a different box (or the same machine, but that doesn&#8217;t really matter). All you have to do is issue a command in your terminal:</p>
<p><code> echo "select * from MyTable" | mysql -h mydatabaseserver -u myusername -pmypassword mydatabase &gt; output.txt</code></p>
<p>That&#8217;s it! Just replace the &lt;MyTable&gt;to your table of choice (or just replace the whole SELECT statement to suit your needs),  &lt;mydatabaseserver&gt; to your database server name, &lt;myusername&gt;  to your database user name, &lt;mypassword&gt;  to your password, and &lt;mydatabase&gt;  to your database.<br />
The piping &#8216;&gt;&#8217; will redirect your output to a text file on your desired location.</p>
<iframe id="basic_facebook_social_plugins_likebutton" src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fnazham.com%2F2008%2F09%2F19%2Fsave-mysql-output-to-a-text-file%2F&amp;layout=standard&amp;show_faces=true&amp;width=&amp;action=like&amp;font=arial&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:px; height:px"></iframe>

<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://nazham.com/2008/09/19/save-mysql-output-to-a-text-file/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>SQL Select Statement with Comma-Separated List Condition</title>
		<link>http://nazham.com/2008/09/18/sql-select-statement-with-comma-separated-list-condition/</link>
		<comments>http://nazham.com/2008/09/18/sql-select-statement-with-comma-separated-list-condition/#comments</comments>
		<pubDate>Wed, 17 Sep 2008 16:26:27 +0000</pubDate>
		<dc:creator>nazham</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://nazham.com/?p=200</guid>
		<description><![CDATA[I have one problem the other day; Why does my &#8220;SELECT * FROM tbl WHERE col IN (@list)&#8221; does not work?  The col datatype is int, while the @list parameter is a comma-separated varchar.
The Problem
In a stored procedure, I&#8217;m composing a SELECT statement with a WHERE clause in which the column condition datatype is integer, while [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>I have one problem the other day; <em>Why does my &#8220;SELECT * FROM tbl WHERE col IN (@list)&#8221; does not work? </em> The <em>col</em> datatype is <strong>int</strong>, while the <em>@list</em> parameter is a comma-separated <strong>varchar</strong>.</p>
<p><strong>The Problem</strong><br />
In a stored procedure, I&#8217;m composing a SELECT statement with a WHERE clause in which the column condition datatype is integer, while the parameter supplied to it has the datatype of varchar:</p>
<p><code>CREATE PROCEDURE get_product_names @ids varchar(50) AS<br />
SELECT ProductID, ProductName<br />
FROM   Northwind..Products<br />
WHERE  ProductID IN (@ids)</code></p>
<p>ProductID is int, @ids is a comma-separated string, i.e.: &#8216;9,12,27,37&#8242;.</p>
<p>When try to call:<br />
<code>EXEC get_product_names '9,12,27,37'</code></p>
<p>But this fails with:<br />
<code>Server: Msg 245, Level 16, State 1, Procedure get_product_names, Line 2<br />
Syntax error converting the varchar value '9,12,27,37' to a column of data type int.</code></p>
<p>Then comes the solution, convert the comma-separated string to a recordset in a temporary table.</p>
<p><strong>The Solution</strong><br />
There&#8217;s quite a number of solutions out there, but I&#8217;ve found a pretty good ways along with a very comprehensive explainations  <a href="http://www.sommarskog.se/arrays-in-sql-2000.html" target="_blank">here</a>. The one that I&#8217;m implementing is called the Iterative Method.</p>
<p>Create a new User Defined Function:</p>
<pre>CREATE FUNCTION iter_intlist_to_table (@list ntext)
      RETURNS @tbl TABLE (listpos int IDENTITY(1, 1) NOT NULL,
                          number  int NOT NULL) AS
   BEGIN
      DECLARE @pos      int,
              @textpos  int,
              @chunklen smallint,
              @str      nvarchar(4000),
              @tmpstr   nvarchar(4000),
              @leftover nvarchar(4000)

      SET @textpos = 1
      SET @leftover = ''
      WHILE @textpos &lt;= datalength(@list) / 2
      BEGIN
         SET @chunklen = 4000 - datalength(@leftover) / 2
         SET @tmpstr = ltrim(@leftover + substring(@list, @textpos,
                                 @chunklen))
         SET @textpos = @textpos + @chunklen

         SET @pos = charindex(',', @tmpstr)
         WHILE @pos &gt; 0
         BEGIN
            SET @str = substring(@tmpstr, 1, @pos - 1)
            INSERT @tbl (number) VALUES(convert(int, @str))
            SET @tmpstr = ltrim(substring(@tmpstr, @pos + 1,
                                 len(@tmpstr)))
            SET @pos = charindex(',', @tmpstr)
         END

         SET @leftover = @tmpstr
      END

      IF ltrim(rtrim(@leftover)) &lt;&gt; ''
         INSERT @tbl (number) VALUES(convert(int, @leftover))

      RETURN
   END</pre>
<p>Modify the Stored Procedure to the one below:</p>
<pre>CREATE PROCEDURE get_product_names @ids varchar(50) AS
      SELECT ProductID, ProductName
      FROM   Northwind..Products
      WHERE  ProductID IN (SELECT number FROM
                       dbo.iter_intlist_to_table(@ids))
   go</pre>
<p>Execute the Stored Procedure:<br />
<code>EXEC get_product_names_iter '9,12,27,37'</code></p>
<p>Now the stored procedure will happily accepts the comma-separated varchar parameter, and returns the records needed. Is there any other method to this? Feel free to share. <img src='http://nazham.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<iframe id="basic_facebook_social_plugins_likebutton" src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fnazham.com%2F2008%2F09%2F18%2Fsql-select-statement-with-comma-separated-list-condition%2F&amp;layout=standard&amp;show_faces=true&amp;width=&amp;action=like&amp;font=arial&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:px; height:px"></iframe>

<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://nazham.com/2008/09/18/sql-select-statement-with-comma-separated-list-condition/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
