现在如果我想删除10个表
而表是是 table_1,table_2,table_3....
用循环怎么处理

解决方案 »

  1.   

    declare @i as int
    select @i=1
    Declare @str as nvarchar(1000)while @i<=10
    begin
    select @str=isnull(@str,'') + 'delete table table'+ltrim(@i)+';'
        select @i=@i+1
    end
    print(@str)
    exec(@str)
      

  2.   

    declare @i as int 
    select @i=1 
    Declare @str as nvarchar(1000) while @i <=10 
    begin 
    select @str=isnull(@str,'') + 'delete table table_'+ltrim(@i)+';' 
        select @i=@i+1 
    end 
    print(@str) 
    exec(@str)
      

  3.   

    declare @i int 
    declare @sql varchar(1000)
    set @sql = ''
    set @i = 1
    while (@i <=10)
      begin
        select @sql =@sql+'delete from table'+cast(@i as varchar(10)) +';'
        set @i=@i+1
     end
    print (@sql)
      

  4.   

    exec sp_msforeachtable @command1='drop table ? ' ,@whereand=' and o.type=''u'' and o.name like ''table[0-9]%'''
      

  5.   


    --trydeclare @str nvarchar(4000)
    set @str=''
    select @str=@str +' truncate table ' + name from sysobjects where type='u' and name like 'table_[1-10]'
    exec(@str)