There was another question for you. I do the trigger. There are two columns with dates, first I want to calculate with the help of a trigger the difference in days between these dates, and then from 30 to subtract the resulting number ...

The difference is considered normal, and with the second part there is a problem: it does not work correctly. What do you tell me, here is the code:

CREATE TRIGGER TR1 ON Table1 FOR INSERT,UPDATE AS UPDATE Table1 set KolvoDays=DATEDIFF(DAY,StartDate,FinishedDate) UPDATE Table1 set Ostatok=30-(select KolvoDays from inserted where inserted.NumID=NumID) 

    2 answers 2

    Update in the trigger is written incorrectly. You need to use the FROM clause if it is SQL Server.

    Again, if this is SQL Server, then the trigger is not needed at all. Make the Residual column a calculated column, i.e. write the formula there:

     30 - DATEDIFF(DAY,StartDate,FinishedDate) 
       select @KolvoDays=KolvoDays from inserted where inserted.NumID=NumID set Ostatok=30-@KolvoDays 
      • I don’t enter, it’s clear about the variable, everything works there, but the problem remains ... ALTER TRIGGER TR1 ON Table1 FOR INSERT, UPDATE AS declare @KolvoDays int UPDATE Holiday set KolvoDays = DATEDIFF (DAY, StartDate, FinishedDate) select @ KolvoDays = KolvoDays from inserted where inserted. NumlID = NumID set Ostatok = 30- @ KolvoDays As a result, in the last line:> Incorrect syntax near '='. - wicS 2:53 pm
      • > set Ostatok = 30- @ KolvoDays This is not an update, then you need to declare a variable and write so set @ Ostatok = 30- @ KolvoDays - msi