【问题】用SQL 语句判断表中是否存在password字段,不存在则新增字段,若存在password字段,把字段的类型由int 类型改为text类型。
【实现方法】
if exists( select name from syscolumns where id=object_id('tb_userinfo') and name='password')
begin
alter table tb_userinfo alter column [password] varchar(50)--先把int转化为varchar
alter table tb_userinfo alter column [password] text--再把varchar转化为text
print 'Had upate Email Colums!'
end
else
begin
ALTER TABLE tb_userinfo ADD [password] int NULL
print 'Had Add colums!'
end
【实现方法】
if exists( select name from syscolumns where id=object_id('tb_userinfo') and name='password')
begin
alter table tb_userinfo alter column [password] varchar(50)--先把int转化为varchar
alter table tb_userinfo alter column [password] text--再把varchar转化为text
print 'Had upate Email Colums!'
end
else
begin
ALTER TABLE tb_userinfo ADD [password] int NULL
print 'Had Add colums!'
end