Showing posts with label columncs. Show all posts
Showing posts with label columncs. Show all posts

Thursday, March 8, 2012

Column's default value in 2005

HI,
Dose column's default value can be set as 2 columns' "+, - , *, / " ?
I want ColumnC's default value as ColumnA/ColumnB.
Any idea? Thanks for any advice!
Angi
angi a crit :
> HI,
> Dose column's default value can be set as 2 columns' "+, - , *, / " ?
> I want ColumnC's default value as ColumnA/ColumnB.
> Any idea? Thanks for any advice!
> Angi
>
NO... Default and Not Null constraint can only be set on a single row.
To do that, you must use a trigger :
CREATE TRIGGER <trig_name> ON <table_name>
FOR INSERT
AS
UPDATE <table_name>
SET ColumnC = ColumnA/ColumnB
FROM <table_name> T
INNER JOIN inserted i
ON T.<key_col> = i.<key_col>
WHERE ColumnC IS NULL
A +
Frdric BROUARD, MVP SQL Server, expert bases de donnes et langage SQL
Le site sur le langage SQL et les SGBDR : http://sqlpro.developpez.com
Audit, conseil, expertise, formation, modlisation, tuning, optimisation
********************* http://www.datasapiens.com ***********************