Click to See Complete Forum and Search --> : read the csv in a cursor instead of bulk update


smithsf22
02-13-2008, 06:24 PM
Hello,
I am trying to read in from a csv file which works like this:

DECLARE @doesExist INT
DECLARE @fileName VARCHAR(200)
SET @fileName = 'c:\file.csv'

SET NOCOUNT ON
EXEC xp_fileexist "' + @fileName + '", @doesExist OUTPUT
SET NOCOUNT OFF

IF @doesExist = 1

BEGIN
BULK INSERT OrdersBulk
FROM "' + @fileName + '"
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
END
ELSE
print('Error cant find file')

What I want to do is check another table before each line inserts, if the data already exists I want to do an UPDATE.
I think i can do what i need with a cursor but I think the bulk update just pushes all the data up and will not allow me to put in the cursor.
So is there a way i can read the csv in a cursor instead of using the bulk insert so i can examine each row?
Or is there an even better way to do this?

Thanks

chazzy
02-14-2008, 11:50 AM
I think you already have your solution (use a cursor). In my opinion it would be the best solution.