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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Update column from a list

Author  Topic 

erodzen
Starting Member

2 Posts

Posted - 2009-01-24 : 15:24:43
This is probably very very simple for any T-SQL experts, but I have only just begun to learn SQL. I have an Excel spreadsheet which is a list of items that I want to change one column for. The spreadsheet has the PrimaryID of the items. It is a subset of all the items. I can create a CSV file from the spreadsheet. I want to read from that CSV file and update the records that are listed in the CSV file. Can someone write me a quick and easy SQL command to do what I need?

Table is ITPRICE, want to set ITP_SCOMM = 1, for all records which match the PrimaryID in products.csv. (Do I need a header row in the csv file?) Here is what I tried: (am I close?)

UPDATE ITPRICE
SET ITP_SCOMM = 1
FROM
OPENROWSET('MICROSOFT.JET.OLEDB.4.0',
'Text;Database=C:\;',
'SELECT * FROM [products.csv]') AS P
where P.PrimaryID=ITPRICE.PrimaryID

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-25 : 04:50:17
you're close. yup you need header in csv

UPDATE i
SET i.ITP_SCOMM = 1
FROM ITPRICE i
JOIN
(SELECT fields...
FROM OPENROWSET('MICROSOFT.JET.OLEDB.4.0',
'Text;Database=C:\;',
'SELECT * FROM [products.csv]') AS P
)AS R
ON R.PrimaryID=ITPRICE.PrimaryID
Go to Top of Page

erodzen
Starting Member

2 Posts

Posted - 2009-01-26 : 10:48:57
Thanks so much for trying to help me!!!!

Tried this as shown and got errors until I made the following changes. Then it updates every row - does not use the CSV file info at all. What have I done wrong?

(CSV file has one column with header PID)

UPDATE ITPRICE
SET ITP_SCOMM = 1
FROM ITPRICE i
JOIN
(SELECT PID
FROM OPENROWSET('MICROSOFT.JET.OLEDB.4.0',
'Text;Database=C:\;',
'SELECT PID FROM [products.csv]') AS P
)AS R
ON R.PID=ITPRICE.ITP_PrimaryID
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-27 : 00:52:12
[code]
UPDATE i
SET i.ITP_SCOMM = 1
FROM ITPRICE i
JOIN
(SELECT PID
FROM OPENROWSET('MICROSOFT.JET.OLEDB.4.0',
'Text;Database=C:\;',
'SELECT PID FROM [products.csv]') AS P
)AS R
ON R.PID=ITPRICE.ITP_PrimaryID
[/code]
Go to Top of Page
   

- Advertisement -