zoukankan      html  css  js  c++  java
  • SQL删除字段及判断字段是否存在的方法

    下面为您介绍使用SQL语句如何增加、删除、修改字段,并判断字段是否存在的详细语句写法,供您参考,希望对您有所帮助。

    增加字段
    alter table docdsp  add dspcode char(200)
    删除字段
    ALTER TABLE table_NAME DROP COLUMN column_NAME
    修改字段类型
    ALTER TABLE table_name  ALTER COLUMN column_name new_data_type
    改名
    sp_rename
    更改当前数据库中用户创建对象(如表、列或用户定义数据类型)的名称。
    语法
    sp_rename [ @objname = ] 'object_name' ,
        [ @newname = ] 'new_name'
        [ , [ @objtype = ] 'object_type' ]

    --假设要处理的表名为: tb

    --判断要添加列的表中是否有主键
    if exists(select 1 from sysobjects where parent_obj=object_id('tb') and xtype='PK')
    begin
     print '表中已经有主键,列只能做为普通列添加'

    --添加int类型的列,默认值为0
     alter table tb add 列名 int default 0
    end
    else
    begin
     print '表中无主键,添加主键列'

    --添加int类型的列,默认值为0
     alter table tb add 列名 int primary key default 0
    end
    /**************************************************************************************/

    判断table1中是否存在name字段
    if exists(select * from syscolumns where id=object_id('table1') and name='name') begin
    select * from people;
    end

    转自http://database.51cto.com/art/201009/223550.htm

  • 相关阅读:
    End of 2007, where are you?
    2007年,前进!
    PHP框架Yii快速搭建高并发网站
    需要 gmail 与 wallop 邀请的 请留言给 email
    开始关注Mono了
    百度,阿里巴巴,google
    重返 cnblogs.com
    解决VMware虚拟机桥接不能上网的问题
    SecureCRT
    linux的ssh和sshd配置
  • 原文地址:https://www.cnblogs.com/yiwd/p/2831822.html
Copyright © 2011-2022 走看看