How to insert data from Excel sheet to SQL Server 2005 - sql-server-2005

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$]');

Related

How to reference a table on an Excel worksheet using OPENROWSET?

Is there any way to reference a table on an Excel worksheet using OPENROWSET?
Referencing a whole sheet works:
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel
12.0;Database=D:\Temp\test.xlsm;IMEX=1;HDR=Yes','select * from [Sheet1$]')
Referencing a range on sheet works too:
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel
12.0;Database=D:\Temp\test.xlsm;IMEX=1;HDR=No','select * from [Sheet1$A1:Z100]')
This is not working... trying to reference Table1
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel
12.0;Database=D:\Temp\test.xlsm;IMEX=1;HDR=No','select * from [Sheet1$Table1]')
NOTE:
If this is not possible, then can anybody suggest a different method, maybe using C#/VB.Net to achieve this?
If the range named Table1 was created with the name box it automatically has global scope. If this is the case just query it without referencing the sheet:
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;
Database=D:\Temp\test.xlsm;IMEX=1;HDR=No','select * from [Table1]')
If you need Table1 to have local (sheet) scope then it should be named SheetName!Table1, where SheetName should be the sheet intended for Table1.
Some more info about opwenrowset and Excel and Named Ranges in Excel.

Can we add borders in excel when exporting data from SQL

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]

How to insert value into DB2 from SQL Server using openquery which are not already inserted?

Is there any option to use not exists in openquery from SQL Server and prevent insert of values which already exist in DB2, from table.
I want to insert data from a table located in SQL Server into a table located in an IBM DB2 server, but I want to check if the value already exists or not.
This works for me actually,
if not exists (select * from openquery(puskarkc, 'select * from puskarlib.castatus'))
begin
insert openquery (puskarkc, 'select * from puskarlib.castatus')
select * from castatus
end

Import Data in Excel Column to SQL Server

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.

Bulk upload from Excel in SQL Server 2008

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