Reputation: 5661
In a nutshell, here's what I'm trying to do:
create function Composite(@year int, @major int, @minor int)
returns int
begin
return(select(@year*10000 + @major*100 + @minor))
end
go
create table AutoGeneratedColumn (
[year] int,
[major] int,
[minor] int,
[composite] int default Composite([year], [major], [minor])
)
(This doesn't work.)
I want the column composite
to be a composite value of year
, major
, and minor
, such that composite
is always equal to Composite(year, major, minor)
. Ideally, explicit values wouldn't be able to be supplied on inserts, and updates should update composite
as well. I'm not sure how to do this, though.
Where should I be looking?
Upvotes: 0
Views: 31
Reputation: 7763
Use a computed column:
create table AutoGeneratedColumn (
[year] int,
[major] int,
[minor] int,
[composite] as ([year]*10000 + [major]*100 + [minor])
)
Upvotes: 2
Reputation: 33581
You shouldn't use a function for this. This is a basic computed column.
create table AutoGeneratedColumn (
[year] int,
[major] int,
[minor] int,
[composite] as ([year]*10000 + major*100 + minor)
)
Upvotes: 1
Reputation: 6713
It's called a computed column. You don't need the function call.
create table AutoGeneratedColumn (
[year] int,
[major] int,
[minor] int,
[composite] AS ([year] * 10000 + [major] * 100 + [minor]),
)
Upvotes: 2