Saturday, September 14, 2013

XL to SQL database data load

i found another sol for exporting and importing to/from excel
i hope that is useful and it work

SQL Server Management Studio (SQL Server 2005)
1. In SQL Server Management Studio, expand Server Objects in Object Explorer.
2. Right-click Linked Servers, and then click New linked server.
3. In the left pane, select the General page, and then follow these steps:
a. In the first text box, type any name for the linked server.
b. Select the Other data source option.
c. In the Provider list, click Microsoft Jet 4.0 OLE DB Provider.
d. In the Product name box, type Excel for the name of the OLE DB data source.
e. In the Data source box, type the full path and file name of the Excel file.
f. In the Provider string box, type Excel 8.0 for an Excel 2002, Excel 2000, or Excel 97 workbook.
g. Click OK to create the new linked server.


Import:-
CREATE PROCEDURE Import_Data
AS
BEGIN
Insert Customer
SELECT * FROM OPENQUERY(EXCELIMPPRT, ‘SELECT * FROM [Sheet1$]‘)
END

to Export:
Create PROCEDURE Export_Data
AS
BEGIN
INSERT INTO OPENROWSET
(‘Microsoft.Jet.OLEDB.4.0',’Excel 8.0;Database=C:\\sales.xls;’,'SELECT Customer_Key, First_Name,Middle_Name,Last_Name,Birthday, Marital_Status, Gender,Email_address, English_Education, Spanish_Education, Phone,Date_Frist_Purchase,Geography_Key FROM [Sheet1$]‘)
SELECT Customer_Key, First_Name,Middle_Name,Last_Name,Birthday, Marital_Status, Gender,Email_address, English_Education, Spanish_Education, Phone,Date_Frist_Purchase,Geography_Key from Customer
END

No comments:

Post a Comment