--在同一个存储过程给某个表添加列,还要使用这一列,需要用动态SQL语句,如:create proc test asexec ('alter table Student add age int')update student set age=datediff(yy,birthday,getdate()) go
create proc test as--先判断一下列是否存在,如果不存在,才添加列 exec ('if not exists (select * from syscolumns where id=object_id(''Student'') and name=''age'') alter table Student add age int')update student set age=datediff(yy,birthday,getdate()) go
asexec ('alter table Student add age int')update student set age=datediff(yy,birthday,getdate())
go
as--先判断一下列是否存在,如果不存在,才添加列
exec ('if not exists (select * from syscolumns where id=object_id(''Student'') and name=''age'')
alter table Student add age int')update student set age=datediff(yy,birthday,getdate())
go