July 20, 2012 at 12:04 am
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
July 20, 2012 at 12:13 am
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
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
July 20, 2012 at 2:45 pm
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.
July 22, 2012 at 7:44 pm
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 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
July 23, 2012 at 2:27 am
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.
July 23, 2012 at 2:33 am
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 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
July 23, 2012 at 5:41 pm
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
Change is inevitable... Change for the better is not.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply