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
 Read a file and insert rows into a table

Author  Topic 

jjs1
Starting Member

1 Post

Posted - 2011-09-16 : 12:35:41
This is my first exposure to Sql Server.

I need to read a file and insert the contents of the file into a SQL Server Table. However, I can not just do a bulk insert. I need to use the Employee Number in the file to retrieve the Employee ID from a SQL Server Table. Then I need to insert the records of the file into the table with the Employee ID as one of the fields.

For example, the Text File contains an Employee Number of 123456. I need to search a SQL Server Table(lets name it TABLEA) and retrieve the EMPLOYEE ID from it. The EMPLOYEE table contains EMPLOYEE NUMBER and EMPLOYEE ID. I need the EMPLOYEE ID from the EMPLOYEE table because I need to insert the records in the Text File into another SQL Server Table(lets name it TABLEB). TABLEB requires the EMPLOYEE ID, not the EMPLOYEE NUMBER.

Any help and code example would be appreciated. Thanks.

ehorn
Master Smack Fu Yak Hacker

1632 Posts

Posted - 2011-09-16 : 12:42:56
Hello,

There are many ways to perform these types of tasks.

One method would be to create a multi-step job.

The first step would be to bulk insert the file into a working table. This is a table specifically established to receive the data from the file.

Then, once this step is complete, you can execute a query which matches the data in this working table with the relevant data in your base tables to find the matching criteria required for the insert.

HTH.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-09-16 : 12:48:20
That's what I would do, and not 1 row at a time. I would do it in a set based operation

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -