Solved

Import form Excel in SQL Server 2012.

Posted on 2014-10-31
7
83 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Nested Case statement 4 37
SQL Server / Update DB? 22 36
AD and SQL Server 2016 2 27
Parse this column 6 25
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how the fundamental information of how to create a table.

839 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