Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Importing Data

Author  Topic 

oufan222
Starting Member

6 Posts

Posted - 2010-03-22 : 10:45:36
I'm new to SQL Server, but I have been reading all I can find on the topic for the past few months. The one topic that doesn't appear to be covered in great detail is importing data. I was hoping to get some advice on this topic from users in the forum.

Specifically, I have several tab delimited files for task data that I want to import into a SQL DB table named RECORDS. Each task data record has a string representing the type of the record that is always greater than 10 characters. Since there are only five unique TASK_TYPE strings, I've read that it is better to save the task type field data in another table (named TASK_TYPE) that has two columns: a auto-increment ID and the task type as a string. The auto-inc ID is then used as a foreign key to the task type field in the RECORDS table. This will help considerably with the size of the database.

However, I now don't understand how to update and/or import records that are in the RECORDS table. What is the best way to import and update data in the RECORDS table, realizing the the task_type column in the RECORDS table will really be a "pointer" to a unique string in the TASK_TYPE table?

Thanks in advance for any help!

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-03-22 : 11:13:42
Import your data from file into a staging table.
Do your needed inserts into your task_type table via grouping and where not exists()...
insert from staging table into destination table joining task_type on task_type string and use the id from task_type.



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

oufan222
Starting Member

6 Posts

Posted - 2010-03-22 : 15:14:35
What about updates? Is it the same process?
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-03-22 : 15:17:59
Yes but only to the destination table.
First make an update where exists() then the insert where not exists().
I can't believe the task_type should be updated via import...


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

oufan222
Starting Member

6 Posts

Posted - 2010-03-22 : 16:12:28
The table will not be updated during import, that question was just me thinking ahead.

Thanks for the prompt response.

quote:
Import your data from file into a staging table.
Do your needed inserts into your task_type table via grouping and where not exists()...
insert from staging table into destination table joining task_type on task_type string and use the id from task_type.


I'm reading the text file into a datatable in C#. By "staging table" are you referring to a datatable object? How do I perform the JOIN between two datatables? Is it possible?


Go to Top of Page
   

- Advertisement -