一日一步 之SQLServer数据库的表操作

      select * into NewTables from LawInfo where 1=2   --复制表结构
      delete newTables                      
--删除表数据
      drop table newTables                   --删除表以及表结构

     Alter table MyTableName Drop constraint 约束名称 --删除表关系

      Alter table MyTableName Drop column 字段名称   --删除表字段
      
Alter table MyTableName NoCheck constraint all    --禁用所有约束

---------接下来是某小牛的存储过程,以实现查询出表的所有外键关系,并删除-----------

代码如下:

Create   proc   DeleteSingleTable(@tablename   varchar(100))
as
begin
declare   @SQL   varchar(2000)
declare   @constraintName   varchar(100)

declare   curName   cursor   for
select   name   from   sysobjects  
where   xtype   =   'f '   and   parent_obj   =
(select   [id]   from   sysobjects   where   [name]=@tablename   and   xtype   =   'u ')

open   curName
fetch   next   from   curName   into   @constraintName
while   @@fetch_status   =   0
begin
set   @SQL   =   'alter   table   '   +   @tablename   +   '   drop   constraint   '
set   @SQL   =   @SQL   +   @constraintName
exec(@SQL)
fetch   next   from   curName   into   @constraintName
end
close   curName
deallocate   curName
end

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值