Solved

Convert process flow to script

Posted on 2014-03-27
1
234 Views
Last Modified: 2014-03-28
Hi all
Today is one of the days.. the brain stop processing logic

I need to do some batch process.
First I need to check if the position are completed with refrence to a header record.

I do this with a batch: exec sp_BuyersCheckPosition @TransID, @CustomerNr, @PosCheck OUTPUT      

If the position is not yet complete, I check if the time  since creation has elapse for 5 mins.
I process the batch.. If the condition is not yet met .. I will exit the execution..

Note..  If the position is complete, there is no need to check the time..  But if the position is not complete .. then check if the waiting time of 5 mins has elapse before processing the batch.


Process Flow
IF EXISTS(SELECT CreateDate FROM Customer_H 	WHERE (TransID = @TransID 
				AND  CreateDate < (select  DATEADD(mi, -2, GETDATE())) ))
				BEGIN
					@TimeCheck = 1
				END
			exec sp_BuyersCheckPosition @TransID, @CustomerNr, @PosCheck OUTPUT	
			IF (@PosCheck = 1 OR @TimeCheck = 0) 
			BEGIN
				Do my Batch thing
			End

Open in new window

0
Comment
Question by:ZURINET
1 Comment
 
LVL 35

Accepted Solution

by:
David Todd earned 500 total points
ID: 39960472
Hi,

Here is how I'd code it.

declare @isOkayToExit bit
set @isOkayToExit = 0

while not @isOkayToExit begin
	set @isOkayToExit = 1

	exec sp_BuyersCheckPosition @TransID, @CustomerNr, @PosCheck OUTPUT	

	if @PosCheck != 1 begin
		IF EXISTS(
			SELECT CreateDate 
			FROM Customer_H 	
			WHERE 
				(TransID = @TransID 
				AND  CreateDate < (select  DATEADD(mi, -5, GETDATE())) ) -- I think the dateadd should be -5 not -2
				)
		BEGIN
			@TimeCheck = 1
		END else begin
			@isOkayToExit = 0
			-- http://technet.microsoft.com/en-us/library/ms187331.aspx
			waitfor delay '0:02:00'
			continue

	end

	IF (@PosCheck = 1 OR @TimeCheck = 1) BEGIN -- I think that the check should be is @TimeCheck = 1
		--set @isOkayToExit = 1
		Do my Batch thing
	End 
end

Open in new window

Regards
  David
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
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.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

920 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

15 Experts available now in Live!

Get 1:1 Help Now