Create Excel Linked Server with Microsoft
|
There are thousands of articles how to create an Excel Link Server in SQL specially in Local environment and not enough on IIS.
Hope this is useful.
Requirements:
SQL server ver. >= 2008
Excel >= 2007
IIS Optional
Provider for SQL server = Microsoft.ACE.OLEDB.12.0
Let’s follow the steps bellow.
Open the SSMS.
From Left Menu Expand the System Databases (master)
Click on New Query and Paste the following lines:
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
Select Programmability -> Stored Procedures. Create New Stored Procedure.
Copy the next lines in the New Procedure body:
USE [master]
GO
SET
ANSI_NULLS
ON
GO
SET
QUOTED_IDENTIFIER
ON
GO
CREATE
PROCEDURE
[dbo].[P_CR_LINK_EXL_SRVR]
@PEXCL_SRVR_NAME AS
NVARCHAR(MAX),
@PSRVR_NAME AS
NVARCHAR(MAX)
, @EXL_FILE_LCTN
AS
NVARCHAR(MAX)
AS
BEGIN
SET
NOCOUNT
ON;
EXEC
master.dbo.sp_addlinkedserver
@server = @PEXCL_SRVR_NAME,
@srvproduct=N'Excel',
@provider=N'Microsoft.ACE.OLEDB.12.0',
@datasrc=@EXL_FILE_LCTN,
@provstr=N'Excel 12.0'
EXEC
master.dbo.sp_addlinkedsrvlogin
@rmtsrvname=@PEXCL_SRVR_NAME,@useself=N'False',@locallogin=NULL,@rmtuser=NULL,@rmtpassword=NULL
EXEC
master.dbo.sp_addlinkedsrvlogin
@rmtsrvname=@PEXCL_SRVR_NAME,@useself=N'True',@locallogin=@PSRVR_NAME,@rmtuser=NULL,@rmtpassword=NULL
EXEC
master.dbo.sp_serveroption
@server=@PEXCL_SRVR_NAME,
@optname=N'collation compatible',
@optvalue=N'false'
EXEC
master.dbo.sp_serveroption
@server=@PEXCL_SRVR_NAME,
@optname=N'data access',
@optvalue=N'true'
EXEC
master.dbo.sp_serveroption
@server=@PEXCL_SRVR_NAME,
@optname=N'dist',
@optvalue=N'false'
EXEC
master.dbo.sp_serveroption
@server=@PEXCL_SRVR_NAME,
@optname=N'pub',
@optvalue=N'false'
EXEC
master.dbo.sp_serveroption
@server=@PEXCL_SRVR_NAME,
@optname=N'rpc',
@optvalue=N'false'
EXEC
master.dbo.sp_serveroption
@server=@PEXCL_SRVR_NAME,
@optname=N'rpc out',
@optvalue=N'false'
EXEC
master.dbo.sp_serveroption
@server=@PEXCL_SRVR_NAME,
@optname=N'sub',
@optvalue=N'false'
EXEC
master.dbo.sp_serveroption
@server=@PEXCL_SRVR_NAME,
@optname=N'connect timeout',
@optvalue=N'0'
EXEC
master.dbo.sp_serveroption
@server=@PEXCL_SRVR_NAME,
@optname=N'collation name',
@optvalue=null
EXEC
master.dbo.sp_serveroption
@server=@PEXCL_SRVR_NAME,
@optname=N'lazy schema validation',
@optvalue=N'false'
EXEC
master.dbo.sp_serveroption
@server=@PEXCL_SRVR_NAME,
@optname=N'query timeout',
@optvalue=N'0'
EXEC
master.dbo.sp_serveroption
@server=@PEXCL_SRVR_NAME,
@optname=N'use remote collation',
@optvalue=N'true'
EXEC
master.dbo.sp_serveroption
@server=@PEXCL_SRVR_NAME,
@optname=N'remote proc transaction promotion',
@optvalue=N'true'
END
Run the Procedure (press F5)
Change the CREATE Command in Procedure body to ALTER
Rerun the Procedure
We are almost ready.
Expanding the Linked Servers… Providers on the Microsoft.ACE.OLEDB.12.0, right click
on
Properties
check the Allow inprocess :
The next steps are to run the Procedure with the Parameters:
The name of Excel Linked Server : 'EXCEL_DATA_TEST'
The Computer Name and Login User Name , OR the Domain Name and Login User Name for IIS
'DESKTOP-HGUOGQ\DPSYC' OR
WEKXOW-XX\IWPD_10(sysuser_0)
The Excel File Location : 'C:\STS\file_example_XLS_10.xlsx'
In case you use IIS we have to give Read Rights for the user IWPD.. to folder 'C:\STS