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)
 read the csv in a cursor instead of bulk update

Author  Topic 

smithsf22
Starting Member

9 Posts

Posted - 2008-02-13 : 18:14:48
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 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?

Thanks

talleyrand
Starting Member

35 Posts

Posted - 2008-02-13 : 18:26:11
Bulk insert is really fast for putting the data into a table but that's about the extent of the logic capabilities. Do you have the ability to push it into a staging table, OrdersStaging and then compare staging to the OrdersBulk table with some TSQL?
Go to Top of Page

smithsf22
Starting Member

9 Posts

Posted - 2008-02-14 : 09:52:30
That is what i will do... Thanks for the info
Go to Top of Page
   

- Advertisement -