SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Calculated column Only for Insert


Calculated column Only for Insert

Author
Message
Sony Francis @EY
Sony Francis @EY
SSC-Enthusiastic
SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)

Group: General Forum Members
Points: 107 Visits: 293
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
Kingston Dhasian
Kingston Dhasian
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3685 Visits: 5176
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/
ScottPletcher
ScottPletcher
SSCertifiable
SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)

Group: General Forum Members
Points: 7782 Visits: 7140
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)[size=2]Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial: If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.[/size]
dwain.c
dwain.c
SSCertifiable
SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)

Group: General Forum Members
Points: 7209 Visits: 6431
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!

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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
R.P.Rozema
R.P.Rozema
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1087 Visits: 1685
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
Posting Performance Based Questions - Gail Shaw
Hidden RBAR - Jeff Moden
Cross Tabs and Pivots - Jeff Moden
Catch-all queries - Gail Shaw


If you don't have time to do it right, when will you have time to do it over?
dwain.c
dwain.c
SSCertifiable
SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)

Group: General Forum Members
Points: 7209 Visits: 6431
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. :-D


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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)

Group: General Forum Members
Points: 84847 Visits: 41069
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search