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
 bulk insert works, can update?

Author  Topic 

detlion1643
Yak Posting Veteran

67 Posts

Posted - 2010-04-19 : 16:23:21
Originally I was going line by line through excel files (a vb.net program I worte) and was using if exists update/insert code. Since some excel files are 20,000+ rows it started taking way too long.

This bulk insert is good so far, I got it working on the excel files, only problem is I can't seem to manipulate it to accept the if exists update part...

Since it's not line by line anymore, is updating still possible?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-19 : 16:29:46
There is no BULK update command. I'm not quite clear on what you want do though. Could you show us a data example?

Maybe you need the MERGE command.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

detlion1643
Yak Posting Veteran

67 Posts

Posted - 2010-04-19 : 16:37:53
Sample data:
id,sku,price,stock,etc...
1 , 1 , 350 , 20 , ...
1 , 2 , 300 , 0 , ...

Column2 is actually the pk, so I test for that. What I used to do was run an if exists select * from sku where sku= variableforsku update else insert.
I got the variable from vb.net reading the excel file line for line.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-19 : 16:42:48
What version of SQL Server are you using?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

detlion1643
Yak Posting Veteran

67 Posts

Posted - 2010-04-19 : 16:48:52
SQL express 2008.

I did a little more searching, and it looks like I might have to bulk insert into a 'work' table and then merge them, then delete the 'work' table.

Will keep searching.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-19 : 16:51:46
Yes that's what I was going to recommend if you were using 2008. The "work" table is often referred to as a "staging" table. In case you will be doing lots of imports, just truncate the table rather than dropping it.

Check out merge in BOL.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-19 : 16:53:20
In the case of SQL Server 2005, instead of MERGE you would instead use WHERE NOT EXISTS in the INSERT/SELECT instead of IF.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

detlion1643
Yak Posting Veteran

67 Posts

Posted - 2010-04-19 : 16:59:53
Thanks tkizer,

Didn't think of truncate, good suggestion. Table/code are built - testing day tomorrow!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-19 : 17:07:34
You're welcome, good luck!

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -