Sqlserver怎样判断表中一个字段是否已经存在,(50分)

  • 主题发起人 delphing
  • 开始时间
D

delphing

Unregistered / Unconfirmed
GUEST, unregistred user!
我想alter table时,先判断字段是否已经存在了?
谢谢~
 
Select回来遍历Fields里面的每一个Field对象,看看FileName是不是你要的。
笨办法哦~
 
借用goujie的回答,见帖子932289,932280
if Exists (select * from (select sysobjects.name as 表名 ,syscolumns.name as 列名 from sysobjects,syscolumns where sysobjects.id =syscolumns.id) report_zycxj_tj
where report_zycxj_tj.表名='report_zycxj_tj' and report_zycxj_tj.列名='发卡')
print 'Have!'
else
begin
print 'No Have!'
alter table report_zycxj_tj add 发卡 float default 0.00
end
select * from report_zycxj_tj
 
自己写一个函数吧

function FieldIsOn(fieldname: string): boolean;
begin
try
query.close;
query.sql.text := 'select '+fieldname+'from tablename';
query.open;
result := true;
except
result := false;
end;
end;
 
先 select * from ... ,然后就可以得到字段列表了
 
谢谢大家了~
 
顶部