Trigger only works when you perform an action in the database (Insert, update, delete...). I believe that the best solution in your case would be to schedule the task.
In SQL Server you can use SQL Server Agent for scheduling. This service is not available in SQL EXPRESS versions. If this is your version you can use the Windows Task Scheduler to schedule a bat that runs your SQL script.
Situation 1 - SQL Server Agent
You need to activate the service first. How to do this:
- Click Start, click Programs, then click SQL Server 2008.
- Click Configuration Tools, then click SQL Server Configuration Manager.
- Expand SQL Server 2008 services.
- Locate the SQL Server Agent service. The SQL Server Agent service is called "SQL Server Agent" for standard instances and "SQL Server
Agent (instance name)" for named instances.
- Click SQL Server Agent and then click Properties.
- On the Logon tab, click to select this account check box. Specify a different account name and password.
- In the status section of the service, click Start, then click Ok.
After the service started you need to create Job. To do this go to Management Studio and, in Object Explorer (side tab where you have the database and the tables) you will have the option SQL Server Agent and there you can create a new job.
Situation 2 - Express version of SQL SERVER
If you use the Express version of SQL SERVER SQL Server Agent will not start. To run your script then you can use the task scheduler to do this service for you "manually"
To do this create a file . bat with the following command:
sqlcmd -i script.sql
In the same folder save the script.sql file with the SQL code you want to run. Then create a task in the Windows task scheduler that runs this bat every day at a specific time.
A command you can use to accomplish this task of adding more time on the date, for your example, is the DATEADD() adding an integer value to a part of the date of its choice.
Sorry to ask but why don’t you use the
now()
in the query itself ?– Bulfaitelo
I’m sorry but I’m layman still, how do I use this now()
– Merlin
For example, when you change the type, you should run a query like this
update tabela set type = 0 where id usernum
instead you could useupdate tabela set type = 0, expiredate = now() where id usernum
with this the date will catch the exact time of the update– Bulfaitelo
@Bulfaitelo doesn’t really want a
trigger
. He wants you to change automatically when you win– Sorack
@Sorack Truth I understood the opposite.
– Bulfaitelo
I saw a staff commenting on another similar question to mount a job that check every n minutes, but do not know how to do
– Merlin
@Merlin Normally this kind of thing I would do in my application, for example using crontab or windows task scheduler
– Bulfaitelo
@Merlin soon after lunch I write a reply
– Sorack
@Thank you so much for your attention
– Merlin
you also @Bulfaitelo
– Merlin
Related: Dynamic insertion according to date and days of the week
– Sorack
You use sql or mysql server?
– Leticia Rosa
@Leticiarosa sql server 2008
– Merlin
@Merlin you can’t just create a view and bring that value filled in according to the date?
– Sorack
@Merlin just posted an answer to sql server
– Leticia Rosa