declare @tb varchar(50)
while exists(select * from sysobjects where xtype='U' and name like 'TBL_%')
begin
  select @tb=name from sysobjects where xtype='U' and name like 'TBL_%'
  exec('drop table '+@tb)
end

解决方案 »

  1.   

    declare tb cursor local for
    select N'drop table  '+quotename(o.name)
    from sysobjects o where quotename(o.name) like 'TBL%'
    where 
    objectproperty(o.id,N'IsUserTable')=1
    declare @s nvarchar(4000)
    open tb
    fetch tb into @s
    while @@fetch_status=0
    begin
    exec(@s)
    fetch tb into @s
    end
    close tb
    deallocate tb
      

  2.   

    --改一点
    declare tb cursor local for
    select N'drop table  '+quotename(o.name)
    from sysobjects o where quotename(o.name) like 'TBL%'
    and 
    objectproperty(o.id,N'IsUserTable')=1
    declare @s nvarchar(4000)
    open tb
    fetch tb into @s
    while @@fetch_status=0
    begin
    exec(@s)
    fetch tb into @s
    end
    close tb
    deallocate tb
      

  3.   

    DECLARE @name VARCHAR(8000) 
    set @name=''
    select @name=@name+char(13)+' drop table '+name from sysobjects
    where xtype='U' and name like'TBL%'EXEC (@name)
      

  4.   

    declare @loop int
    declare  @strsql varchar(8000)set @loop = 1
    while @loop < 某个数
    begin
      select @strsql='drop tab_'+convert(varchar,@loop)
      exec(@stsql)
    end---------------------------------------------------------
    declare tb cursor for
    select o.name from sysobjects o where name like 'TBL%' and xtype='U'declare @objectname nvarchar(4000)
    open tb
    fetch tb into @objectname 
    while @@fetch_status=0
    begin
    exec('drop table '+@objectname )
    fetch tb into @objectname 
    end
    close tb
    deallocate tb
      

  5.   

    declare @tb varchar(50)
    while exists(select * from sysobjects where xtype='U' and name like 'TBL_%')
    begin
      select top 1 @tb = name from sysobjects where xtype='U' and name like 'TBL_%'
      exec('drop table '+@tb)
    end
      

  6.   

    pbsql(风云) ( ) 写的挺好的,也好理解
    declare @tb varchar(50)
    while exists(select * from sysobjects where xtype='U' and name like 'TBL_%')
    begin
      select @tb=''  ----把变量·tb设为空值
      select @tb=name from sysobjects where xtype='U' and name like 'TBL_%'
      exec('drop table '+@tb)
      
    end
      

  7.   

    DECLARE @name VARCHAR(8000) 
    set @name=''
    select @name=@name+','+name from sysobjects
    where xtype='U' and name like'TBL%'set @name='drop table '+right(len(@name)-1)
    EXEC (@name)
      

  8.   

    --一句搞定exec sp_msforeachtable @command1='drop table ?',@whereand=' and o.name like ''TBL_%'''
      

  9.   

    请教一下高手们:xtype='U',这一语句是什么意思呢?
      

  10.   

    另外,我是用VB连接SQL Server,在VB中写SQL的时候,“declare @tb varchar(50)”,@这个符号是不是必须的?谢谢!
      

  11.   

    sysobjects 是系统表,记录了数据库中的所有对象情况,xtype这个字段表示对象类型,xtype='U'代表对象类型为用户表无论用什么程序连接,都是原样写sql语句,所以@当然需要,注意,是把sql语句做为一个整体执行,即:
    conn.execute "declare @tb varchar(50) while exists(select * from sysobjects where xtype='U' and name like 'TBL_%') begin  select @tb=''   select @tb=name from sysobjects where xtype='U' and name like 'TBL_%'  exec('drop table '+@tb) end"或者:
    conn.execute "exec sp_msforeachtable @command1='drop table ?',@whereand=' and o.name like ''TBL_%'''"