views:

19742

answers:

9

I have an excel spreadsheet that I want to import select columns into my database table. the wiz didn't offer that option.

any easy code options?

Thanks newbie

A: 

The import wizard does offer that option. You can either use the option to write your own query for the data to import, or you can use the copy data option and use the "Edit Mappings" button to ignore columns you do not want to import.

NYSystemsAnalyst
It didn't give me an option to import into an existing table, just the database and it would create a new table. (SQLServer 2008)
Found how that works. thanks. Now I found a bigger proplem. I'm using Management studio 2008 and the data base is 2000. I assumed wrong that it was 2005.
I haven't used management studio 2008 yet (we're running 2005). Management studio 2005 is backward compatible with 2000.
NYSystemsAnalyst
+6  A: 

Microsoft suggest several methods:

  • SQL Server Data Transformation Services (DTS)
  • Microsoft SQL Server 2005 Integration Services (SSIS)
  • SQL Server linked servers
  • SQL Server distributed queries
  • ActiveX Data Objects (ADO) and the Microsoft OLE DB Provider for SQL Server
  • ADO and the Microsoft OLE DB Provider for Jet 4.0

If the wizard (DTS) isn't working (and I think it should) you could try something like this http://www.devasp.net/net/articles/display/771.html which basically suggests doing something like

INSERT INTO [tblTemp] ([Column1], [Column2], [Column3], [Column4])

SELECT A.[Column1], A.[Column2], A.[Column3], A.[Column4]
FROM OPENROWSET 
('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=D:\Excel.xls;HDR=YES', 'select * from [Sheet1$]') AS A;
DrG
Hmm. this indicates that I have to have excel on a server instead of my pc. is there a way to direct it to my pc? I haven't found that answer yet.
+3  A: 

You could use OPENROWSET, something like:

SELECT * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0', 
  'Excel 8.0;IMEX=1;HDR=NO;DATABASE=C:\FILE.xls', 'Select * from [Sheet1$]'

Just make sure the path is a path on the server, not your local machine.

SqlACID
nO WAY TO ACCESS MY LOCAL MACHINE?
Well, you could probably create a share on your local machine and map a network drive from your SQL Server machine to that share, and access your file that way....
marc_s
A: 

By 'the wiz' I'm assuming you're talking about the 'SQL Server Import and Export Wizard'. (I'm also pretty new so I don't understand most questions, much less most answers, but I think I get this one). If so couldn't you take the spreadsheet, or a copy of it, delete the columns you don't want imported and then use the wizard?

I've always found the ability to do what I need with it and I'm only on SQL Server 2000 (not sure how other versions differ).

Edit: In fact I'm looking at it now and I seem to be able to choose which columns I want to map to which rows in an existing table. On the 'Select Source Tables and Views' screen I check the datasheet I'm using, select the 'Destination' then click the 'Edit...' button. From there you can choose the Excel column and the table column to map it to.

Jeff Keslinke
+2  A: 

This may sound like the long way around, but you may want to look at using Excel to generate INSERT SQL code that you can past into Query Analyzer to create your table.

Works well if you cant use the wizards because the excel file isn't on the server

Nick Kavadias
This always seems to be the easiest way for me...
Nathan DeWitt
A: 

Microsoft Access is another option. You could have a Access database locally on your machine that you import the excel spreadsheets into (wizards available) and link to the the SQL Server database tables via ODBC.

You could then design a query in access that appends data from the Excel spreadsheet to the SQL Server Tables.

Troy
+3  A: 

Once connected to Sql Server 2005 Database, From Object Explorer Window, right click on the database which you want to import table into. Select Tasks -> Import Data. This is a simple tool and allows you to 'map' the incoming data into appropriate table. You can save the scripts to run again when needed.

Tejas
A: 

seems tejas is right, if you hit Import Data from within management studio, it brings up the DTS Wizard with much more data source options.

giddy
A: 

Read this post "http://msdotnetbuddy.blogspot.com/2010/08/data-exchange-between-sql-server-excel.html"