数据库中有50几个表
我想删除:除了khda,khzh,khzmx这3个表之外的所有用户表中的记录
该怎样写SQL语句
谢谢!!!!

解决方案 »

  1.   

    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
      

  2.   

    从sysobjects中找出你需要删除数据的用户表,然后跑循环truncate前提是没有外键什么的约束,有的话只能先删除约束,我记得有现成的脚本的,不过我忘了收藏了=.=!
      

  3.   

    --try
    exec sp_msforeachtable @command1="truncate table ? ;",
                           @whereand='and schema_id = (select schema_id from sys.schemas where [name] not in(''khda'',''khzh'',''khzmx''))'
      

  4.   

    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
      

  5.   

    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
      

  6.   

    动态查sys.sysobjects 或sp_msforeachtable
      

  7.   

    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
      

  8.   

    --删除外键约束
    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
      

  9.   

    坎成除khda,khzh,khzmx 三個用戶~~~
    樓主 額的不能用,別把數據給刪除了
      

  10.   

    sp_msforeachtable 
    @command1='delete ?; ',
    @whereand=' and o.name not in (''khda'',''khzh'',''khzmx'')'
      

  11.   

    三次sp_msforeachtable 
    1.用它禁用这些表的约束
    2.用它清除这些表的记录(用delete较好,truncate table对有外键约束的主表不能先清除)
    3.用它启用这些表的约束