December 17, 2013 at 12:31 pm
Hi all
i need open a excel file using store procedure, someone know how do it?
claudia
December 17, 2013 at 12:40 pm
you posted in SQL2000, so be aware that if you meant 2008 or above, the ACE drivers would b eused instead.
typically, you will create a linked server, and reference theworksheet(s) via a four part naming convention inside the stored proc or other code.
--#################################################################################################
--Linked server Syntax for Excel
--#################################################################################################
DECLARE @server sysname,
@srvproduct nvarchar(256),
@provider nvarchar(256),
@datasrc nvarchar(100),
@location nvarchar(100),
@provstr nvarchar(100),
@catalog sysname,
@sql varchar(1000)
--add an excel spreadsheet as a linked server.
SET @server = 'HENNXLS'
SET @srvproduct = 'Jet 4.0'
SET @provider = 'Microsoft.Jet.OLEDB.4.0'
set @provstr = 'Excel 5.0;'
SET @datasrc ='C:\Clients\Hennepin\Scripts\Activity Types Client FINAL APPROVAL.xls'
EXEC sp_addlinkedserver @server,@srvproduct,@provider,@datasrc,NULL,@provstr
EXEC dbo.sp_AddLinkedSrvLogin @server, FALSE, NULL, Admin, NULL
--you MUST know the name of the spreadsheet;
--spreadsheet name has a dollar sign at the end of it!
--I've personally never got a spreadsheet that has a space in it
--for example "Activity Data" =ActivityData$ never got [Activity Data$] or [Activity Data]$ to work
--to work, so I end up editing the spreadsheet to remove spaces if that happens.
IF OBJECT_ID('dbo.HENN', 'SN') IS NOT NULL
DROP SYNONYM dbo.HENN;
CREATE SYNONYM dbo.HENN FOR HENNXLS...ActivityData$;
--list all the spreadsheets and their names
EXEC sp_tables_ex 'HENNXLS'
/*
cleanly access data taking advantage of synonyms in 2005
select * from HENNXLS...ActivityData$;
select * from HENN
*/
GO
Lowell
Viewing 2 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy