December 5, 2006 at 9:17 am
Is there such a thing as calling a function to populate a column when adding a column using ALTER table?
A developer proposed this but I've not heard of it. It seems like it would be the functionality of an insert trigger, but he's saying it's a function.
Anyone have an example?
thanks!
December 5, 2006 at 9:25 am
I thaught about this but it's not accepted.
ALTER TABLE tableName
ADD Whatever INT CONSTRAINT D_CName DEFAULT (dbo.fn_name(ColName))
How about adding the column, then running the update statement right after?
December 5, 2006 at 9:36 am
i believe you can use a calculated column as well; here's an example that uses a function:
CREATE FUNCTION GetAge (@BirthDate datetime, @CurrentDate datetime)
RETURNS int
AS
BEGIN
return year(@CurrentDate) - year(@BirthDate)
- case when month(@CurrentDate)
> month(@BirthDate)
then 0
else
case when month(@CurrentDate)
< month(@BirthDate)
then 1
else
case when day(@CurrentDate)
< day(@BirthDate)
then 1
else 0
end
end
end
END
create table age(
ageid int identity(1,1) not null primary key,
dob datetime,
calcage as dbo.getage(dob,getdate() ) )
or if i was adding this to an existing table:
ALTER TABLE OLDAGE ADD calcage as dbo.getage(dob,getdate() )
Lowell
December 5, 2006 at 10:48 am
If you post schema and explains your question based on the schema... Posters can post exact solution instead of guessing.
MohammedU
Microsoft SQL Server MVP
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply