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 from xls file

Author  Topic 

AJSkydiver
Starting Member

8 Posts

Posted - 2011-07-11 : 07:20:02
Hey guys... If someone could help me with this, I would be so appreciative. I have an excel spreadsheet (2007) with a list of a little over 1000 serial numbers listed that I need to load into an existing inventory table. I know I need to create a view, bulk insert into the view and then load from the view into my device inventory table, but I'm stuck...Here is what I have so far...if someone could help me understand the steps I need to perform (along with the code) it would help me out so much...

CREATE VIEW vwserialimport
AS
SELECT Field1, Field2, Field3 (do I select all of the columns from the table that I need here? I have all of the data in the excel sheet, so I don't need to pull any values from the existing table..)
FROM Device (this is my table that exists)
GO

BULK INSERT vwserialimport
FROM ‘C:\YourFilePath’
WITH ( FIELDTERMINATOR = ‘,’,
ROWTERMINATOR = ‘\n’
)
GO

And then I'm assuming I need to insert into the existing device table the values I'm loading into my view?

Thanks in advance guys!


edit: moved to proper forum

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-07-11 : 08:20:16
Create a staging table instead of a view. When you create the table, you might want to create all columns as varchar(something) if you suspect there may be invalid data.
CREATE dbo.Inventory_Staging
(
col1 VARCHAR(511),
col2 VARCHAR(511),
...
colN VARCHAR(511)
);


Once you import into the staging table using bulk insert (or other means), then you can insert the data into your inventory table using a simple select after examining the data to make sure it is correct and as you expect.

The other thing I want to point out is that if your file is really an Excel file (.XLS as opposed to a plain text .CSV file), you should use the Import/Export Wizard or SSIS to import the data into the staging table.
Go to Top of Page

AJSkydiver
Starting Member

8 Posts

Posted - 2011-07-11 : 08:56:29
Thank you! Quick question...if I use SSIS (I've went ahead and built the package for this)..will it wipe out current data, or just insert the new values? I created the package like this: http://www.techrepublic.com/blog/datacenter/how-to-import-an-excel-file-into-sql-server-2005-using-integration-services/205

Thank you for your help...
Go to Top of Page

AJSkydiver
Starting Member

8 Posts

Posted - 2011-07-11 : 10:41:09
eek...another issue. I created the package above, but having an issue with a couple of the numeric fields (can't convert between types DT Numeric and DT 14)... As I was looking around trying to find a conversion fix, I've read that I can't do this remotely? Can you only develop the packages (and test) at the server level? If so, i'm back to square 1. :( Sorry to be a pain, I have to get this figured out..
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-07-11 : 12:18:42
Assuming you have proper permissions, you should be able to connect to a SQL server from a remote box and create SSIS packages.
Go to Top of Page

AJSkydiver
Starting Member

8 Posts

Posted - 2011-07-11 : 14:13:46
Is there any way someone can help me with how to import data from Excel to an existing SQL Table? This is a one time thing and I seem to be having connection issues when I try via SSIS. If anyone can help, you'll be my new hero :)

Thanks for your help sunita, I really need to understand how to do this via my original post. It was a great idea, but this being a one time thing, if I can get help writing the code, that would be so wonderful :)
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-07-11 : 14:32:04
There is step by step instructions to use SSIS Import/Export wizard here. Give it a try and if you run into a problem, post which step you encountered the problem and the error message?
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-07-11 : 14:55:26
Can't you just save the data in some delimited format?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

AJSkydiver
Starting Member

8 Posts

Posted - 2011-07-11 : 15:01:53
Thank you...I will give it another go. The link you provided is for SQL 2008... Is there a way I can run the import/export wizard from Management Studio for SQL 2005?
Go to Top of Page

AJSkydiver
Starting Member

8 Posts

Posted - 2011-07-11 : 15:04:42
Hey Brett! Yes, I can save the worksheet to a csv file easily. That would be my preference. Can you tell me what steps I need to produce to do a bulk insert using the csv file? Thank you!!! :)
Go to Top of Page
   

- Advertisement -