Solved

SQL Server - Execute a store procedure asynchronously from Excel

Posted on 2013-10-23
8
1,019 Views
Last Modified: 2014-03-12
Set cmd = New ADODB.Command
        cmd.ActiveConnection = Cn
       
        cmd.CommandText = "TRP.FFS_Contracts_process_v1"
        cmd.CommandType = adCmdStoredProc
        cmd.Execute , , adAsyncExecute
             
        cmd.CommandText = "Update TRP.Param set F_Proj_type = 'P'"
        cmd.CommandType = adCmdText
        cmd.Execute
       
        cmd.CommandText = "TRP.sp_FFS_Projection"
        cmd.CommandType = adCmdStoredProc
        cmd.Execute , , adAsyncExecute

Could you please let me know why I'm getting an error message on this line and how to fix it?
        cmd.CommandText = "Update TRP.Param set F_Proj_type = 'P'"

Error message says "Operation cannot be performed while executing asynchronously"
0
Comment
Question by:HNA071252
  • 4
  • 4
8 Comments
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 500 total points
ID: 39595963
a single connection cannot execute multiple command at the same time.

why don't you just send the 3 queries at the same time?

        cmd.CommandText = "exec TRP.FFS_Contracts_process_v1 " +
                                              "Update TRP.Param set F_Proj_type = 'P' " +
                                              "exec TRP.sp_FFS_Projection "
        cmd.CommandType = adCmdText
        cmd.Execute , , adAsyncExecute
0
 

Author Comment

by:HNA071252
ID: 39601642
with combining the 3 queries in one command, is it execute one after another or simultaneously? because I would need the first one done, before the 2nd, then the 3rd.
0
 

Author Comment

by:HNA071252
ID: 39711941
Would anyone please help me with my question above?
0
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
LVL 70

Expert Comment

by:Éric Moreau
ID: 39711986
what have you done of the 2 comments?
0
 

Author Comment

by:HNA071252
ID: 39712078
You asked me to combine the three queries in one command:

        cmd.CommandText = "exec TRP.FFS_Contracts_process_v1 " +
                                              "Update TRP.Param set F_Proj_type = 'P' " +
                                              "exec TRP.sp_FFS_Projection "
        cmd.CommandType = adCmdText
        cmd.Execute , , adAsyncExecute

and I asked if is it execute one after another or simultaneously? Although I didn't get Error message says "Operation cannot be performed while executing asynchronously", but it didn't execute all three commands, it didn't continue on executing it in the Server but it stops when I closed Excel.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 39712131
since it is a single command, the 3 statements will run one after the other.

try removing your adAsyncExecute
0
 

Author Comment

by:HNA071252
ID: 39712144
If I remove adAsyncExecute, then the command cmd.Execute is hanging in Excel until it finish which can be as long as an hour and I don't want to wait in Excel, I wanted to continue executing in the Server even when I'm done with Excel but I couldn't figure how to make it work,
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 39712179
the execution stops because the connection is closed. so you need to run the command from a different context. one way of achieving this is to schedule your commands.

check the sample at http://stackoverflow.com/questions/287060/scheduled-run-of-stored-procedure-on-sql-server
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question