SQL Running a query when a condition exists

I have the following query where I set two variable dates to field values in a view:

use livedb
go
Declare @OPSDate as DateTime;
Declare @AUDDate as DateTime;
if OBJECT_ID(N'tempdb..#RollTest') is not null
	truncate table #RollTest
else
	
	Create table #RollTest(
		  [AccountNumber] varchar (15)
		  ,[CAmt] decimal(15,2)
		  ,[VisitID] varchar (15)
		  );

set @OPSDate  = (select [OPS_LD_Closed] from [livedb].[dbo].[vw_DAILY_ATB_OPS_CLOSE] )
set @AUDDate =(select  [AUDIT_LD_Closed]  from [livedb].[dbo].[vw_DAILY_ATB_OPS_CLOSE])
select dateadd(dd,datediff(dd,0,@OPSDate),0) as OPS_LD
select dateadd(dd,datediff(dd,0,@AUDDate),0) as AUD_LD
[b]/*while  dateadd(dd,datediff(dd,0,@OPSDate),0) = dateadd(dd,datediff(dd,0,@AUDDate),0)*/[/b]
	
	insert into #RollTest   
	SELECT     TOP (100) PERCENT dbo.BarVisits.AccountNumber, SUM(CONVERT(numeric(15, 2), dbo.BarChargeTransactions.Amount)) AS CAmt, 
						  dbo.BarVisits.VisitID
	FROM         dbo.BarChargeTransactions LEFT OUTER JOIN
						  dbo.BarVisits ON dbo.BarChargeTransactions.VisitID = dbo.BarVisits.VisitID
	WHERE     (dbo.BarChargeTransactions.BatchDateTime >= dateadd(dd,datediff(dd,0,@OPSDate),0)) AND 
						  (dbo.BarChargeTransactions.BatchDateTime < dateadd(dd,datediff(dd,0,@AUDDate)+1,0)) AND (dbo.BarChargeTransactions.Type = 'C')
	GROUP BY dbo.BarVisits.AccountNumber,  dbo.BarVisits.VisitID
	ORDER BY dbo.BarVisits.AccountNumber
Select * from #RollTest

Open in new window


Without a while condition set, the query runs fine.  Currently the @OPSDate = @AUDDate.   However, what I am trying to do is set up a condition that the query will run when the @OPSDate > than @AUDDate.  The @OPSDate is the last date the source system has finished its closing process.  The @AUDDate is the last time I have successfully run this query and updated some table.  The commented "While" statement in the code does not give me any results when it is uncommented.

What can I do to insert this condition to only run when @OPSDate > @AUDDate?

Thanks

Glen
GPSPOWAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
hmm, what's wrong with

SELECT	@OPSDate =[OPS_LD_Closed] ,
		@AUDDate= [AUDIT_LD_Closed]
FROM	[livedb].[dbo].[vw_DAILY_ATB_OPS_CLOSE];

INSERT INTO #RollTest   
	SELECT	BV.AccountNumber, 
			SUM(CONVERT(NUMERIC(15, 2), dBCT.Amount)) AS CAmt, 
			BV.VisitID
	FROM	dbo.BarChargeTransactions BCT
		LEFT OUTER JOIN dbo.BarVisits BV ON BCT.VisitID = BV.VisitID
	WHERE	dBCT.BatchDateTime >= DATEADD(DD,DATEDIFF(DD,0,@OPSDate),0)) 
		AND	dBCT.BatchDateTime < DATEADD(DD,DATEDIFF(DD,0,@AUDDate)+1,0)
		AND dBCT.Type = 'C'
		AND @OPSDate > @AUDDate
	GROUP BY BV.AccountNumber,  
		BV.VisitID
	ORDER BY BV.AccountNumber;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott PletcherSenior DBACommented:
The code is current checking for them to be equal.  You should just be able to change it to check if @OPSDate > @AUDDate:

while  dateadd(dd,datediff(dd,0,@OPSDate),0) > dateadd(dd,datediff(dd,0,@AUDDate),0)
--                                                                                > rather than =
0
GPSPOWAuthor Commented:
thanks

Glen
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

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.