Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 SQL Server Administration (2008)
 SQL Server Agent / Jobs

Author  Topic 

nhess80
Yak Posting Veteran

83 Posts

Posted - 2011-03-11 : 18:35:40
Hi,

I have several sql jobs setup that run every night. They are queries that update tables in my database. In these queries I have a set date range to update the data over. So lets say the date range is set to today and greater

Example: Where Date >= Today
I also have the Where clause to only update where there is a NULL value

This works great for awhile until the Date get further away from the Today Date that I set. My question is, is there a way to dynamically update the sql job so that I can have the date range change? Is there a way to have the sql job grab the query it uses from a file rather than the static script that I type into it?

Hope all of this makes sense

Thanks,

Nick

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-03-11 : 18:38:52
Yes and yes. Show us your query, sample, data, expected outcome so we can help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

nhess80
Yak Posting Veteran

83 Posts

Posted - 2011-03-11 : 20:15:35
quote:
Originally posted by tkizer

Yes and yes. Show us your query, sample, data, expected outcome so we can help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog




Here is the query. It works just fine. Just need to figure out a way to change the date other than manually. Note that the date is in epoch time, The part in bold is what I am trying figure out. If there was a way that the sql job could grab this query from a file and if the content of the file were to change then the job would change. This query is static and the further the current date gets away from the set date the more the query slows down. Currently this query takes maybe 4 sec or so and it updates 150,000 records give or take. The Date range is roughly 10 days. I would like to keep it in that range

UPDATE CDRS
SET Revenue = CASE WHEN b.C_ANPI_PREM_npanxx = LEFT(RIGHT(LRN, 10), 6)
THEN CASE WHEN InterIntra = 'Inter' THEN RoundDuration
* b.C_ANPI_PREM_Inter WHEN InterIntra = 'Intra' THEN RoundDuration
* b.C_ANPI_PREM_Intra WHEN InterIntra = 'Undfd' THEN RoundDuration
* b.C_ANPI_PREM_Intra END ELSE RoundDuration * 0.035 END
FROM CDRS AS a LEFT OUTER JOIN
C_ANPI_PREM AS b ON b.C_ANPI_PREM_npanxx = LEFT(RIGHT(LRN, 10), 6)
WHERE (DateTimeInt > 1298937600) AND (InterIntra IS NOT NULL) AND (Revenue IS NULL)
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-03-12 : 14:38:32
But what should it be dynamically changed to? Describe in words and/or pseudocode what it should do so that we can help you make it dynamic.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-03-12 : 14:38:52
Maybe GETDATE() - 10 and then convert to Epoch time?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

nhess80
Yak Posting Veteran

83 Posts

Posted - 2011-03-14 : 01:46:37
quote:
Originally posted by tkizer

Maybe GETDATE() - 10 and then convert to Epoch time?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog




That might actually work. The goal would be to take the epoch time that is in BOLD below and have that be dynamic. Say keep that 10 days behind the current time

WHERE (DateTimeInt > 1298937600)
Go to Top of Page

nhess80
Yak Posting Veteran

83 Posts

Posted - 2011-03-14 : 15:21:23
That worked. Thank you.

I ended up replace the epoch time with this DATEDIFF(s,'1970-01-01 00:00:00',getdate() - 10)


quote:
Originally posted by tkizer

Maybe GETDATE() - 10 and then convert to Epoch time?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

Go to Top of Page
   

- Advertisement -