• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 499
  • Last Modified:

SQL Server - Execute SQL Server store procedure from Excel


Could you please let me know why my code below isn't working?

        Set cmd = New ADODB.Command
        cmd.ActiveConnection = Cn
        cmd.CommandText = "Exec TRP.M_Par_update_process"
        'cmd.CommandTimeout = 1200               ' 20 minutes
        cmd.CommandType = adCmdStoredProc
        cmd.Execute , , adAsyncExecute
        MsgBox ("Loaded & Processed!")

When a button in Excel is clicked, I want it to execute a store procedure name "TRP.M_Par_update_process" in the SQL Server without having to wait in Excel until the procedure is completely done.
3 Solutions
If you use the command type for stored proc, just enter the name of the stored proc, not the command text, i.e. replace
 cmd.CommandText = "Exec TRP.M_Par_update_process"

Open in new window

 cmd.CommandText = "TRP.M_Par_update_process"

Open in new window

Of course, you can always just use the Connection object to run a store procedure

Cn.Execute "Exec TRP.M_Par_update_process"

Open in new window

HNA071252Author Commented:
When I executed the store procedure using adAsyncExecute, how do I know when it's finish running? From the SQL Server Management Studio window, how do I check what are the procedures currently running?
SharathData EngineerCommented:
You can find out currently running statements with this query. You can embed this into your VBA code and check if the stored proc is running.
SELECT r.*,t.text FROM sys.dm_exec_requests r cross apply sys.dm_exec_sql_text(r.sql_handle) t
WHERE r.status IN (N'Suspended',N'Running',N'Runnable',N'Pending')

Open in new window

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Hi all

I know this question has already been answered, but if it helps I finished an article on how to build an Excel front-end, SQL Server Stored Procedure back-end that allows users to enter cell-based parameters:

Microsoft Excel & SQL Server:  Self service BI to give users the data they want
If you like what you see, please click the 'Yes' button next to 'Was this article helpful?', and provide some feedback.

Thanks in advance.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now