Importing csv to MS SQL

Please support our MS SQL advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2009
Posts: 3
Reputation: oddvalue is an unknown quantity at this point 
Solved Threads: 0
oddvalue oddvalue is offline Offline
Newbie Poster

Importing csv to MS SQL

 
0
  #1
Jun 16th, 2009
Hi everyone.

I'm relatively new to MS SQL but one of my clients has a site based entirely around it so I am having to get to know it a bit better.

While trying to clean out some of the fake emails from the mail list table they all got deleted.

I have CSV backups of the table, is there any way of importing the CSV into the table with out destroying any new entries into the table?

The table consists of 2 fields, one auto incrementing number and the email address.

The database is on a thirdparty host server and I use SQL Management Studio Express 2005 to manage the database.

Any help at all would be greatly appreciated.

Thanks

Jim
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,187
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Importing csv to MS SQL

 
0
  #2
Jun 16th, 2009
Sure you can. What I would is import the .CSV file to another table then:

  1. SET IDENTITY_INSERT dbo.Email ON
  2.  
  3. INSERT INTO Email (IdentityColumn, EmailColumn)
  4. SELECT IdentityColumn, EmailColumn
  5. FROM NewEmailTable
  6. WHERE
  7. NOT EXISTS
  8. (
  9. SELECT *
  10. FROM Email (NOLOCK)
  11. WHERE Email.IdentityColumn = NewEmailTable.IdentityColumn
  12. )
  13.  
  14.  
  15. SET IDENTITY_INSERT dbo.Email OFF

You can use that to match identities but more than likely depending on if you had activity before/after the delete and whether or not your table was truncated (which would reset the identity seed value) you may have overlapping identity values with different email data. In this case you could use the same query but test to see if the email exists, and not the identity.

I hope this helps and good luck, data recovery is never fun.
Last edited by sknake; Jun 16th, 2009 at 4:02 pm. Reason: typo
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 3
Reputation: oddvalue is an unknown quantity at this point 
Solved Threads: 0
oddvalue oddvalue is offline Offline
Newbie Poster

Re: Importing csv to MS SQL

 
0
  #3
Jun 17th, 2009
Originally Posted by sknake View Post
Sure you can. What I would is import the .CSV file to another table then:
Thanks for your response, it does help but the trouble I am having is getting that first bit done. How do I actually go about importing the CSV?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,187
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Importing csv to MS SQL

 
0
  #4
Jun 17th, 2009
Open Microsoft SQL Server Management Studio, expand the server, expand database, Right click on the database --- Tasks --- Import Data, Click next, for choose data source pick "Flat File", navigate to the file, and away you go
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 3
Reputation: oddvalue is an unknown quantity at this point 
Solved Threads: 0
oddvalue oddvalue is offline Offline
Newbie Poster

Re: Importing csv to MS SQL

 
0
  #5
Jun 18th, 2009
Originally Posted by sknake View Post
Open Microsoft SQL Server Management Studio, expand the server, expand database, Right click on the database --- Tasks --- Import Data, Click next, for choose data source pick "Flat File", navigate to the file, and away you go
I don't have that option in my menus. I'm using Microsoft SQL Server Management Studio Express 2005.

Any other suggestions?
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 44
Reputation: cartman714 can only hope to improve 
Solved Threads: 3
cartman714's Avatar
cartman714 cartman714 is offline Offline
Light Poster

Re: Importing csv to MS SQL

 
0
  #6
Jun 18th, 2009
Originally Posted by sknake View Post
Sure you can. What I would is import the .CSV file to another table then:

  1. SET IDENTITY_INSERT dbo.Email ON
  2.  
  3. INSERT INTO Email (IdentityColumn, EmailColumn)
  4. SELECT IdentityColumn, EmailColumn
  5. FROM NewEmailTable
  6. WHERE
  7. NOT EXISTS
  8. (
  9. SELECT *
  10. FROM Email (NOLOCK)
  11. WHERE Email.IdentityColumn = NewEmailTable.IdentityColumn
  12. )
  13.  
  14.  
  15. SET IDENTITY_INSERT dbo.Email OFF

You can use that to match identities but more than likely depending on if you had activity before/after the delete and whether or not your table was truncated (which would reset the identity seed value) you may have overlapping identity values with different email data. In this case you could use the same query but test to see if the email exists, and not the identity.

I hope this helps and good luck, data recovery is never fun.
I agree with this just import the .CSV file to another table
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,187
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Importing csv to MS SQL

 
0
  #7
Jun 18th, 2009
You're right, SQL Express does not have an import option. Google on "bcp" or bulk copy and you should be able to import with that. Check out this thread:
http://social.msdn.microsoft.com/For...3-e6f0ba39a9d1
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 9
Reputation: yangski is an unknown quantity at this point 
Solved Threads: 0
yangski yangski is offline Offline
Newbie Poster

Re: Importing csv to MS SQL

 
0
  #8
Aug 11th, 2009
Originally Posted by oddvalue View Post
I don't have that option in my menus. I'm using Microsoft SQL Server Management Studio Express 2005.

Any other suggestions?
In the object explorer click the (+) beside the "<Server name>" then the (+) beside the "Databases" then right click the "<name of your database>" select "tasks" then select "Import Data". That's how you do it. I also use SQL Server Management Studio 2005.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 118
Reputation: cgyrob is on a distinguished road 
Solved Threads: 18
cgyrob's Avatar
cgyrob cgyrob is offline Offline
Junior Poster

Re: Importing csv to MS SQL

 
0
  #9
Aug 11th, 2009
@yangski

The user said he was using express which does not include SSIS which is used for the import wizard. The link Sknake provided has a blog on the subject and some possible solutions.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 9
Reputation: yangski is an unknown quantity at this point 
Solved Threads: 0
yangski yangski is offline Offline
Newbie Poster

Re: Importing csv to MS SQL

 
0
  #10
Aug 11th, 2009
Oh I see... Sorry...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC