<?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>Grapii &#187; SQL</title>
	<atom:link href="http://www.grapii.com/tag/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.grapii.com</link>
	<description>Personal Site of Raj Patel</description>
	<lastBuildDate>Thu, 08 Jul 2010 15:09:12 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>List Table/Column names in SQL database</title>
		<link>http://www.grapii.com/2008/03/list-tablecolumn-names-in-sql-database/</link>
		<comments>http://www.grapii.com/2008/03/list-tablecolumn-names-in-sql-database/#comments</comments>
		<pubDate>Mon, 17 Mar 2008 12:50:26 +0000</pubDate>
		<dc:creator>grapii</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.grapii.com/index.php/2008/03/17/list-tablecolumn-names-in-sql-database/</guid>
		<description><![CDATA[Microsoft SQL Server 2000 provides a method for obtaining meta data using information schema views.
List all user tables in the pubs database, we&#8217;re not interested in views
SELECT *
FROM information_schema.tables
WHERE table_type = 'base table'
List only the table names
SELECT table_name
FROM information_schema.tables
WHERE table_type = 'base table'
List all columns in the authors table
SELECT *
FROM information_schema.columns
WHERE table_name = 'authors'
List only [...]]]></description>
			<content:encoded><![CDATA[<p>Microsoft SQL Server 2000 provides a method for obtaining meta data using information schema views.<span id="more-93"></span></p>
<p>List <strong>all</strong> user tables in the <em>pubs</em> database, we&#8217;re not interested in views</p>
<pre>SELECT *
FROM information_schema.tables
WHERE table_type = 'base table'</pre>
<p>List <strong>only</strong> the table names</p>
<pre>SELECT table_name
FROM information_schema.tables
WHERE table_type = 'base table'</pre>
<p>List <strong>all</strong> columns in the <em>authors</em> table</p>
<pre>SELECT *
FROM information_schema.columns
WHERE table_name = 'authors'</pre>
<p>List <strong>only</strong> the column names</p>
<pre>SELECT column_name
FROM information_schema.columns
WHERE table_name = 'authors'</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.grapii.com/2008/03/list-tablecolumn-names-in-sql-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reducing the Risk of SQL Injection Attack</title>
		<link>http://www.grapii.com/2008/01/reducing-the-risk-of-sql-injection-attack/</link>
		<comments>http://www.grapii.com/2008/01/reducing-the-risk-of-sql-injection-attack/#comments</comments>
		<pubDate>Fri, 11 Jan 2008 13:18:47 +0000</pubDate>
		<dc:creator>grapii</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[ASP]]></category>
		<category><![CDATA[Forms]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.grapii.com/?p=7</guid>
		<description><![CDATA[Databases can be compromised if they are open to SQL Injection Attack. Stripping invalid characters from form inputs will reduce this risk.
If you have a form on your site that interacts with a database (e.g. a username/password login form), you should secure the form by adding an additional stage between submission and the database look-up. [...]]]></description>
			<content:encoded><![CDATA[<p class="note">Databases can be compromised if they are open to SQL Injection Attack. Stripping invalid characters from form inputs will reduce this risk.</p>
<p>If you have a form on your site that interacts with a database (e.g. a username/password login form), you should secure the form by adding an additional stage between submission and the database look-up. One way to do this is to check for valid content.<span id="more-7"></span></p>
<p>As usenames and passwords are usually strings of alphanumeric characters, you can strip out &#8216;bad&#8217; characters from the input string.<br />
The easiest way to do this is to collect the form&#8217;s input and check each character against a regular expression, removing any that are invalid.</p>
<p>The code below removes all non-alphanumeric characters from the input string:</p>
<pre>&lt;%
'gets the text submitted via a form
Dim strUsername, strPassword
strUsername = Request.Form("username")
strPassword = Request.Form("password")

'call the function to use
strUsername = stripString(strUsername)
strPassword = stripString(strPassword)

'function to strip all non-alphnumric characters
function stripString(strInput)
 Dim objRE
 Set objRE = New RegExp
 With objRE
  .Pattern = "[^A-Za-z0-9]"
  .Global = True
 End With
 stripChars = objRE.Replace(strInput, "")
 Set objRE = nothing
End Function
%&gt;</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.grapii.com/2008/01/reducing-the-risk-of-sql-injection-attack/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using ADO and SQL with ASP</title>
		<link>http://www.grapii.com/2008/01/using-ado-and-sql-with-asp/</link>
		<comments>http://www.grapii.com/2008/01/using-ado-and-sql-with-asp/#comments</comments>
		<pubDate>Sat, 05 Jan 2008 10:15:41 +0000</pubDate>
		<dc:creator>grapii</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[ADO]]></category>
		<category><![CDATA[ASP]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.grapii.com/?p=19</guid>
		<description><![CDATA[This article illustrates how you can easily connect to and manipulate SQL databases using ActiveX Data Objects (ADO) with Active Server Pages (ASP)

Connection
To access a database we first need to open a connection to it, which involves creating an ADO Connection object.
We then specify the connection string and call the Connection object&#8217;s Open method.
I tend [...]]]></description>
			<content:encoded><![CDATA[<p>This article illustrates how you can easily connect to and manipulate SQL databases using ActiveX Data Objects (ADO) with Active Server Pages (ASP)<br />
<span id="more-19"></span></p>
<h3>Connection</h3>
<p>To access a database we first need to open a connection to it, which involves creating an ADO Connection object.</p>
<p>We then specify the connection string and call the Connection object&#8217;s <a title="Open" href="http://www.w3schools.com/ado/met_conn_open.asp">Open</a> method.</p>
<p>I tend to create a seperate file called OpenDataConnection.asp and include this file with every page that needs a database connection.</p>
<pre>Dim SQLConnection
Set SQLConnection = Server.CreateObject("ADODB.Connection")
SQLConnection.ConnectionString = "Provider=SQLOLEDB; Data Source=localhost; Initial Catalog=Northwind; UID=BlogIT; PWD=BlotIT"
SQLConnection.Open</pre>
<h3>Disconnection</h3>
<p>Disconnecting from the database is very important as it releases any resources tied to it, Use the objects <a title="Close" href="http://www.w3schools.com/ado/met_conn_close.asp">Close</a> method at the end of your code, or alternativly create a file called CloseDataConnection.asp and include it as a footer at the end of each file that has OpenDataConnection.asp included.</p>
<pre>SQLConnection.Close
set SQLConnection=nothing</pre>
<h3>Retrieve Data</h3>
<p>One of the most common tasks in ASP web application is the retrieval of data from a database. This is achieved via the ADO RecordSet object. Using this objects <a title="Open" href="http://www.w3schools.com/ado/met_rs_open.asp">Open</a> method we can pass in any SQL string that our database driver supports.</p>
<p>Here we will retrieve the contents of the Customer table from the Northwind database, and place them in an array.</p>
<pre>&lt;%
Dim rsCustomer, strSQL, intCustomerCount, arrCustomerDataSet rsCustomer = Server.CreateObject("ADODB.RecordSet")
rsCustomer.CursorLocation = 3
strSQL = "SELECT * FROM Customers "
rsCustomer.Open strSQL, SQLConnection
if not (rsCustomer.BOF or rsCustomer.EOF) then
 intCustomerCount = rsCustomer.RecordCount
 arrCustomerData = rsCustomer.GetRows()
else
 intCustomerCount = 0
end if
rsCustomer.Close
set rsCustomer = nothing
%&gt;</pre>
<p>Now that we have the data in our array arrCustomerData, we can simply loop through this dataset and display on screen</p>
<pre>&lt;%
Dim intCustomerLoop
intCustomerLoop = 0
if intCustomerCount &lt;&gt; 0 then
 do until intCustomerLoop = intCustomerCount
  response.write(arrCustomerData(0, intCustomerLoop))
  response.write(arrCustomerData(1, intCustomerLoop))
  response.write(arrCustomerData(2, intCustomerLoop))
  response.write(arrCustomerData(3, intCustomerLoop))
  intCustomerLoop = intCustomerLoop +1
 loop
end if
%&gt;</pre>
<h3>Manipulate Data</h3>
<p>In order to use &#8220;INSERT INTO&#8221;, &#8220;UPDATE&#8221; or &#8220;DELETE&#8221; SQL statements, we need to amend the above RecordSet object to allow updates. This is achieved by using two properties of the RecordSet object, <a title="CursorType" href="http://www.w3schools.com/ado/prop_rs_cursortype.asp">CursorType</a> and <a title="LockType" href="http://www.w3schools.com/ado/prop_rs_locktype.asp">LockType</a></p>
<h4>Insert</h4>
<pre>&lt;%
 Dim rsCustomer, strSQLSet rsCustomer = Server.CreateObject("ADODB.RecordSet")
strSQL = "INSERT INTO Customers(CompanyName, ContactName, ContactTitle) VALUES('BlogIT','CodeMonkey','Administrator')"
rsCustomer.CursorType = 2
rsCustomer.LockType =3
rsCustomer.Open strSQL, SQLConnection
set rsCustomer = nothing
%&gt;</pre>
<h4>Update</h4>
<pre>&lt;%
Dim rsCustomer, strSQLSet rsCustomer = Server.CreateObject("ADODB.RecordSet")
strSQL = "UPDATE Customers SET ContactName='CodeMonkey' WHERE CustomerID='ALFKI'"
rsCustomer.CursorType = 2
rsCustomer.LockType =3
rsCustomer.Open strSQL, SQLConnection
set rsCustomer = nothing
%&gt;</pre>
<h4>Delete</h4>
<pre>&lt;%
Dim rsCustomer, strSQLSet rsCustomer = Server.CreateObject("ADODB.RecordSet")
strSQL = "DELETE FROM Customers WHERE CustomerID='ALFKI'"
rsCustomer.CursorType = 2
rsCustomer.LockType =3
rsCustomer.Open strSQL, SQLConnection
set rsCustomer = nothing
%&gt;</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.grapii.com/2008/01/using-ado-and-sql-with-asp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Encrypting Usernames and Password</title>
		<link>http://www.grapii.com/2007/12/encrypting-usernames-and-password/</link>
		<comments>http://www.grapii.com/2007/12/encrypting-usernames-and-password/#comments</comments>
		<pubDate>Sat, 15 Dec 2007 13:15:59 +0000</pubDate>
		<dc:creator>grapii</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[ASP]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.grapii.com/?p=12</guid>
		<description><![CDATA[Using the MD5 and SHA1 hash functions
If you&#8217;re using a login script on your site you probably store usernames and passwords in a database for authenticating the login.
For security reasons, you should never store these as plain text but should encrypt them with a one-way hash function such as md5 or sha1.

As neither of these [...]]]></description>
			<content:encoded><![CDATA[<p class="note">Using the MD5 and SHA1 hash functions</p>
<p>If you&#8217;re using a login script on your site you probably store usernames and passwords in a database for authenticating the login.</p>
<p>For security reasons, you should never store these as plain text but should encrypt them with a one-way hash function such as md5 or sha1.<br />
<span id="more-12"></span></p>
<p>As neither of these funtions are included with ASP, you&#8217;ll need to download and unzip the hash function you want to use and upload it to your webspace.</p>
<p>To use the functions, include the file in the pages you want to use hashing.</p>
<pre>&lt;!--#include file="md5.asp"--&gt;
or
&lt;!--#include file="sha1.asp"--&gt;</pre>
<p>Then you simply call the function with either:</p>
<pre>&lt;% MD5("string") or SHA1("string") %&gt;</pre>
<p>For example, if you wanted to encrypt a Username and Password on a signup form, you would collect the Username and Password from the submitted form, hash them and then insert the hashed values into your database</p>
<pre>&lt;%
strHashedUsername = MD5(Request.Form("Username"))
strHashedPassword = MD5(Request.Form("Password"))
%&gt;</pre>
<p>To authenticate a user who is attempting to sign in, Hash the username and Password from the form and compare these with the strHashedUsername and strHashedPassword stored in your database.</p>
<p>If a user forgets their password you&#8217;ll need to generate a new, pseudo-random password for the user as hashing is one-way can&#8217;t be unencrypted.</p>
<p><small>Downloads: <a title="MD5" href="http://resource.grapii.com/md5.zip">md5.zip</a> <a title="SHA1" href="http://resource.grapii.com/sha1.zip">sha1.zip</a></small></p>
]]></content:encoded>
			<wfw:commentRss>http://www.grapii.com/2007/12/encrypting-usernames-and-password/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Finding duplicate entries in SQL databases</title>
		<link>http://www.grapii.com/2007/12/finding-duplicate-entries-in-sql-databases/</link>
		<comments>http://www.grapii.com/2007/12/finding-duplicate-entries-in-sql-databases/#comments</comments>
		<pubDate>Wed, 12 Dec 2007 14:27:21 +0000</pubDate>
		<dc:creator>grapii</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.grapii.com/?p=13</guid>
		<description><![CDATA[Here&#8217;s a handy query for finding duplicates in a table. Suppose you want to find all email addresses in a table that exist more than once:
SELECT email, COUNT(email) AS NumOccurrences
FROM users
GROUP BY email
HAVING ( COUNT(email) &#62; 1 )
You could also use this technique to find rows that occur exactly once:
SELECT email
FROM users
GROUP BY email
HAVING ( [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a handy query for finding duplicates in a table. <span id="more-13"></span>Suppose you want to find all email addresses in a table that exist more than once:</p>
<pre>SELECT email, COUNT(email) AS NumOccurrences
FROM users
GROUP BY email
HAVING ( COUNT(email) &gt; 1 )</pre>
<p>You could also use this technique to find rows that occur exactly once:</p>
<pre>SELECT email
FROM users
GROUP BY email
HAVING ( COUNT(email) = 1 )</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.grapii.com/2007/12/finding-duplicate-entries-in-sql-databases/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL Joins &#8211; Overview</title>
		<link>http://www.grapii.com/2007/12/sql-joins-overview/</link>
		<comments>http://www.grapii.com/2007/12/sql-joins-overview/#comments</comments>
		<pubDate>Fri, 07 Dec 2007 16:31:38 +0000</pubDate>
		<dc:creator>grapii</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.grapii.com/?p=14</guid>
		<description><![CDATA[I&#8217;m not always working with SQL Server, and sometimes keep forgetting which SQL join to use, this article keeps me refreshed and hopefully give you an overview of the three main join types: Inner, Left and Right.


Inner Join
The INNER JOIN returns all rows from both tables where there is a match. If there are rows [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m not always working with SQL Server, and sometimes keep forgetting which SQL join to use, this article keeps me refreshed and hopefully give you an overview of the three main join types: Inner, Left and Right.</p>
<p><span id="more-14"></span><br />
<img src="http://www.grapii.com/wp-content/uploads/2008/02/sqljoin.gif" alt="SQL Joins" /></p>
<h2>Inner Join</h2>
<p>The INNER JOIN returns all rows from both tables where there is a match. If there are rows in Employees that do not have matches in Orders, those rows will not be listed.</p>
<h3>Example</h3>
<p>Who has ordered a product, and what did they order?</p>
<pre>SELECT Employees.Name, Orders.Product
FROM Employees
INNER JOIN Orders
ON Employees.Employee_ID=Orders.Employee_ID</pre>
<h3>Result</h3>
<table cellpading="0" border="0" width="80%" cellSpacing="0" class="tableBorder">
<tr>
<th>Name</th>
<th>Product</th>
</tr>
<tr>
<td>Hansen, Ola</td>
<td>Printer</td>
</tr>
<tr>
<td>Svendson, Stephen</td>
<td>Table</td>
</tr>
<tr>
<td>Svendson, Stephen</td>
<td>Chair</td>
</tr>
</table>
<h2>Left Join</h2>
<p>The LEFT JOIN returns all the rows from the first table (Employees), even if there are no matches in the second table (Orders). If there are rows in Employees that do not have matches in Orders, those rows <strong>also</strong> will be listed.</p>
<h3>Example</h3>
<p>List all employees, and their orders &#8211; if any.</p>
<pre>SELECT Employees.Name, Orders.Product
FROM Employees
LEFT JOIN Orders
ON Employees.Employee_ID=Orders.Employee_ID</pre>
<h3>Result</h3>
<table cellpading="0" border="0" width="80%" cellSpacing="0" class="tableBorder">
<tr>
<th>Name</th>
<th>Product</th>
</tr>
<tr>
<td>Hansen, Ola</td>
<td>Printer</td>
</tr>
<tr>
<td>Svendson, Tove</td>
<td> </td>
</tr>
<tr>
<td>Svendson, Stephen</td>
<td>Table</td>
</tr>
<tr>
<td>Svendson, Stephen</td>
<td>Chair</td>
</tr>
<tr>
<td>Pettersen, Kari</td>
<td> </td>
</tr>
</table>
<h2>Right Join</h2>
<p>The RIGHT JOIN returns all the rows from the second table (Orders), even if there are no matches in the first table (Employees).</p>
<h3>Example</h3>
<p>List all employees, and their orders &#8211; if any</p>
<pre>SELECT Employees.Name, Orders.Product
FROM Employees
RIGHT JOIN Orders
ON Employees.Employee_ID=Orders.Employee_ID</pre>
<h3>Result</h3>
<table cellpading="0" border="0" width="80%" cellSpacing="0" class="tableBorder">
<tr>
<th>Name</th>
<th>Product</th>
</tr>
<tr>
<td>Hansen, Ola</td>
<td>Printer</td>
</tr>
<tr>
<td> </td>
<td>Monitor</td>
</tr>
<tr>
<td>Svendson, Stephen</td>
<td>Table</td>
</tr>
<tr>
<td>Svendson, Stephen</td>
<td>Chair</td>
</tr>
</table>
]]></content:encoded>
			<wfw:commentRss>http://www.grapii.com/2007/12/sql-joins-overview/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
