Solved

Import form Excel in SQL Server 2012.

Posted on 2014-10-31
7
85 Views
Last Modified: 2014-11-17
Hello,

I have an excel document with one tab called Measurements.  The tab has many columns but i am only interested in one called PersonKod

I want to import this column into a new table in the database.  I have tried to write my own query but i cant get the syntax correct.

What would be the correct Insert statement to just read this one column?  Do i need to create the table before hand, or can i do that in the next step?
0
Comment
Question by:soozh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40415215
Do you have to use query for this? Can use import import data option?

Here is step by step: http://www.mssqltips.com/sqlservertutorial/203/simple-way-to-import-data-into-sql-server/

but if you need it from query here are some good examples http://support.microsoft.com/kb/321686
0
 

Author Comment

by:soozh
ID: 40415239
Hello,

Yes i need to use a query because i only want one column.  There are some columns that fail during the import.

On the microsoft page it says:

Import vs. Append

The sample SQL statements that are used in this article demonstrate Create Table queries that import Excel data into a new SQL Server table by using the SELECT...INTO...FROM syntax. You can convert these statements to Append queries by using the INSERT INTO...SELECT...FROM syntax while you continue to reference the source and destination objects as shown in these code samples.

Its just that syntax i am having trouble with!  And my other question relating to the destination table.  Will the import tool create it from my query?
0
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40415247
You can specify the columns you need in the Import tool...  

can you post your syntax?

If you use select .. into .  table will be create otherwise you need to prepare the table..
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 9

Expert Comment

by:selva_kongu
ID: 40415323
Use the sql server data import export wizard
import wizand select the source as Microsoft excel and select the excel file
source filethen select the destination sql server and db
destination serverfrom next screen select sheet and click the edit mapping and select column mappings for source and destination
edit mappingsfinish with run immediately option.
0
 

Author Comment

by:soozh
ID: 40415355
Hello,

Sevla kongu - thanks for the instructions - i can already import that way... i am trying to write my own query rather than let the import do it.

As i wrote :
I have an excel document with one tab called Measurements.  The tab has many columns but i am only interested in one called PersonKod

so maybe it should be something like ?

insert into Newtable (personkod)
  select personkod from Measurements$
0
 
LVL 2

Expert Comment

by:Pratik Makwana
ID: 40415426
Please try by this excel...
Book1.xlsx
0
 
LVL 15

Accepted Solution

by:
Haris Djulic earned 500 total points
ID: 40415545
Try like this:

for Excel 2007 and up

SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
  'Excel 12.0;Database=C:\excel-sql-server.xlsx', 'Select PersonKod from  [Measurements$]')

for Excel 2003 

SELECT * INTO NEW_TABLE_NAME FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=C:\test\xltest.xls', 'SELECT PersonKod FROM [Measurements$]')

Open in new window

0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to shrink a transaction log file down to a reasonable size.

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question