declare @tablename varchar(30) DECLARE cur_temp Cursor For select b.name from dbo.syscolumns a,dbo.sysobjects b where a.name = colname --要删的字段 and a.id = b.id and b.type = 'U' OPEN cur_temp_memid FETCH cur_tempd Into @tablename while @@fetch_status = 0 begin exec('alter table '+@tablename+' drop column colname') FETCH cur_temp Into @tablename endClose cur_temp Deallocate cur_temp
--try exec sp_msforeachtable @command1="truncate table ? ;", @whereand='and schema_id = (select schema_id from sys.schemas where [name] not in(''khda'',''khzh'',''khzmx''))'
declare @sql nvarchar(200) declare c1 cursor for select 'truncate table '+[name] from sysobjects where xtype='U' and [name] not in ('khda','khzh','khzmx','dtproperties')open c1 fetch next from c1 into @sql while @@fetch_status=0 begin exec(@sql) fetch next from c1 into @sql end close c1 deallocate c1
declare @tablename varchar(30) DECLARE cur_temp Cursor For select name from dbo.sysobjects where name not in ('khda','khzh','khzmx' ) and type = 'U' OPEN cur_temp FETCH cur_temp Into @tablename while @@fetch_status = 0 begin exec('truncate table '+@tablename) FETCH cur_temp Into @tablename endClose cur_temp Deallocate cur_temp
动态查sys.sysobjects 或sp_msforeachtable
declare @uname varchar(20) declare cuser cursor for select so.name from sysobjects so,sysusers su where so.uid=su.uid and su.name='khda'--修改成對應的 and so.xtype='U' open cuser fetch next from cuser into @uname while(@@fetch_status=0) begin exec('truncate table [khda].['+@uname+']') --修改成對應的 fetch next from cuser end close cuser deallocate cuser
--删除外键约束 DECLARE c1 cursor for select 'alter table ['+ object_name(parent_obj) + '] drop constraint ['+name+']; ' from sysobjects where xtype = 'F' open c1 declare @c1 varchar(8000) fetch next from c1 into @c1 while(@@fetch_status=0) begin exec(@c1) fetch next from c1 into @c1 end close c1 deallocate c1 --删除表 DECLARE c2 cursor for select 'drop table ['+name +']; ' from sysobjects where xtype = 'u' and [name] not in ('khda','khzh','khzmx','dtproperties') open c2 declare @c2 varchar(8000) fetch next from c2 into @c2 while(@@fetch_status=0) begin exec(@c2) fetch next from c2 into @c2 end close c2 deallocate c2
坎成除khda,khzh,khzmx 三個用戶~~~ 樓主 額的不能用,別把數據給刪除了
sp_msforeachtable @command1='delete ?; ', @whereand=' and o.name not in (''khda'',''khzh'',''khzmx'')'
DECLARE cur_temp Cursor For
select b.name from dbo.syscolumns a,dbo.sysobjects b
where a.name = colname --要删的字段
and a.id = b.id
and b.type = 'U'
OPEN cur_temp_memid
FETCH cur_tempd Into @tablename
while @@fetch_status = 0
begin
exec('alter table '+@tablename+' drop column colname') FETCH cur_temp Into @tablename
endClose cur_temp
Deallocate cur_temp
exec sp_msforeachtable @command1="truncate table ? ;",
@whereand='and schema_id = (select schema_id from sys.schemas where [name] not in(''khda'',''khzh'',''khzmx''))'
declare c1 cursor for
select 'truncate table '+[name]
from sysobjects
where xtype='U'
and [name] not in ('khda','khzh','khzmx','dtproperties')open c1
fetch next from c1 into @sql
while @@fetch_status=0
begin
exec(@sql)
fetch next from c1 into @sql
end
close c1
deallocate c1
DECLARE cur_temp Cursor For
select name from dbo.sysobjects
where name not in ('khda','khzh','khzmx' )
and type = 'U'
OPEN cur_temp
FETCH cur_temp Into @tablename
while @@fetch_status = 0
begin
exec('truncate table '+@tablename) FETCH cur_temp Into @tablename
endClose cur_temp
Deallocate cur_temp
declare cuser cursor for
select so.name
from sysobjects so,sysusers su where so.uid=su.uid and su.name='khda'--修改成對應的
and so.xtype='U'
open cuser
fetch next from cuser into @uname
while(@@fetch_status=0)
begin
exec('truncate table [khda].['+@uname+']') --修改成對應的
fetch next from cuser
end
close cuser
deallocate cuser
DECLARE c1 cursor for
select 'alter table ['+ object_name(parent_obj) + '] drop constraint ['+name+']; '
from sysobjects
where xtype = 'F'
open c1
declare @c1 varchar(8000)
fetch next from c1 into @c1
while(@@fetch_status=0)
begin
exec(@c1)
fetch next from c1 into @c1
end
close c1
deallocate c1
--删除表
DECLARE c2 cursor for
select 'drop table ['+name +']; '
from sysobjects
where xtype = 'u' and [name] not in ('khda','khzh','khzmx','dtproperties')
open c2
declare @c2 varchar(8000)
fetch next from c2 into @c2
while(@@fetch_status=0)
begin
exec(@c2)
fetch next from c2 into @c2
end
close c2
deallocate c2
樓主 額的不能用,別把數據給刪除了
@command1='delete ?; ',
@whereand=' and o.name not in (''khda'',''khzh'',''khzmx'')'
1.用它禁用这些表的约束
2.用它清除这些表的记录(用delete较好,truncate table对有外键约束的主表不能先清除)
3.用它启用这些表的约束