Extract results updated in last 2 hours - sql-server-2008

I am trying to write a query to extract records updated in the last 2 hours in SQL Server 2008.
Could anyone help me write this?

select * from table where table.date1>=dateadd(hh,-2,getdate())
dateadd() function lets you subtract hours from getdate() letting you choose records updated past 2 hours

First, you have to design the table so you have a field where the time of the last change will be stored
Then, whenever you update a row, update the value in the 'last update' field. After that, you can use a script such as suggested by Vijaykumar
The downside of this method is that when a single record was changed more than once in the specified time period, you will be notified only about the time of the last update.
Another solution for tracking the updates is to read the database online transaction log file, but you'll need a third party tool for that

Related

Fetching rows changed or added last day without time field?

I want query a table to find the changed rows last day. But there is no explicit time filed in column name. Is there any way I can access these data?
Without a field indicating when a record was changed there is no way to determine when a record was changed, therefore it is unfortunately impossible for you to query the records changed yesterday.

Delete Row at Set Time

I'm creating a database table where rows need to be removed after a set time. That time is defined in minutes by the valid_time cell in that row. I found this answer though I am not sure how I can implement what I need to into it.
Is someone able to tell me how I can implement this time (as minutes) into the event in the previous answer or if it's not possible, another way to do so. Thanks.
Clarification, I have two columns in the table. One is created which is a TIMESTAMP of when the row is created, and the second is valid_time, an integer in minutes of how long the row is valid for.
DELETE FROM table WHERE created < DATE_SUB(NOW(), INTERVAL `valid_time` MINUTE)
You can try to use the MySQL event scheduler and attach a DELETE query to it. That DELETE will be a simple query that will delete all records where current_time is greater that the valid_time/valid_until fields.
You can configure the scheduler to run in a minute/hourly/daily/... basis as you wish to erase the registers.
Check here and here for more information. M0rtiis offered the query example.

New records since last query in MySQL View

I am looking for a way to create a View that when queried will automatically only retrieve new records since the last query. My tables have a timestamp field for all entries, so for a simple example I can
SELECT * WHERE timestamp >= 'blah'
but I don't know how to determine what blah should be from the last query. So if the View was queried at 11:00 and then again at 12:00, the query at 12:00 should only return records added since 11:00. And so on... This all needs to be accomplished in the View, the end user should simply be able to query the View and get the results.
Is this possible?
There are two ways:
Store last access date time in database per user persistent session
table, if you have one. On next view call to database, use the
previous latest access time in the session to filter rows starting
from.
Store last access date time in user virtual session at client
environment. On every call to server, send last access date time as
well. So that server uses it to filter rows starting from.
I prefer to use second option that process won't write any data in database tables.
As there may be an unread record that slips through undetected (say it came less than a second since the last one accessed, so it has the same timestamp), set a column to auto increment (typically labelled id) and check for entries using it e.g. in PHP save the last accessed record in a $lastId variable, and use:
$sql="SELECT * WHERE `id` > '$lastId'";

Retrieving sql database records based on time continuously

I have an online database CUSTOMERINFO with more than 100k details stored like following format Cust Id, Customer name, Addr, Phone,.......,Call back time
I want to retrieve data when call back time equals current time automatically.
I have designed front end with Java and currently 10 employees working with the database and now they are manually retrieving the data by ID,..
I know select command is very useful to retrieve but I want it to do automatically instead of calling each time manually.
Edited:
When the customer data is retrieved from table, either we will set another call back time or no call back and then pushed into table again.. In the next time if no call back is set in the place of call back time that row no need to be retrieved.
I'm likely missing something here, but:
Something like SELECT ... FROM table WHERE 'Call back time' >= NOW()
I'd recommend not using simply equals, as has been said in the comments, because you might miss items.
If you update the callback time after you have retrieved the callback items, that should work as long as you do not do the query to get callbacks more than once every few seconds.
As was mentioned in the comments, this is just the start though. There are going to be other issues you'll have to deal with.

Make a data field in MySQL database update automatically each day or week?

I have a field in a MySQL database which i want to add a certain number each day or week whichever is easier to achieve. For example i have a field with the number 2000 in it and everyday or week i want to add 200 to that number.
Is this possible and if so how can i achieve it?
Just write a SQL function which will return the field times number of week for example.
I do not post a complete function because the question is not that clear about what you expected to return