注册账号 登录
用友之家-用友软件论坛 返回首页

wozengcong的个人空间 https://www.oyonyou.com/?242966 [收藏] [复制] [分享] [RSS]

日志

下文为您介绍的SQL语句可以实现判断字段是否存在,并判断添加列的表中是否有主键

已有 1013 次阅读2013-4-5 15:15 |个人分类:SQL数据库资料

  1. --判断要添加列的表中是否有主键      
  2. if exists(select 1 from sysobjects where parent_obj=object_id('tb') and xtype='PK')      
  3. begin     
  4. print '表中已经有主键,列只能做为普通列添加'     
  5.      
  6. --添加int类型的列,默认值为0      
  7. alter table tb add 列名 int default 0      
  8. end     
  9. else     
  10. begin     
  11. print '表中无主键,添加主键列'     
  12.      
  13. --添加int类型的列,默认值为0      
  14. alter table tb add 列名 int primary key default 0      
  15. end     
  16. /**************************************************************************************/      
  17.      
  18. 判断table1中是否存在name字段      
  19. if exists(select * from syscolumns where id=object_id('table1') and name='name') begin     
  20. select * from people;      
  21. end     
  22.  
  23. 判断table1中是否存在name字段且删除字段   
  24. if exists(select * from syscolumns where id=object_id('table1') and name='name') begin     
  25. select * from people;     
  26. alter table table1 DROP COLUMN name   
  27. end  

路过

雷人

握手

鲜花

鸡蛋

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 注册账号

QQ|站长微信|Archiver|手机版|小黑屋|用友之家 ( 蜀ICP备07505338号|51072502110008 )

GMT+8, 2024-5-15 22:54 , Processed in 0.023484 second(s), 10 queries , Gzip On, Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

返回顶部