Calculated column Only for Insert

  • I have a table with two columns created date and mofified date. I have a condition like

    1. When the operation in insert then Created date is set as getdate. When update happens then created date is not chnaged.

    2. When the operation is update then modiifed date as getdate. WHen insertion happens modified date is null.

    Can I achive it by using calculated field column.

    Note :- Trigger is not applicable here

  • I think you are talking about the computed column in SQL Server

    You cannot achieve the desired result using computed columns

    If trigger is not applicable, then there is only one option

    Step 1. Define the column Created Date with a default value of GETDATE() or make sure you insert the value for the column in every INSERT query in your application

    Step 2. Define the column Modified Date as NULLable and update it manually in every UPDATE query in your application


    Kingston Dhasian

    How to post data/code on a forum to get the best help - Jeff Moden
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • Sony Francis @EY (7/20/2012)

    ...

    2. When the operation is update then modiifed date as getdate. WHen insertion happens modified date is null.

    ...

    Note :- Trigger is not applicable here

    If you don't use a trigger, you can't trust the value in the modified date -- UPDATEr might have forgotten to set it ... or deliberately set it to the wrong date.

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.

  • Kingston's solution is true enough as it goes, but you can also assign a default constraint to the update date column and then use this whenever a row is updated.

    As a demonstration:

    create table #t1 (Invoice_no int, item VARCHAR(5), price MONEY

    ,Inserted_date DATETIME DEFAULT GETDATE()

    ,Updated_date DATETIME DEFAULT GETDATE())

    insert into #t1 (invoice_no, item, price)

    select 102,'#1', 6.21

    union all select 102,'#2', 3.56

    union all select 102,'#3', 4.28

    union all select 105,'#4', 1.90

    union all select 105,'#5', 3.66

    union all select 107,'#6', 2.01

    SELECT Invoice_no, item, price, Inserted_date, updated_date

    FROM #T1

    UPDATE #T1

    SET price = 5.00

    ,updated_date = DEFAULT

    WHERE invoice_no = 107

    SELECT Invoice_no, item, price, Inserted_date, updated_date

    FROM #T1

    DROP TABLE #T1

    You would of course, just need to remember to do so, or force it to happen in a trigger.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    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?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • Then while you're at it, don't use getdate() for the timestamp on your audit data and don't use current_timestamp either. Instead I suggest to use getutcdate().

    Using getutcdate() ensures that a) your audit trail won't be broken when daylight saving time changes (UTC does not have day light saving). And b) when the data is moved from one timezone into another -for example by replicating the data into a server on another continent- the audit information will be unambigous.



    Posting Data Etiquette - Jeff Moden[/url]
    Posting Performance Based Questions - Gail Shaw[/url]
    Hidden RBAR - Jeff Moden[/url]
    Cross Tabs and Pivots - Jeff Moden[/url]
    Catch-all queries - Gail Shaw[/url]


    If you don't have time to do it right, when will you have time to do it over?

  • R.P.Rozema (7/23/2012)


    Then while you're at it, don't use getdate() for the timestamp on your audit data and don't use current_timestamp either. Instead I suggest to use getutcdate().

    Using getutcdate() ensures that a) your audit trail won't be broken when daylight saving time changes (UTC does not have day light saving). And b) when the data is moved from one timezone into another -for example by replicating the data into a server on another continent- the audit information will be unambigous.

    Good point RP!

    Too many of us operate in our own little worlds (call them time zones if you prefer). In Thailand, we only have one time zone and no daylight savings time. So I'm safe from all but the pundits. 😀


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    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?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • CELKO (7/20/2012)


    We use CURRENT_TIMESTAMP, not getdate() now.

    Heh... no... some of us don't believe in the myth of portability. Actually, I think both functions are silly because they're named wrong.

    --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.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply