sp_msforeachtable 'truncate table ?'
如果有主外键,不适用

解决方案 »

  1.   

    生成sql脚本,删除表后,重新执行。
      

  2.   

    有人告诉我说:
    declare @name varchar(50)
    DECLARE cur_test CURSOR FOR
           SELECT name FROM sysobjects where xtype='u' 
        OPEN cur_test 
          FETCH NEXT FROM cur_test  into @name 
    WHILE @@FETCH_STATUS =0
       BEGIN
                  exec ('truncate table '+@name)
              FETCH NEXT FROM cur_test  into @name 
               end
        close  cur_test  
       DEALLOCATE  cur_test但是执行后有如下的信息:
    Server: Msg 170, Level 15, State 1, Line 1
    Line 1: Incorrect syntax near 'Details'.
    Server: Msg 4712, Level 16, State 1, Line 1
    Cannot truncate table 'Code' because it is being referenced by a FOREIGN KEY constraint.
    第二个好像就是主外键引起的。
      

  3.   

    對啊﹐有外鍵約束時就不能用
    truncate table 表名
      

  4.   

    那先删除有外键约束的表再执行  sp_msforeachtable "truncate table ?"