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
 Import data into Field

Author  Topic 

Wabby
Starting Member

27 Posts

Posted - 2008-04-11 : 08:09:34
Hi,

I am new here, and looking for some help with SQL import.

I am using SQL Server 2000.

Problem: I have a table called 'People', with a field that I wish to populate inside of it (Town).
I already have 500 records in the table.

I have a seperate csv file containing the primary key record (ID) information, and the information that I wish to put into the field (Town).

How can I import the information into the field 'Town' by inserting the correct information next to the ID in the CSV file (There are some that I do not have to import, so I cannot just insert straight in).

Best Regards

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-04-11 : 08:33:45
1 Import data into some staging_table
2 Insert into your_main_table(columns)
select columns from staging_table s
where not exists(select * from your_main_table where id=s.id)

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Wabby
Starting Member

27 Posts

Posted - 2008-04-15 : 08:43:40
So if I had the following setup:

Table1:
Item,Description,Price,Unit

And I wanted to import *some* prices (Item already exists, just without price) into Table1, then I would create a staging table like the following:

Table2:
Item,Price

And then I run the following SQL statement?

INSERT into TABLE1(Price)
SELECT * TABLE2
WHERE not exists(Select * from Table1 where id=s.id)

??

New to SQL so go easy on me please :)
Go to Top of Page

pravin14u
Posting Yak Master

246 Posts

Posted - 2008-04-15 : 08:52:30
1. Import your CSV file into a temp table
2. Update P
set P.Town=T.Town
from People P
inner join temp T
on p.id=t.id


Prakash.P
The secret to creativity is knowing how to hide your sources!
Go to Top of Page

Wabby
Starting Member

27 Posts

Posted - 2008-04-15 : 08:54:01
Cheers, think I got that now :)

Will try and report back :D
Go to Top of Page

Wabby
Starting Member

27 Posts

Posted - 2008-04-15 : 09:31:48
Works a treat pravin14u.

Thanks VERY much :)
Go to Top of Page
   

- Advertisement -