MS SQL triggers. How to over ride a trigger and add a date.

I have a trigger set on the Date feild in the Action Table.  I need to be able to add a different date if the user supplies one.  How can I still keep this trigger?  Or do I have to remove it and add the current date manually every time.


CREATE TRIGGER [Action Date] ON [db129981905].[dbo129981905].[Actions]
FOR INSERT, UPDATE
AS
    SET DATEFORMAT mdy
     update Actions
     Set ActionDate = GetDate()

Where ActionID = @@Identity
tjgilbertAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gad_flyCommented:
Hi

it's better to remove Update from your trigger . and now you can update it with what ever you want .

CREATE TRIGGER [Action Date] ON [db129981905].[dbo129981905].[Actions]
FOR INSERT
AS
    SET DATEFORMAT mdy
     update Actions
     Set ActionDate = GetDate()

Where ActionID = @@Identity


Mohammad Pourebtehaj
imran_fastCommented:
why wont you use default value for the field actiondate?
alter table action alter column actiondate datetime default (getdate())
any how if you want to modify trigger then

CREATE TRIGGER [Action Date] ON [db129981905].[dbo129981905].[Actions]
FOR INSERT, UPDATE
AS
    SET DATEFORMAT mdy
if exists(select * from actions with(nolock) where actionid = @@identity and actiondate is null )

     update Actions
     Set ActionDate = GetDate()

Where ActionID = @@Identity
Aneesh RetnakaranDatabase AdministratorCommented:
CREATE TRIGGER [Action Date] ON [db129981905].[dbo129981905].[Actions]
FOR INSERT, UPDATE
AS
    SET DATEFORMAT mdy
    update Actions
    Set ActionDate = GetDate()
    FROM [db129981905].[dbo129981905].[Actions] inner join inserted ins
    ON Actions.ActionID = ins.ActionID
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

auke_tCommented:
CREATE TRIGGER [Action Date] ON [db129981905].[dbo129981905].[Actions]
FOR INSERT, UPDATE
AS
     update Actions
     Set ActionDate = IsNull(ActionDate, GetDate())
     FROM INSERTED
     Where Actions.ActionID = INSERTED.ActionID

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tjgilbertAuthor Commented:
Thank you for your help this is what I ended up using:

CREATE TRIGGER [Action Date] ON [db129981905].[dbo129981905].[Actions]
FOR INSERT, UPDATE
AS
    SET DATEFORMAT mdy
     update Actions
    Set ActionDate = IsNull(ActionDate, GetDate())

Where ActionID = @@Identity
auke_tCommented:
That will not work correctly if multiple records are updated at once.
You need to use the INSERTED table to do that!
tjgilbertAuthor Commented:
I get an Error 209 : Ambigous column named 'ActionDate'

CREATE TRIGGER [Action Date] ON [db129981905].[dbo129981905].[Actions]
FOR INSERT, UPDATE
AS
    SET DATEFORMAT mdy
     update Actions

     Set ActionDate = IsNull(ActionDate, GetDate())
     FROM INSERTED
     Where Actions.ActionID = INSERTED.ActionID
auke_tCommented:
-- Ok this should work!

CREATE TRIGGER [Action Date] ON [db129981905].[dbo129981905].[Actions]
FOR INSERT, UPDATE
AS

SET NOCOUNT ON

update
    Actions
Set
    ActionDate = IsNull(INSERTED.ActionDate, GetDate())
FROM
    INSERTED
Where
    Actions.ActionID = INSERTED.ActionID
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.