SQL 指定表名和列名,刪除對應的列

2021-06-04 00:53:36 字數 987 閱讀 6366

create procedure [dbo].[proc_dropcolumn]

@tablename varchar (30),

@columnname varchar (30)

as/*

功能:刪除字段,同時刪除約束

*/ if not exists

(select *

from syscolumns a inner join sysobjects b on a.id = b.id

where b.name = @tablename and a.name = @columnname)

return 0

/*查詢約束*/

declare @contraint varchar (50)

set @contraint = ''

select @contraint = b.name

from sysobjects a

inner join sysobjects b

on a.id = b.parent_obj

inner join syscolumns c

on b.id = c.cdefault and c.name = @columnname

where a.name = @tablename

/*執行刪除*/

declare @sqltext varchar (200)

if @contraint <> ''

begin

set @sqltext = ' alter table ' + @tablename + ' drop constraint ' + @contraint

exec (@sqltext)

endset @sqltext = ' alter table ' + @tablename + ' drop column ' + @columnname

exec (@sqltext)

SQL刪除表名包含指定字元的所有表

create procedure dbo.proc deltable as declare tablename as nvarchar 128 declare cur del cursor for 以下有3種不同的篩選方式,反注釋即可使用 刪除表名以kcpd開頭的所有表 kcpd select na...

SQL查詢包含自增列的表名和列名

sqlserver自增列判斷 簡單的判斷語句 sql2000以上 if columnproperty object id tb col isidentity 1 print 自增列 else print 不是自增列 sql2000以上查詢所有自增列字段 select 表名 b.name,欄位名 a....

R語言按照行名或列名刪除對應的行列

按照行名或列名刪除對應的行列 data colnames data in c b d 函式比which好用,因為which如果選中不存在的行列名,將刪除所有內容 x grep b colnames x x grep b d colnames x x grepl b d colnames x grep...