水滴石穿

渴望成为高手--Amy.Qiu
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

SQL更改数据库多表column属性

Posted on 2008-05-08 10:31  AmyQiu  阅读(582)  评论(0编辑  收藏  举报

drop table #a

select identity(int,1,1) as Id, table_name, column_name, Is_Nullable, Data_Type
into #a
from INFORMATION_SCHEMA.COLUMNS where column_name='ModelID' and Data_Type<>'Bigint'

select * from #a

declare @Id int
declare @table nvarchar(50)
declare @column nvarchar(50)
declare @is_nullable nvarchar(50)
set @Id=1

while @Id<=(select max(id) from #a)
begin
 set @table=(select table_name from #a where id=@id)
 set @column=(select column_name from #a where id=@id)
 set @is_nullable=case when (select is_nullable from #a where id=@id)='No' then 'not null' else '' end;
print @table
print @column
print @is_nullable
 alter table @table alter @column Bigint @is_nullable
 set @id=@id+1
 continue
end