Solved

SQL Server - Execute a store procedure asynchronously from Excel

Posted on 2013-10-23
8
963 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 69

Assisted Solution

by:Éric Moreau
Éric Moreau earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Would anyone please help me with my question above?
0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
what have you done of the 2 comments?
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:HNA071252
Comment Utility
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 69

Expert Comment

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

try removing your adAsyncExecute
0
 

Author Comment

by:HNA071252
Comment Utility
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 69

Accepted Solution

by:
Éric Moreau earned 500 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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.
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now