if not exists (select * from dbo.syscolumns where id = object_id(N'[dbo].[t]') and name = 'sum_num') BEGIN ALTER TABLE t ADD sum_num int NULL update t set sum_num=dbo.function(num) END GO
alter table tb add column id varchar(100) default dbo.function(num go
不改表结构直接得结果: select name,num,sum_num=function(num) from t
alter table tb add id varchar(100) default dbo.ddfunction(num)
--楼主应该是这个意思吧?--示例--处理函数 create function f(@name varchar(10)) returns int as begin return(select sum(num) from 表 where name=@name) end go--创建表 create table 表(name varchar(10),num int,sum_num as dbo.f(name)) go--插入数据 insert 表 select '张梅芳',2007 union all select '张小文',2012 union all select '李援平',2009--显示结果 select * from 表 order by name go--再次插入数据 insert 表 select '段定友',2010 union all select '阚静坤',2013 union all select '阚静坤',2013 union all select '张霞',2015--显示结果 select * from 表 order by name go--删除测试 drop table 表 drop function f/*--测试结果name num sum_num ---------- ----------- ----------- 李援平 2009 2009 张梅芳 2007 2007 张小文 2012 2012(所影响的行数为 3 行) (所影响的行数为 4 行)name num sum_num ---------- ----------- ----------- 段定友 2010 2010 阚静坤 2013 4026 阚静坤 2013 4026 李援平 2009 2009 张梅芳 2007 2007 张霞 2015 2015 张小文 2012 2012(所影响的行数为 7 行) --*/
BEGIN
ALTER TABLE t ADD sum_num int NULL
update t set sum_num=dbo.function(num)
END
GO
add column
id varchar(100) default dbo.function(num
go
select name,num,sum_num=function(num) from t
add
id varchar(100) default
dbo.ddfunction(num)
create function f(@name varchar(10))
returns int
as
begin
return(select sum(num) from 表 where name=@name)
end
go--创建表
create table 表(name varchar(10),num int,sum_num as dbo.f(name))
go--插入数据
insert 表 select '张梅芳',2007
union all select '张小文',2012
union all select '李援平',2009--显示结果
select * from 表 order by name
go--再次插入数据
insert 表 select '段定友',2010
union all select '阚静坤',2013
union all select '阚静坤',2013
union all select '张霞',2015--显示结果
select * from 表 order by name
go--删除测试
drop table 表
drop function f/*--测试结果name num sum_num
---------- ----------- -----------
李援平 2009 2009
张梅芳 2007 2007
张小文 2012 2012(所影响的行数为 3 行)
(所影响的行数为 4 行)name num sum_num
---------- ----------- -----------
段定友 2010 2010
阚静坤 2013 4026
阚静坤 2013 4026
李援平 2009 2009
张梅芳 2007 2007
张霞 2015 2015
张小文 2012 2012(所影响的行数为 7 行)
--*/