Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

GETUTCDATE() return null Expand / Collapse
Author
Message
Posted Wednesday, December 18, 2013 5:43 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Sunday, January 05, 2014 5:16 AM
Points: 64, Visits: 153
I have trigger after Insert that update field in the table with GETUTCDATE() but after Insertion I see the value = null


SET NOCOUNT ON
UPDATE [dbo].[Employee]
SET [CurrentDate] = GETUTCDATE()
FROM inserted
WHERE inserted.[Id] = [dbo].[Employee].[Id]


Post #1524416
Posted Wednesday, December 18, 2013 8:12 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 8:53 AM
Points: 35,983, Visits: 30,273
ali.m.habib (12/18/2013)
I have trigger after Insert that update field in the table with GETUTCDATE() but after Insertion I see the value = null


SET NOCOUNT ON
UPDATE [dbo].[Employee]
SET [CurrentDate] = GETUTCDATE()
FROM inserted
WHERE inserted.[Id] = [dbo].[Employee].[Id]




It appears that none of that code is the actual problem. Please post all of the code for the trigger.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1524432
Posted Wednesday, December 18, 2013 8:14 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 8:53 AM
Points: 35,983, Visits: 30,273
I take that back... that's actually an illegal form of update that will sometimes work and sometimes not. I'll explain more after I see the rest of the code for the trigger.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1524434
Posted Wednesday, December 18, 2013 11:03 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 4:39 AM
Points: 3,594, Visits: 5,105
Jeff Moden (12/18/2013)
I take that back... that's actually an illegal form of update that will sometimes work and sometimes not. I'll explain more after I see the rest of the code for the trigger.


I look forward to hearing that explanation.

In the meantime, wouldn't something like this work?

    SET NOCOUNT ON 
UPDATE a
SET [CurrentDate] = GETUTCDATE()
FROM [dbo].[Employee] a
JOIN inserted b
ON a.[Id] = b.[Id]





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1524448
Posted Thursday, December 19, 2013 7:28 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 9:29 AM
Points: 11,994, Visits: 11,012
Jeff Moden (12/18/2013)
I take that back... that's actually an illegal form of update that will sometimes work and sometimes not. I'll explain more after I see the rest of the code for the trigger.


I was about to make that same comment but noticed you had. I know what the problem is but I am having a hard time finding the explanation of why it happens.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1524575
Posted Thursday, December 19, 2013 7:32 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Sunday, January 05, 2014 5:16 AM
Points: 64, Visits: 153
Jeff Moden (12/18/2013)
I take that back... that's actually an illegal form of update that will sometimes work and sometimes not. I'll explain more after I see the rest of the code for the trigger.
+
this all what the trigger do no more no less
Post #1524576
Posted Saturday, December 21, 2013 10:23 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 8:53 AM
Points: 35,983, Visits: 30,273
ali.m.habib (12/19/2013)
Jeff Moden (12/18/2013)
I take that back... that's actually an illegal form of update that will sometimes work and sometimes not. I'll explain more after I see the rest of the code for the trigger.
+
this all what the trigger do no more no less


You don't understand. I need to see ALL the trigger code because there are other nuances in a trigger that could be important.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1525278
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse