I've worked on SQL Server 2014 and I need to import data in it from Excel.
'Till now, I've imported data by right-clicking on the current database -> Tasks -> Import Data.
I want to know if there is a SQL syntax to import data directly into a table?
You can try OPENROWSET
Insert into SQLServerTable
Select * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=D:\testing.xls;HDR=YES',
'SELECT * FROM [SheetName$]')
Related
I have written a openrowset code which gets table content and saves them in a excel sheet I want to format the data in excel sheet with borders while loading any help is highly appreciated.
insert into OPENROWSET(
'Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;Database=M:\abc.xlsx;;HDR=YES',
'SELECT * FROM [Sheet1$]')
select * from [abc]
I have an excel spreadsheet with two columns; job_number and referred_by. I have a SQL Server table with the same two columns. Most of the Job Numbers from the excel spreadsheet exist in the SQL server but not all of them.
How would I import all the referred_by into the SQL Server where a matching job_number exists?
If you need to query an excel spreadsheet I would set it up as a linked server, so:
EXEC sp_addLinkedServer
#server= 'xlsx_NewSheet',
#srvproduct = 'ACE 12.0',
#provider = 'Microsoft.ACE.OLEDB.12.0',
#datasrc = 'C:\spreadsheet.xlsx',
#provstr = 'Excel 12.0; HDR=Yes'
With that set up you can almost query it like just another table:
SELECT * FROM OPENQUERY (XLSX_NewSheet, 'Select * from [Sheet1$]')
or
SELECT * FROM XLSX_NewSheet...[Sheet1$]
There are some limitations that you may need to explore. I would suggest just dumping the entire contents into a temp table and operating from there.
I'm trying to bulk upload from an Excel file using the following query:
BULK INSERT TempRevenueForecast
FROM 'E:\RevenueTracker\Demo\UserTemplate.xls'
WITH (FORMATFILE = 'E:\RevenueTracker\Demo\TRF.FMT');
GO
But, I'm getting this error:
Bulk load data conversion error (truncation) for row 1, column 2
How can I solve it?
If you open the table TempRevenueForecast in edit mode (right-click-table - edit top 200 rows) in SSMS, you can just copy-paste the excel data over.
That's a very handy feature of SSMS.
Otherwise, use OpenRowSet:
INSERT INTO [TempRevenueForecast ] ([Column1], [Column2], [Column3], [Column4])
SELECT A.[Column1], A.[Column2], A.[Column3], A.[Column4]
FROM OPENROWSET
('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=E:\RevenueTracker\Demo\UserTemplate.xls;HDR=YES', 'select * from [Sheet1$]') AS A;
For that to work, the Microsoft Access Database Engine (ACE) components must be installed.
http://www.microsoft.com/en-us/download/details.aspx?id=23734
or
http://www.microsoft.com/en-us/download/details.aspx?id=13255
How to use OpenRowSet to insert data into a blank file?
I need to insert into a txt file (say to D:\TDB) some select output (say select * from sys.tables) from the database
INSERT INTO OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Text;Database=D:\TDB;HDR=Yes;', 'SELECT * FROM sys.tables.txt')
select * from sys.tables;
I get
OLE DB provider "MICROSOFT.JET.OLEDB.4.0" for linked server "(null)"
returned message "The Microsoft Jet database engine could not find the
object 'sys.tables.txt'. Make sure the object exists and that you spell its
name and the path name correctly.".
Msg 7350, Level 16, State 2, Line
1 Cannot get the column information from OLE DB provider
"MICROSOFT.JET.OLEDB.4.0" for linked server "(null)".
What is wrong?
PS. please do not propose the bcp solution, cause already tested and does not work everytime, so I would test openrowset now..
#serhio , I tested your sql below:
INSERT INTO OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Text;Database=D:\TDB;HDR=Yes;', 'SELECT * FROM sys.tables.txt')
select * from sys.tables;
I got a few test results
The filename should not include "." in it.
(sys.tables.txt→systables.txt)
HDR(Header Row) can not be used here.(Remove it)
The txt file must exist.(Create it)
The first line in the txt file should be the all column name of your
source data.
sql
INSERT INTO OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Text;Database=D:\TDB;', 'SELECT * FROM systables.txt')
select * from sys.tables;
systables.txt
name,object_id,principal_id,schema_id,parent_object_id,type,type_desc,create_date,modify_date,is_ms_shipped,is_published,is_schema_published,lob_data_space_id,filestream_data_space_id,max_column_id_used,lock_on_bulk_load,uses_ansi_nulls,is_replicated,has_replication_filter,is_merge_published,is_sync_tran_subscribed,has_unchecked_assembly_data,text_in_row_limit,large_value_types_out_of_row,is_tracked_by_cdc,lock_escalation,lock_escalation_desc
I have an Excel sheet now I need to insert data from that sheet into SQL Server 2005.
I need to create a temp table and insert data into that table. The database name is Employee
Can you pls provide me the syntax for achieving it.
A simple search: http://support.microsoft.com/kb/321686
Probably easiest is
SELECT *
INTO #tmptable
FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=C:\test\xltest.xls', [SheetName$])
--Excel 2007-2010
INSERT INTO DATABASE.SCHEMA.TABLENAME
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0 Xml;HDR=YES;Database=C:\SheetName.xlsx','SELECT * FROM [SheetName$]');
--Excel 97-2003
INSERT INTO DATABASE.SCHEMA.TABLENAME
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.4.0',
'Excel 8.0;HDR=YES;Database=C:\SheetName.xls','SELECT * FROM [SheetName$]');