|
Execute the following
Microsoft SQL Server T-SQL script to export table data into an Excel worksheet and import the data back into a table:
USE AdventureWorks
-- SQL Server export to Excel
-- the target empty worksheet should exist with header line only
INSERT INTO OPENROWSET(
'Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=F:\data\test\NameAddress.xls;HDR=YES',
'SELECT * FROM [Sheet1$]')
SELECT FirstName,
LastName,
EmailAddress,
Phone
FROM Person.Contact
GO
-- import
SELECT *
INTO tempdb.dbo.NameAddress
FROM OPENROWSET(
'Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=F:\data\test\NameAddress.xls;HDR=YES',
'SELECT * FROM [Sheet1$]')
GO
USE tempdb
SELECT *
FROM dbo.NameAddress
GO
Related article:
How to import data from Excel to SQL Server
|