17 ธันวาคม 2557

การ Export Table to Excel (SQL Script)

EXEC sp_configure 'Show Advanced Options', 1
RECONFIGURE
GO
 
EXEC sp_configure 'Ad Hoc Distributed Queries', 1
RECONFIGURE
GO

EXEC sp_MSSet_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'AllowInProcess', 1
GO
 
EXEC sp_MSSet_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'DynamicParameters', 1
GO

---- Export To Excel File
insert into OPENROWSET('Microsoft.ACE.OLEDB.12.0', 
    'Excel 12.0;Database=E:\_Backup\testing.xls;', 
    'SELECT * FROM [Sheet1$]') select Sku_Id,Str1 from ms_SKU

---- Insert Form Excel File
SELECT * INTO dbo.ImportedEmployeeData
FROM   OPENROWSET('Microsoft.ACE.OLEDB.12.0',
       'Excel 12.0 Xml;HDR=YES;Database=E:\_Backup\testing.xls;',
       'SELECT * FROM [Sheet1$]')
GO

อ้างอิง :: http://sqlandme.com/2013/04/15/sql-server-import-data-from-excel-using-t-sql/