数据库中有许多如下形式的表:
a 表有字段bh,mc ,bz
b 表中有字段bh,mc ,bz
c 表中有字段bh1,mc ,bz
d 表有字段mc ,bz
e,f,g...
清空记录的规则
1,表名为e,f,g 的记录不能清空
2,数据表中无 “bh” 字段的表的记录不能清空,比如表c,d 等
3,写一存储过程,传入参数@bh,在满足1,2 条件的情况下,清空数据库中
表,字段bh等于传入参数的表的记录

解决方案 »

  1.   

    Create Proc SP_ClearTables
    @col nvarchar(100)
    asdeclare @table nvarchar(100);declare curTables cursor for select name from sys.objects where type='U' and name not in('e','f','g') 
    and  object_ID not in (select a.object_ID from sys.columns a inner join sys.objects b 
    on a.object_id = b.object_id where b.type='U' and a.name ='ID');
    open curTables;
    fetch curTables into @table;
    while @@fetch_status =0
    begin
    exec(N'truncate table ' + @table );
    fetch curTables into @table;
    end
    close curTables;
    deallocate curTables;
      

  2.   

    上面那个有一个地方写错了,正确的是
    Create Proc SP_ClearTables
    @col nvarchar(100)
    asdeclare @table nvarchar(100);declare curTables cursor for select name from sys.objects where type='U' and name not in('e','f','g') 
    and  object_ID not in (select a.object_ID from sys.columns a inner join sys.objects b 
    on a.object_id = b.object_id where b.type='U' and a.name =@col);
    open curTables;
    fetch curTables into @table;
    while @@fetch_status =0
    begin
    exec(N'truncate table ' + @table );
    fetch curTables into @table;
    end
    close curTables;
    deallocate curTables;
      

  3.   

    declare @colName varchar(20)
    set @colName ='bh'
    declare CKCr cursor
    for select [name] from sysobjects where xtype='U' and [name]<>'dtproperties'
    open CKCr 
    declare @tblName sysname
    fetch CKCr into @tblname
     begin 
     if (@colname<>'e') and (@colname<>'f') and (@colname<>'g') 
     begin 
       if exists(select * from sys.columns where [object_id]=OBJECT_ID(N'+@tblname+') and name=@colName)
        exec ('delete from '+@tblname)
     end
    end
    while @@fetch_status=0
    begin
      fetch next from CKCr into @tblname 
      begin 
        if (@colname<>'e') and (@colname<>'f') and (@colname<>'g') 
        begin 
         if exists(select * from sys.columns where [object_id]=OBJECT_ID(N'+@tblname+') and name=@colName)
          exec ('delete from '+@tblname)
        end
      end 
    end
    close CKCr试试
      

  4.   

    谢谢你的回答,我清空表中的记录是要带条件的truncate table 好像不能带条件吧?
      

  5.   

    type='U' 和
    sysobjects where xtype='U' 这表示什么?
      

  6.   

    谢谢你的解答, 我需要 delete from tblname where bh=@bh 这样清空表的记录
      

  7.   

    truncate tabel  不能带条件,
      

  8.   

    Create Proc SP_ClearTables
    @bh nvarchar(100)
    as
    declare CKCr cursor
    for select [name] from sysobjects where xtype='U' and [name]<>'dtproperties' and [name]<>'e' and [name]<>'f' and [name]<>'g'
    open CKCr 
    declare @tblName sysname
    fetch CKCr into @tblname
      if exists(select * from sys.columns where [object_id]=OBJECT_ID(N'+@tblname+') and name=@bh)
        exec ('delete from '+@tblName +' where bh='+@bh)
    while @@fetch_status=0
    begin
      fetch next from CKCr into @tblname 
      begin 
        if exists(select * from sys.columns where [object_id]=OBJECT_ID(N'+@tblname+') and name=@bh)
          exec ('delete from '+@tblName+' where bh='+@bh)
      end 
    end
    close CKCr
      

  9.   

    借4楼的改改,看成不Create Proc SP_ClearTables
    @col nvarchar(100)
    as
    declare @table nvarchar(100);declare curTables cursor for select name from sys.objects where type='U' and name not in('e','f','g') 
        and  object_ID not in (select a.object_ID from sys.columns a inner join sys.objects b 
        on a.object_id = b.object_id where b.type='U' and a.name ='bh');
    open curTables;
    fetch curTables into @table;
    while @@fetch_status =0
    begin
        exec(N'delete from ' + @table+' where bh= '+@col);
        fetch curTables into @table;
    end
    close curTables;
    deallocate curTables;
      

  10.   

    在2000中报
    对象名 'sys.objects' 无效。
    对象名 'sys.columns' 无效。
    对象名 'sys.objects' 无效。
      

  11.   

     dbo.sysobjects  不是'sys.objects' 
      

  12.   

    测试通过了if( object_id('pr_LZ') is not null )
    drop procedure pr_LZ
    go
    create procedure pr_LZ @bh varchar(50)
    as
    begin
    declare @nID int
    declare @cName varchar(100)
    declare @nCnt int
    declare @cSQL varchar(2000)
    -- 1,表名为e,f,g 的记录不能清空
    declare myCur cursor for
    select [ID], [name] 
    from sysobjects 
    where type = 'U' and 
    [name] not in( 'e', 'f', 'g' )
    open myCur 
    fetch next from myCur into @nID, @cName
    while @@fetch_status=0
    begin
    select @nCnt = count(1)
    from syscolumns 
    where [id] = @nID and 
    name = @bh
    -- 2,数据表中无 “bh” 字段的表的记录不能清空,比如表c,d 等 
    if( @nCnt > 0 )
    begin
    set @cSQL = 'delete from ' + @cName
    exec( @cSQL )
    end 
    fetch next from myCur into @nID, @cName
    end
    close myCur
    deallocate myCur
    end
    go