sql怎么样删除表中指定字段以外的所有字段
2个回答
展开全部
专门写的 请采纳 要注意主键有约束 需要单独处理
ALTER TABLE 表名 DROP CONSTRAINT 默认约束名
其他的
USE 数据库名
DECLARE @columnname varchar(250)
DECLARE @sqltext varchar(250)
DECLARE curTbls CURSOR FOR
SELECT syscolumns.name FROM syscolumns,sysobjects WHERE syscolumns.id = sysobjects.id AND sysobjects.name = '表名' and syscolumns.name!='不想删除的列名'
OPEN CurTbls
FETCH NEXT FROM curTbls INTO @columnname
WHILE @@FETCH_STATUS=0
BEGIN
SELECT @sqltext = 'alter table drop column COLUMN' + @columnname
PRINT @sqltext
FETCH NEXT FROM curTbls INTO @columnname
END
CLOSE CurTbls
DEALLOCATE curTbls
ALTER TABLE 表名 DROP CONSTRAINT 默认约束名
其他的
USE 数据库名
DECLARE @columnname varchar(250)
DECLARE @sqltext varchar(250)
DECLARE curTbls CURSOR FOR
SELECT syscolumns.name FROM syscolumns,sysobjects WHERE syscolumns.id = sysobjects.id AND sysobjects.name = '表名' and syscolumns.name!='不想删除的列名'
OPEN CurTbls
FETCH NEXT FROM curTbls INTO @columnname
WHILE @@FETCH_STATUS=0
BEGIN
SELECT @sqltext = 'alter table drop column COLUMN' + @columnname
PRINT @sqltext
FETCH NEXT FROM curTbls INTO @columnname
END
CLOSE CurTbls
DEALLOCATE curTbls
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询