Trigger on a specific date SQL Server 2014 - sql

I'd like to know if is it possible to trigger a delete on a table, if a date of a row is less than current date?
For example
ID token Expiration_date
---------------------------
1 fkjeorf 2017/05/15 14:05:00
2 gdgrhjj 2017/05/16 13:05:00
3 mojkkut 2017/05/17 18:05:00
If we are on the 2017/05/17 18:00:00, then the two first rows are auto deleted

You can set up a job to delete the records (using SQL Server Agent).
I would recommend that you simply use a view so the data is always accurate:
create view v_example_actives as
select e.*
from example e
where expiration_date < getdate();
Personally, I prefer to keep all the rows in the table (for future analysis). But, you can periodically delete rows at a convenient time. Users of the view will never see expired accounts.

Related

How to check , if all my records are refreshed in a oracle table every friday

I have a table that will get refreshed every Friday on a weekly basis, I need to place a check to make sure , the records that is available in the table is the latest refreshed records before we consume the data.
What is the best way to check using ORACLE SQL query.
The check query could be as follows
select min(update_date) from your_table
If the result is less that the last expected refresh date, you see that at least some rows were not updated.

Show Deleted Records point in time SQL Server

I want to write a function / stored procedure where I want to pass a date and time and get all the rows that exist for a particular date and time, if records were deleted after that date and time it should show them otherwise no.
Let's say I have a table students (id, name, createdate) with 100 rows today morning before 10:30 am, I delete 30 records today at 10:30 am and I run my function with today's date and time 10:35, it should return 70 records, but if I run the function with an older date like today's date at 09:30 am, it should return 100 records.
Please reply with the best possible solutions for this in SQL Server. I don't want to create a separate table of deleted items and then compare and show with that.
DELETE removes the rows, they are not available to view later on.
Unless you are using Sql Server 2016+ and then you could use a System versioned temporal table.
You could create an IsActive column or something to that effect, and instead of DELETE you UPDATE tbl SET IsActive = 0, dateInactivated = getdate() WHERE...
Then in your stored procedure you would:
SELECT *
FROM tbl
WHERE IsActive = 1
OR (IsActive = 0 AND dateInactivated > #myDate)
(EDIT reason: helpful comments below)

Is there a way to tell the last date time a table was modified in SQL Server 2012

Is there a way to tell the last date time any table records were modified in SQL Server 2012? without having to put in a column for last update in table.
The idea is not to query say a 10 million record table when no rows were changed.
Is there a way to tell the last date time any table records were
modified in SQL Server 2012? without having to put in a column for
last update in table.
Yes - you can write an update/insert/delete trigger on the table that records the fact that an update happened along with the date and time.
The idea is not to query say a 10 million record table when no rows
were changed.
You might also want to look at the built in Change Tracking features

Delete rows using table partition

I have a huge log table which is very busy in saving the user logs. My task is to schedule a job which keeps the last 3 days log or last 50k rows (whichever is greater) and deletes the rest. Shall this can be done through TABLE PARTITION.? I can't do this through DELETE statement which is very time expensive and stopping rows to be inserted. The table contains log_time as VARCHAR.
Thanks.
I can suggest you this simple solution:
create every 3 days table named like log-2015-01-28 and write all logs between 2015-01-28 - 2015-01-30 to this table
after 2015-01-30 create new table log-2015-01-28-31 and write all new rows to it
DROP table log-2015-01-28 after 2 days
I think it must work very fast
create trigger on insert
while trigger check the spaceused of the table .
if > 50k then delete the 'oldest row'

How to track number of changes occured in a column? T-SQL - SQL Server

For example, i have a column named EmployeeName.
Every time a user changes/fix his name, i need to keep a count. If he changes his name twice, then count is 2. Also, i need to store the time of every change employee makes on EmployeeName e.g. if the name essentially is James and time created is 9:00 AM and then employee changes to John on 11:00 AM, i need to preserve this new time and new value as well as the previous one which is James at 9:00 AM.
I hope its clear! Thank you guys...Best Answer will be chosen...
If this requires a trigger, giving a sketchy pseudo-code will be very much appreciated.
First up - if not already implemented, highly advisable to have employees identified by a permanent identifier (i.e. NOT EmployeeName) and so you can keep track on everything.
If you want to use a trigger, you could use an AFTER UPDATE trigger and look for a change to the name using if update(EmployeeName).
If that's been updated, you could increment the count column on the Employee table at the same time. Use the inserted table to identify those entries which have been updated. (Is the count essential? If you are storing a history of the name changes, I don't think it's necessary to have a count column - it's redundant informaiton.)
You would then add a row to your employee name history table that holds the details of this change with the current timestamp.
A trigger will suffice. Triggers should be as simple and lightweight as possible, so just insert an entry into some logging table, and compute aggregates on it later.
In SQL Server 2008 you have the new Change Data Capture feature, you could use that to get the number of changes, plus the changes made: http://weblogs.sqlteam.com/derekc/archive/2008/01/28/60469.aspx
Sample Northwind code, as links can die over time:
EXEC Sp_cdc_enable_table
humanresources ,
employee ,
'HumanResources_Employee' ,
1 ,
dbo
UPDATE humanresources.employee
SET ContactId = 1
WHERE employeeid = 1
DECLARE #begin_lsn BINARY(10), #end_lsn BINARY(10)
SELECT #begin_lsn = sys.fn_cdc_get_min_lsn('humanresources_employee')
SELECT #end_lsn = sys.fn_cdc_get_max_lsn()
SELECT * FROM cdc.fn_cdc_get_all_changes_humanresources_employee(#begin_lsn, #end_lsn, 'all');