Domanda

I have a table with 2 columns dateRecieved and dateDue. I know how to make the dateNow column show today's date by default by using: DEFAULT GETDATE() The user can then modify the dateRecieved column to whatever date they want.

After the user modifies the dateRecieved column; how do I set up the dateDue column so that it, by default, shows a date x days away from dateRecieved?

I looked at this answer but I need to refer to another column. Need to add constraint: date plus 10 days

Is there a way that dateRecieved can modify its value automatically when dateRecieved changes? The user should be able to override the default values in both these columns.

I'm using the W3 schools sql tutorial but it didn't have and answer I couldn't find one on stackoverflow either http://www.w3schools.com/sql/sql_default.asp

use current date as default value for a column

Thanks

È stato utile?

Soluzione

So when you say the "user modifies" you are talking about an UPDATE.

DEFAULT values are relevant to INSERT statements only.

It is true you can accomplish whatever automagical UPDATE change you want via triggers but as a beginner, I would squarely advise you away from triggers. They are rarely implemented correctly and make understanding when things go wrong more difficult.

What you want to do is INSERT your row(s) (allowing any DEFAULT values to "happen") and then UPDATE your row(s) with the values you intended.

Altri suggerimenti

It sounds like what you're looking for is a trigger that waits until the update is finished, then the dateDue automatically updates.

This answer may point you in the right direction: How to: Create trigger for auto update modified date with SQL Server 2008

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top