English 中文(简体)
Monitoring a calculated data (A person s age) in SQL Server
原标题:

I have a table, tblClient, which stored a client s date of birth in a field of type datetime, DOB.

The goal here is that, when a client reaches 65 years old (need to be calculated thru DOB), I need to insert a new record into another table.

But since the age of a client does not change due to a database transaction (INSERT,UPDATE,DELETE), trigger is out of question.

What would be a good idea to montior such changes?

最佳回答

create a sql agent job that runs daily or hourly that will do this calculation with T-SQL and then if someone reaches 65 it will do the insert

问题回答

Keep it as self-contained to SQL Server as possible - a SQL Server Agent job that periodically executes a stored procedure should do nicely.

A scheduled task or SQL Server maintenance plan that runs a stored procedure as often as required, updating the required rows.

What about a nightly job using SSIS with a stored procedure that checks and if it happens that they are 65 it enters a new row in the table?

you can create a SQL Server Agent Job within the database using SQL Server Management Studio for this:

http://www.databasedesign-resource.com/sql-server-jobs.html

Set up a daily job to EXEC BirthdayProcessingProcedure or whatever you want to name it.

As long as the database is up and running, the JOB will run according to the schedule you set up (from within the database).

I m going to propose another approach - run something every time a DOB is updated (or added) that calculates the period from now until the first person reaches 65. Then (re-)schedule a job to run at that time.

Also, I can t believe you need to insert that row the second they reach 65, so a once-a-day procedure that calculates today s new 65year olds would seem good enough?

How about a new field that is age65 date. Calculate it once on record insert, then you can query to your hearts content on this field. You will need to do this is a trigger (and account for updates, they are rare for DOB fields but possible when they are mistyped.) Now that I think about it some, a calculted filed will probably work instead of a trigger.

Then run a daily job to catch anyone who turned 65 since the last time the job was run successfully. Make sure to handle this so that if the job fails one day, the people from that daty are picked up the next run.

The reason why I suggest this is that calculating the age of every person inyour database every day is such a waste of resources for a calculation that really only needs to be done once. Ok not a big deal when you have 100 people, big problem when you have a million. Doindthis kindof calc on a million records to identify the three you need is painful. Doing it once on data entry, not so bad.





相关问题
How to write this T-SQL WHERE condition?

I ve got two tables: TableA Col1 Col2 TableB Col3 Col4 I want to join them together: SELECT * from TableA join TableB ON (...) Now, in place of ... I need to write an expression ...

Customer and Order Sql Statement

TSQL query to select all records from Customer that has an Order and also select all records from customer that does not have an Order. The table Customer contains a primary key of CustomerID. The ...

Recommended way of querying multiple Versioned tables

Have a win 2003 box with MSSQL 2005 running on it. There is a database which is populated every morning with new/modified SalesOrder made the previous day. The database has several tables: SalesOrder, ...

update duplicate record

I have a table with the following fields Id Name IsPublic i need to write a sql query that updates IsPublic to false where name has a duplicate. Only one of the duplicates should have IsPublic = ...

Define variable to use with IN operator (T-SQL)

I have a Transact-SQL query that uses the IN operator. Something like this: select * from myTable where myColumn in (1,2,3,4) Is there a way to define a variable to hold the entire list "(1,2,3,4)"? ...

Selecting records during recursive stored procedure

I ve got a content management system that contains a hierarchical structure of categories, with sub-categories subject to different ordering options at each level. Currently, that s retrieved by a (...

热门标签