Saturday, August 27, 2011

Using waitfor time command in Sybase ASE or How to run a stored procedures from different sessions simultaneously at the same time in Sybase ASE?

Sometimes while working with stored procedures it becomes important for us to test some scenario's where the same stored procedure needs to be run from two different sessions at the same time. But what to do in this case? We wont be able to open two different sessions and manually run the same stored procedure at the same time. So what to do in this case?

Well, nothing to fear in this scenario as Sybase already provides a command to enable a wait or make the session sleep for sometime. The waiting command is very simple and can be used in two versions -

1. waitfor time <<At what time should session run>>
2. waitfor delay <<Till what time should the session wait>>

As we can see the waitfor time command when encountered in the process flow the session is halted until the given time is reached. Example, Lets say we run a script like this at '10:20 am'.

use MyDb
go

select * from MyTab

waitfor time '10:25'

update MyTab set Id = 10 where Id = 5

select * from MyTab
go

Now when we ran the script at '10:20 am' then the script runs through all the parts of the script until it reaches the command -

waitfor time '10:25'

When the control reaches this statement the session is put on hold till system time is equal to '10:25 am' and the rest of the session executes exactly (with minor delay) at the given time. So, if we open two sessions at the same time and write waitfor time as the first line of the script then we will be able to run same stored procedure from two sessions at the same time.

No comments:

Post a Comment