Solved

Import form Excel in SQL Server 2012.

Posted on 2014-10-31
7
80 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 9

Expert Comment

by:selva_kongu
Comment Utility
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
Comment Utility
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
Comment Utility
Please try by this excel...
Book1.xlsx
0
 
LVL 15

Accepted Solution

by:
Haris Djulic earned 500 total points
Comment Utility
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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Suggested Solutions

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now