Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

How to set a DateFrom Parameter based on a date in a different database table

$
0
0

I have a lengthy SELECT statement I am running on we'll say machine1 and then in a CTE it joins to data on machine2.  I have turned the query into an INSERT statement for loading data from machine 1 to machine 2.  This works fine when I am manually manipulating the DateFrom and DateTo parameters involved.  I need a repeatable process I can put into an SQL Agent job.  My problem is that I need to be able to set the @DateFrom parameter based on a date in the second instance's table.  It will run daily and if it fails I need the query to be able to figure out what the proper DateFrom value is.  I'm just not quite sure how to make that work.  What I have below is the concept of what I want to do. 

The MAX(DispatchDate) is the most recent date in the Dispatches table.  I need the parameter to be set so that the query running on machine 1 can always figure out that the DateFrom parameter should be the Max(Dispatch) value plus 1 day from the Dispatches table on Machine2. 

SET @DateFrom = DATEADD(dd,+1,MAX(DispatchDate)) from Machine2.Warehouse.dbo.Dispatches


Lee Markum


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>