Sunday, August 30, 2009

Joins - Joining a table to itself

This is a quick sql query that you can use to join a table to itself. There are a lot of joins, the most common is used to join two related tables together, but the LEFT JOIN can be a really quick alternative to a Sub query.

I used something similar to populate a treeview that had child and parent nodes. Effectively each parent had muliple children, and I used a sql query to check the data that I wanted (I will turn this into a LINQ statement in code, rather than a stored procedure)

use category
select c.ParentID, c.ChildID, c.Description
from category c
left join category p on c.ChildID = p.ParentID and p.ChildID = c.ParentID
order by c.ParentID

More info here: http://www.w3schools.com/sql/sql_join_left.asp

Sunday, August 23, 2009

Importing an Access database into SQL Server

I have an old application I want to rewrite. It uses an Access database and considering there's more about a 1000 records in it, I am not about to manually re-add the data. As part of the upgrade I wanted to work with SQL Server instead. So what's a quick way to upgrade?

I have Access & Microsoft SQL Server 2008 installed. I have a blank database on SQL server set up to import the data into.

  1. Go to Start > Microsoft SQL Server 2008> Import and Export Data. This begins a simple Wizard process
  2. Select the source that you want to import from (in this case Access) & browse to the location of the Access database.

    Set any options like passwords. I'm doing a straight conversion into a blank database. I don't need to do any special queries, but you could specify which tables the data goes into, which tables you want to import and which ones you don't.
  3. Click the ok button :)

If everything goes well, when you hook into SQL Server, your data will all be there!

More info from MSDN: http://msdn.microsoft.com/en-us/library/ms188032.aspx