Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Tips on selecting SQL data into a worksheet?


I have a SQL Server stored procedure I need to run from within an excel
spreadsheet, if that's possible.

The stored procedure takes about three minutes to run, and the end result is
10 columns worth of data. Ideally, I'd like for those 10 columns to be my
spreadsheet's 10 columns.

Are there code snippets out there I can look at to learn:

1. how to connect to a SQL Server database via excel (hopefully using a
DSN-less connection-- I have a connection string), and
2. how to call a SQL Stored procedure from Excel, and snag the resulting
data.

TIA,

-Jim


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22
Default Tips on selecting SQL data into a worksheet?

Hi Jim,

"Jim Bancroft" <bobbgambles_at_nospam.msn.com wrote in message
...

I have a SQL Server stored procedure I need to run from within an excel
spreadsheet, if that's possible.

The stored procedure takes about three minutes to run, and the end result

is
10 columns worth of data. Ideally, I'd like for those 10 columns to be my
spreadsheet's 10 columns.

Are there code snippets out there I can look at to learn:

1. how to connect to a SQL Server database via excel (hopefully using a
DSN-less connection-- I have a connection string), and
2. how to call a SQL Stored procedure from Excel, and snag the resulting
data.


I've pasted some code below that shows you how to execute a SP from Excel.
It's not that hard, it just involves a few steps. If you don't have any
parameters in your SP, you can take out the 2 lines related to that. And
you'll want to add error handling that cleans up when you hit an error.
Most errors will occur when connecting (if the connection is down or the
string is incorrect) or when executing the SP via the Command object (this
will fail if anything you've done up to that point is incorrect or missing).

BTW, 3 minutes for a SP to execute seems like a long time - do you have a
_ton_ of data, or is the query very complex? Just wondering if you can't
optimize the query a bit.

Regards,

Jake Marx
MS MVP - Excel



---------sample code----------
Sub PutRecordsetInRange()
'/ you must set a reference to the Microsoft ActiveX Data Objects 2.x
Library
'/ (where x is the highest # you or your users will have)
Dim cn As ADODB.Connection
Dim cd As ADODB.Command
Dim rsData As ADODB.Recordset

Set cn = New ADODB.Connection
With cn
.CursorLocation = adUseClient
.ConnectionString = "<your cxn string"
.Mode = adModeRead
.Open
End With

Set cd = New ADODB.Command
With cd
Set .ActiveConnection = cn
.CommandType = adCmdStoredProc
.CommandText = "dbo.<your sp name_sp"
.CommandTimeout = 180 '/ set this higher if you need to

.Parameters.Append .CreateParameter("@starttime", _
adDBTimeStamp, adParamInput, , CLng(Now()) - 2)
.Parameters.Append .CreateParameter("@endtime", adDBTimeStamp, _
adParamInput, , Now())

Set rsData = .Execute
End With

With rsData
If .State = adStateOpen Then
If Not (.BOF And .EOF) Then
'/ got records - put them in range
Range("A1").CopyFromRecordset rsData
End If
.Close
Else
'/ error
End If
End With

Set rsData = Nothing
Set cd = Nothing
cn.Close
Set cn = Nothing
End Sub


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Locating and selecting Data from Worksheet Jason K[_2_] Excel Discussion (Misc queries) 2 May 29th 09 04:21 PM
Selecting a row in a worksheet Rochelle in Melbourne Excel Worksheet Functions 1 April 9th 09 11:34 AM
Selecting a specific worksheet Jackie New Users to Excel 5 February 25th 09 07:03 PM
selecting a worksheet rocket0612 Excel Worksheet Functions 2 March 1st 06 09:47 AM
Selecting a Worksheet Range Coolboy55 Excel Worksheet Functions 6 August 23rd 05 03:57 PM


All times are GMT +1. The time now is 05:36 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"