<?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>TechieDan &#187; SQL</title>
	<atom:link href="http://techiedan.com/tag/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://techiedan.com</link>
	<description>Your Search for The Tech Stuffs</description>
	<lastBuildDate>Tue, 22 May 2012 08:10:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Track Records on Database using DB2</title>
		<link>http://techiedan.com/2011/02/10/track-records-database-db2/</link>
		<comments>http://techiedan.com/2011/02/10/track-records-database-db2/#comments</comments>
		<pubDate>Thu, 10 Feb 2011 03:55:17 +0000</pubDate>
		<dc:creator>techieDan</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[DB2]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[timestamp]]></category>

		<guid isPermaLink="false">http://techiedan.com/?p=1029</guid>
		<description><![CDATA[For Database Administrators and Developers, this is a common practice. Sometimes we would love to track who has accessed a particular record from the users' perspective based on ID and time. The usage of TIMESTAMP would be the best way.]]></description>
			<content:encoded><![CDATA[<p>For <strong>Database Administrators</strong> and <strong>Developers</strong>, this is a common practice. Sometimes we would love to <strong>track who has accessed a particular record from the users&#8217; perspective based on ID and time</strong>.</p>
<p>How can we track it then? This is usually the job for the developer to code into their <strong>SQL query to do a simple INSERT or a simple UPDATE</strong>. It is known to anyone who wants to start coding, the best way to track is to get the user ID of the specific user and the date and time of the transaction was done. For the simplicity and best way to track date and time, the usage of <strong>TIMESTAMP</strong> would be the best way.</p>
<p>If you’ve just joined a company and they have a field name <strong>DATE_UPDATED</strong> and values <strong><em>1/7/2011 2:39:35 PM</em></strong>, now how do you update this?</p>
<p>Yes, just by using <strong>TIMESTAMP</strong>.</p>
<p><img class="alignnone size-full wp-image-1035" title="SQL DB2" src="http://techiedan.com/wp-content/uploads/2011/02/sql-db2.jpg" alt="SQL DB2" width="226" height="199" /></p>
<p>Using <strong>DB2 as the platform</strong> to perform my SQL queries, this is how a TIMESTAMP could be updated from the previous TIMESTAMP.</p>
<pre class="brush: sql; title: ; notranslate">UPDATE [tablename] SET DATE_UPDATED = (CURRENT TIMESTAMP);</pre>
<p>Make sure to always use <strong>CURRENT TIMESTAMP</strong> in order to get the latest date and time. Now you can track when was the last transaction done. This only applies for developers that are still using native SQL to update their database. <strong>Those using <a title="Hibernate" href="http://www.hibernate.org" target="_blank">Hibernate</a></strong>would not have a big problem with this.</p>
]]></content:encoded>
			<wfw:commentRss>http://techiedan.com/2011/02/10/track-records-database-db2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Top 10 Results using SQL on DB2</title>
		<link>http://techiedan.com/2011/01/19/top-10-results-using-sql-db2/</link>
		<comments>http://techiedan.com/2011/01/19/top-10-results-using-sql-db2/#comments</comments>
		<pubDate>Wed, 19 Jan 2011 04:38:11 +0000</pubDate>
		<dc:creator>techieDan</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[DB2]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://techiedan.com/?p=1004</guid>
		<description><![CDATA[Well, you can have the all common SQL, but it works differently for most of the DB systems. We have different syntaxes for MS-SQL, mySQL and the DB2. There are many more. Though we will cover the DB2 today]]></description>
			<content:encoded><![CDATA[<p>Most developers would have problem trying to find the correct syntax for their SQL on different responding machines. This is where we see how are we going to do some <strong>SQL on the DB2</strong>. Some users are so acclimatized with mySQL that they are so used to having this syntax in order to get the first top 10 records.</p>
<pre class="brush: sql; title: ; notranslate">SELECT * FROM [tablename] LIMIT 10</pre>
<p>The above SQL statement was just an excerpt from my <a title="MYSQL and MSSQL Syntax" href="http://techiedan.com/2008/02/13/mysql-mssql-syntax/" target="_blank">previous blog post here</a>.</p>
<p>The above few statements are all short SQL statements. Wait till you see how we can do that on DB2.</p>
<p><img class="alignnone size-full wp-image-1006" title="DB2 SQL" src="http://techiedan.com/wp-content/uploads/2011/01/db2.jpg" alt="DB2 SQL" width="421" height="348" /></p>
<pre class="brush: sql; title: ; notranslate">SELECT * FROM [tablename] FETCH FIRST 10 ROWS ONLY</pre>
<p>Simple yet efficient. Supposedly one wants to get the last 10 rows? How is it done then?</p>
<pre class="brush: sql; title: ; notranslate">SELECT * FROM [tablename] WHERE CTRY = 'MY' ORDER BY ID DESC FETCH FIRST 10 ROWS ONLY</pre>
<p>As for added bonus, I have even included the above WHERE inside the statement. Finally, you will now be able to fetch the latest 10 records from [tablename] with country code &#8216;<strong>MY</strong>&#8216;.</p>
]]></content:encoded>
			<wfw:commentRss>http://techiedan.com/2011/01/19/top-10-results-using-sql-db2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Alter Table in Aqua Data Studio 4.7</title>
		<link>http://techiedan.com/2011/01/11/alter-table-in-aqua-data-studio-4-7/</link>
		<comments>http://techiedan.com/2011/01/11/alter-table-in-aqua-data-studio-4-7/#comments</comments>
		<pubDate>Tue, 11 Jan 2011 09:03:22 +0000</pubDate>
		<dc:creator>techieDan</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Alter]]></category>
		<category><![CDATA[DB2]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://techiedan.com/?p=999</guid>
		<description><![CDATA[Unable to alter the table in a database? Might have been a simple mistake from typing something unnecessary. It's just that simple.]]></description>
			<content:encoded><![CDATA[<p>While updating the table in <strong>Aqua Data Studio 4.7</strong> for a <strong>DB2 database</strong>, TechieDan faced an error which doesn&#8217;t allow him to proceed.</p>
<p>This was the SQL Script written.</p>
<blockquote><p><strong><em><span style="color: #0000ff;">ALTER TABLE &#8220;REF&#8221;.&#8221;CARD_BIN_SEGMENT&#8221;<br />
ADD COLUMN &#8220;CARD_TYPE_CD&#8221; VARCHAR(3);</span></em></strong></p></blockquote>
<p>Then there was an error which shows this code.</p>
<blockquote><p><span style="color: #ff0000;"><strong><em>DB2 SQL error: SQLCODE: -104, SQLSTATE: 42601, SQLERRMC: ;;_TYPE_CD&#8221; VARCHAR(3);END-OF-STATEMENT<br />
Message: An unexpected token &#8220;&#8221; was found following &#8220;&#8221;.  Expected tokens may include:  &#8220;_TYPE_CD&#8221; VARCHAR(3)&#8221;</em></strong></span></p></blockquote>
<p>Looking through the web didn&#8217;t bring much help at all. Thus he looked upon as to what is wrong with the <strong>SQL syntax</strong>. Perhaps it was a typo or some escape key being typed on it.</p>
<p><img class="alignnone size-full wp-image-1001" title="HeadAche Using SQL" src="http://techiedan.com/wp-content/uploads/2011/01/headachesql.jpg" alt="HeadAche Using SQL" width="252" height="207" /></p>
<p>It had been a headache for TechieDan but alas, the solution was found after much tinkering. It was actually in fact a simple solution. I just have to take out that <strong>annoying little semicolon</strong>.</p>
<p><strong>PROBLEM SOLVED!</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://techiedan.com/2011/01/11/alter-table-in-aqua-data-studio-4-7/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Hibernate Error : exception setting property value</title>
		<link>http://techiedan.com/2009/12/17/hibernate-error-exception-setting-property-value/</link>
		<comments>http://techiedan.com/2009/12/17/hibernate-error-exception-setting-property-value/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 07:25:38 +0000</pubDate>
		<dc:creator>techieDan</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://techiedan.com/?p=709</guid>
		<description><![CDATA[<p>Been playing around with <a title="Hibernate Mapping Site" href="http://www.hibernate.org" target="_blank">Hibernate</a> and mapping classes with Java. A reason why Hibernate is useful is so to facilitate users to stop writing pure SQL for queries and reduce code redundancy. A slight note, Hibernate also works well with the <strong>.NET platform</strong>.</p>
<p></p>
<p>Though there was this error which TechieDan experienced</p>
<p><em>org.springframework.orm.hibernate3.HibernateSystemException: [...]]]></description>
			<content:encoded><![CDATA[<p>Been playing around with <a title="Hibernate Mapping Site" href="http://www.hibernate.org" target="_blank">Hibernate</a> and mapping classes with Java. A reason why Hibernate is useful is so to facilitate users to stop writing pure SQL for queries and reduce code redundancy. A slight note, Hibernate also works well with the <strong>.NET platform</strong>.</p>
<p><img class="alignnone size-full wp-image-712" title="Hibernate Mapping Error" src="http://techiedan.com/wp-content/uploads/2009/12/error-hibernate.jpg" alt="Hibernate Mapping Error" width="305" height="293" /></p>
<p>Though there was this error which TechieDan experienced</p>
<blockquote><p><span style="color: #ff0000;"><em>org.springframework.orm.hibernate3.HibernateSystemException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of com.tm.ccpm.biz.domain.product.ProductSubCategory.setIdelivery;</em></span></p></blockquote>
<p>Surprisingly the mistake wasn&#8217;t software based. After looking through Hibernate.org websites and other sites for help, it was because of a simple mistake. While <strong>creating the field of IDELIVERY</strong> into the table, the default value was <span style="color: #0000ff;"><strong>null</strong></span>, and considering that the <strong>IDELIVERY column is integer</strong>, it should be defaulted to a default value. So since the column was created earlier with a <strong>null value</strong>, all that was needed to be done is to update the data in the table to repair this error.</p>
<p>Here&#8217;s what I did and it finally is running once again.</p>
<blockquote><p>update TABLENAME set IDELIVERY = 0;</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://techiedan.com/2009/12/17/hibernate-error-exception-setting-property-value/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Java Why PreparedStament not Statement</title>
		<link>http://techiedan.com/2009/03/17/java-why-preparedstament-not-statement/</link>
		<comments>http://techiedan.com/2009/03/17/java-why-preparedstament-not-statement/#comments</comments>
		<pubDate>Tue, 17 Mar 2009 01:45:04 +0000</pubDate>
		<dc:creator>techieDan</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://techiedan.com/?p=382</guid>
		<description><![CDATA[a programmer uses PreparedStatement instead of Statement when doing SQL queries in my projects. I found out that I can indelibly hacked into the system when I use SQL injection]]></description>
			<content:encoded><![CDATA[<p>This is a story of why is it that me, a programmer uses <strong>PreparedStatement </strong>instead of <strong>Statement</strong> when doing SQL queries in my projects.</p>
<p>After what seemed to be a so called secure connection to my database and login system, written by a so called 3rd party, I found out that I can indelibly hack into the system when I use <strong>SQL injection</strong>. Huh? What&#8217;s this? You see, all I need to know at times is your username and I can find ways to login.</p>
<p>But that&#8217;s just logging in, what about dropping your table. Here&#8217;s an example of why</p>
<blockquote>
<pre class="jive-pre"><code class="jive-code jive-java"><span style="color: navy;"><strong>public</strong></span> List processUser(String username, String password)
   <span style="color: navy;"><strong>throws</strong></span> SQLException
<span style="color: navy;">{</span>
   String query = <span style="color: red;">"SELECT * FROM userTbl WHERE id = '"</span> + username + <span style="color: red;">
"' and pass= '"</span> + password +</code><code class="jive-code jive-java"><span style="color: red;"> "'"</span></code><code class="jive-code jive-java">;
   ResultSet rs = this.connection.executeQuery(query);
   <span style="color: darkgreen;">// ... process results ...</span>
<span style="color: navy;">}</span>
</code></pre>
</blockquote>
<p>The above seems very innocently innocent. Now imagining a login page. The user enters something weird. For example in password field he/she does this.</p>
<blockquote><p><span style="color: #3366ff;">abc&#8217; or a = &#8216;a</span></p></blockquote>
<p>Now how would it look like.</p>
<blockquote>
<pre class="jive-pre"><code class="jive-code jive-java">String query = <span style="color: red;">"SELECT * FROM userTbl WHERE id = '"</span> + username +
<span style="color: red;">"' and pass= '"</span> + </code><span style="color: #3366ff;">abc' or a = 'a </span><code class="jive-code jive-java">+</code><code class="jive-code jive-java"><span style="color: red;"> "'"</span></code><code class="jive-code jive-java">;

</code></pre>
</blockquote>
<p>Now can you finally see the dangerousness of using Statement. The difference in using PreparedStatement I can now control what the user enters. To see some of my previous SQL queries using PreparedStatement refer <a title="SQL PreparedStatement" href="http://techiedan.com/2008/03/15/writing-sql-statement-with-loop-objects/" target="_blank">this previous post about SQL</a>.</p>
<p>So always be aware of the security of your codes. Hope this bring some enlightenment to the programmers out there.</p>
]]></content:encoded>
			<wfw:commentRss>http://techiedan.com/2009/03/17/java-why-preparedstament-not-statement/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sample HQL Query</title>
		<link>http://techiedan.com/2009/01/21/sample-hql-query/</link>
		<comments>http://techiedan.com/2009/01/21/sample-hql-query/#comments</comments>
		<pubDate>Wed, 21 Jan 2009 07:23:40 +0000</pubDate>
		<dc:creator>techieDan</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[hql]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://techiedan.com/?p=275</guid>
		<description><![CDATA[<p>For those Java like programmers, they will be using pure SQL statements to get the whole of a table by typing the typical SQL query statement.</p>
String SQL = "select * from tablename where condition = ?";
<p>From then onwards it can be passed to a POJO to be declared but everything has to be done one [...]]]></description>
			<content:encoded><![CDATA[<p>For those Java like programmers, they will be using pure SQL statements to get the whole of a table by typing the typical SQL query statement.</p>
<pre style="background-color:#aaaacc">String SQL = "select * from tablename where condition = ?";</pre>
<p>From then onwards it can be passed to a POJO to be declared but everything has to be done one by one where each POJO object is set one by one. I have also written a <a title="SQL Statement with Loop" href="http://techiedan.com/2008/03/15/writing-sql-statement-with-loop-objects/" target="_blank">simple complex SQL call in Java previously</a> here. Now what if you had a hibernate mode setup, it&#8217;ll be just one time setup and you&#8217;ll get it redirect to the POJO it was meant to be.</p>
<p>Though this time, I&#8217;ll be talking about how HQL Query is somewhat similar to the above SQL query.</p>
<pre style="background-color:#aaaacc">String SQL = " from tableName where condition = ?";
<span class="postbody">HibernateTemplate hibernate =
new HibernateTemplate(this.getSessionFactory());
List list = hibernate.find(SQL,"condition");</span></pre>
<p>Now in just 2 lines I can call a SQL query in a simple HQL Query. Now how simple is Hibernate, if you would like to know more about hibernate, visit hibernate.org to learn about it&#8217;s framework.</p>
]]></content:encoded>
			<wfw:commentRss>http://techiedan.com/2009/01/21/sample-hql-query/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Writing SQL statement with Loop Objects</title>
		<link>http://techiedan.com/2008/03/15/writing-sql-statement-with-loop-objects/</link>
		<comments>http://techiedan.com/2008/03/15/writing-sql-statement-with-loop-objects/#comments</comments>
		<pubDate>Fri, 14 Mar 2008 16:19:36 +0000</pubDate>
		<dc:creator>techieDan</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://techiedan.com/2008/03/15/writing-sql-statement-with-loop-objects/</guid>
		<description><![CDATA[<p>Coding have never been the same!!! Some bad habits when writing codes which are redundant</p>
String[] special = {"a1","a2","a3","a4"};  

Connection con = null;  

PreparedStatement stmt = null;  

PreparedStatement stmt2 = null;  

ResultSet rs = null;  

ResultSet rs2 = null;  

Class.forName ("com.mysql.jdbc.Driver")  

conn = DriverManager.getConnection (url, userName, password);  

String [...]]]></description>
			<content:encoded><![CDATA[<p>Coding have never been the same!!! Some bad habits when writing codes which are redundant</p>
<pre width="50%">String[] special = {"a1","a2","a3","a4"};  

Connection con = null;  

PreparedStatement stmt = null;  

PreparedStatement stmt2 = null;  

ResultSet rs = null;  

ResultSet rs2 = null;  

Class.forName ("com.mysql.jdbc.Driver")  

conn = DriverManager.getConnection (url, userName, password);  

String SQL_QUERY = " SELECT count(*) FROM tblABC WHERE " +
" name_tennis LIKE = ? ";  

String SQL_QUERY2 = " SELECT count(*) FROM tblABC WHERE " +
" name_swimwear LIKE = ? ";  

stmt = con.prepareStatement(SQL_QUERY);  

stmt2 = con.prepareStatement(SQL_QUERY2);  

for (int i=0;i&lt;special[i].length;i++) {<special.length;i++)></special.length;i++)>  

      stmt.setString(1,special[i]);  

      rs = stmt.executeQuery();  

      if (rs.next()) {  

         /* More work here */  

      }  

}  

for (int i=0;i<special.length;i++)></special.length;i++)>i&lt;special[i].length;i++) {  

stmt2.setString(1,special[i]);  

      rs2 = stmt2.executeQuery();  

      if (rs2.next()) {  

         /* More work here */  

      }  

}</pre>
<p>The above is a very bad example of coding. It might work, and run smoothly but it&#8217;s really bad coding. What one should do is to not to let the code be redundant. For example, put both statements into one the loop.</p>
<pre width="50%">
for (int i=0;i&lt;special[i].length;i++) {  

      stmt.setString(1,special[i]);  

      rs = stmt.executeQuery();  

      if (rs.next()) {  

         /* More work here */  

      }      stmt2.setString(1,special[i]);  

      rs2 = stmt2.executeQuery();  

      if (rs2.next()) {  

         /* More work here */  

      }  

}</pre>
<p>What&#8217;s the use of this? Well, iif you have an object or rather a Bean to set into, you don&#8217;t have to loop and reloop. All can be done in the same loop. Well, hope this brings about some bad habits of coding java.</p>
]]></content:encoded>
			<wfw:commentRss>http://techiedan.com/2008/03/15/writing-sql-statement-with-loop-objects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>mySQL msSQL syntax</title>
		<link>http://techiedan.com/2008/02/13/mysql-mssql-syntax/</link>
		<comments>http://techiedan.com/2008/02/13/mysql-mssql-syntax/#comments</comments>
		<pubDate>Tue, 12 Feb 2008 16:39:17 +0000</pubDate>
		<dc:creator>techieDan</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[mssql]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://techiedan.com/2008/02/13/mysql-mssql-syntax/</guid>
		<description><![CDATA[<p><strong>mySQL</strong> and <strong>msSQL</strong> has a similar SQL syntax but not the same. To have a grasp of what does this mean, let&#8217;s have an exercise to <strong>get 10 rows from a table</strong> with mySQL.</p>
<p>select * from [tablename] limit 10</p>
<p>Now if you were to run this sql query above on msSQL server you&#8217;ll either get a wrong sql [...]]]></description>
			<content:encoded><![CDATA[<p><strong>mySQL</strong> and <strong>msSQL</strong> has a similar SQL syntax but not the same. To have a grasp of what does this mean, let&#8217;s have an exercise to <strong>get 10 rows from a table</strong> with mySQL.</p>
<blockquote style="background-color: #ffefd5"><p>select * from [tablename] limit 10</p></blockquote>
<p>Now if you were to run this sql query above on msSQL server you&#8217;ll either get a wrong sql query or query not able to compute. So alternative for msSQL is by entering this query instead.</p>
<blockquote style="background-color: #ffefd5"><p>select top 10 * from [tablename]</p></blockquote>
<p>Simple?? Yes, that&#8217;s all there is to it. To those that need further questions answered, there are bound to be helpful people posting or me trying to help people out.</p>
<blockquote></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://techiedan.com/2008/02/13/mysql-mssql-syntax/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

