473,378 Members | 1,469 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Use variable for datasource in Opendatasource

I would like to create a stored procedure that imports an Excel file to
a sql server table. I need to be able to pass the path to the
spreadsheet to the stored procedure like so:

CREATE PROCEDURE dbo.ImportSpreadsheet

(
@Path nvarchar(120)

)

AS

SELECT * INTO Audit
FROM OPENDATASOURCE( 'Microsoft.Jet.OLEDB.4.0',
'Data Source = ' + @Path + ';Extended Properties=Excel 8.0')...Audit$
I will call the stored procedure from within my .NET application and
pass it the path as @Path. The path will be chosen by the user at run
time and will be something similar to
C:\Spreadsheets\Audits.xls.

I can make this run in the Query Analyzer:
SELECT * INTO Audit
FROM OPENDATASOURCE( 'Microsoft.Jet.OLEDB.4.0',
'Data Source = ''C:\Spreadsheets\Audits.xls\Audit_TC.xls'';Extend ed
Properties=Excel 8.0')...Audit$

I could run the SQL directly from my app, but I like to use stored
procs whenever I can for the speed and flexibility. I can change the
stored proc and not have to recompile/reinstall the app.

Any help would be greatly appreciated.

May 3 '06 #1
1 8584
(lo*****************@gmail.com) writes:
I can make this run in the Query Analyzer:
SELECT * INTO Audit
FROM OPENDATASOURCE( 'Microsoft.Jet.OLEDB.4.0',
'Data Source = ''C:\Spreadsheets\Audits.xls\Audit_TC.xls'';Extend ed
Properties=Excel 8.0')...Audit$

I could run the SQL directly from my app, but I like to use stored
procs whenever I can for the speed and flexibility. I can change the
stored proc and not have to recompile/reinstall the app.


To do this in a stored procedure, you would have to use dynamic SQL.
See http://www.sommarskog.se/dynamic_sql.html for details.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
May 4 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jim Andersen | last post by:
Version 2000. How do I do something like the example SELECT * FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0', 'Data Source="c:\Finance\account.xls";User ID=Admin;Password=;Extended...
1
by: David Horowitz | last post by:
Hi folks. I'm trying to connect to a Medisoft Advantage SQL db through SQL Server using OpenDataSource or OpenRowSet. I have general connections to the db working fine, but not with OpenDataSource...
1
by: Stefania Scott | last post by:
I wrote the following code: DoCmd.OutputTo acOutputTable, "tmpWorkSheet", acFormatXLS, "C:\WS.xls", False, "" Set oWordDoc = CreateObject("Word.Application") oWordDoc.Application.Visible = True...
0
by: Bob Smith | last post by:
I have existing word documents that we need to have the data document reflect whether we're running in development, test or production. The data header is longer than 255 characters, so I cannot...
2
by: confused99 | last post by:
Hi everybody... I've been working on moving access data to a word document for the last 10 days but keep getting stuck. The latest problem is that i'm getting the error "Object Variable Or With Block...
1
by: bogorman | last post by:
I am using the following coding: Dim strWordDocFile As System.String = Me.txtLetter.Text Dim WordDocFile As New System.IO.FileInfo(strWordDocFile) Dim Msg As String Dim...
1
by: sayedul | last post by:
Hi, I can access data from xls file using OpenDataSource. But if the worksheet name starts with numeric character it gives error. Scripts are as follows: If the worksheet name is 'Sheet1' it...
1
by: akaess | last post by:
I am trying to merge information from the the database into word documents using the following methods but it keeps giving the error 91: Object Variable or With Block Variable not set. could anybody...
2
by: akaess | last post by:
Hey everybody I am currently trying to merger data from a form in MS access but it keeps giving me the error 91: Object variable or with block variable not set. could somebody please look at the code...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.