Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Defaulting column to another column's value

Author  Topic 

simondeutsch
Aged Yak Warrior

547 Posts

Posted - 2002-12-18 : 11:53:37
Is this possible? (check subject)

Sarah Berger MCSD

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2002-12-18 : 12:05:56
Not with a simple default constraint. A default can only be a constant expression.


Jay White
{0}
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2002-12-18 : 13:12:52
I'd say do it using the client, if possible, or at the point the data is getting into the table.

For example, if it is an INSERT query, put the logic in there.

- Jeff
Go to Top of Page

mohdowais
Sheikh of Yak Knowledge

1456 Posts

Posted - 2002-12-19 : 03:07:00
I dont know if this qualifies as a default... but I created a table that looks like this:

CREATE TABLE [dbo].[Table1] (
[colA] [varchar] (50) COLLATE SQL_Latin1_General_CP1256_CI_AS NULL ,
[colB] AS ([ColA])
) ON [PRIMARY]
GO

...and it works just fine.

What I find surprising is that I can hardly find any documentation on this syntax. Its not a constraint, not a default, what is it then? In SQL Server 2000 table design mode, it appears in the Formula property. When I type "Formula" into the BOL...nothing!
Sheesh!

OS

Go to Top of Page

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2002-12-19 : 06:46:51
quote:

What I find surprising is that I can hardly find any documentation on this syntax. Its not a constraint, not a default, what is it then?



It's called a COMPUTED COLUMN. You can't manually update it or insert into it. In SQL 2K, you can index it ...

Jay White
{0}
Go to Top of Page

mohdowais
Sheikh of Yak Knowledge

1456 Posts

Posted - 2002-12-19 : 09:10:08
Aaahh! thanx for the enlightenment, Jay!
And yes, I forgot to mention that it can't be updated. Not very useful to have ColB AS (ColA) then, is it? I could imagine it being of use like ColC AS (ColA + ' ' + ColB)

OS

Go to Top of Page

Lavos
Posting Yak Master

200 Posts

Posted - 2002-12-22 : 15:42:08
Hi Sarah,

It depends on what you want to do. I have a feeling that you want to be able to leave the column out of the values list when inserting a new row, and have it default to the value of another column instead.

I would personally advise against it, but it's possible to fake it by using an insert trigger. Unfortunately, you'd then have to make the column nullable, and the trigger wouldn't be able to differentiate between intentionally inserted nulls and nulls inserted because you want it to default to a value.

I think you (and whoever is trying to maintain it afterwords) might be able to keep your sanity when maintaining the code by having it explicitly set instead of relying on defaults. YMMV.

----------------------
"O Theos mou! Echo ten labrida en te mou kephale!"

So Long and Thanks for the Links.
Go to Top of Page
   

- Advertisement -