創建一個表前先判斷它是否存在?
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[aa]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[aa]
GO

解决方案 »

  1.   

    創建一個表前先判斷它是否存在?if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[aa]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[aa]
    GOCREATE TABLE [dbo].[aa] (
    [id] [int] IDENTITY (1, 1) NOT NULL ,
    [person_no] [numeric](5, 0) NULL ,
    [name] [varchar] (10) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL ,
    [indate] [datetime] NULL 
    ) ON [PRIMARY]
    GO
      

  2.   

    if not exists (select 1 from sysobjects where type='u' and name ='table1')
    begin
    select * into table1 from table2 where 1=2
    end
    --至于添加主键,非空等约束以及索引,比较麻烦,要大量搜索系统表.
    --如果楼主的这个语句只是针对一个表的,建议还是把建表的脚本放进来比较合适.
      

  3.   

    if exists(select * from sysobjects where id = OBJECT_ID(N'tableName') and OBJECTPROPERTY(id, 'IsUserTable') = 1)
    begin
    -- 存在时执行相关的操作
    end
    else
    begin
    -- 不存在时根据 tableName 表的结构创建一个新表
    select * into newTableName from tableName where id is null
    end
      

  4.   

    if not exists (select 1 from sysobjects where type='u' and name ='table1')
    begin
        select * into Table1 from Table2 --复制整个Table2到Table1
        delete from Table1 --删除里面数据,只留表结构
    end
      

  5.   

    --上面有点问题,这样:
    if not exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Table1]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    begin
        select * into Table1 from Table2 --复制整个Table2到Table1
        delete from Table1 --删除里面数据,只留表结构
    end
      

  6.   

    if not exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Table1]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    begin
        select * into Table1 from Table2 where 1=2 --复制整个Table2到Table1
    end这样就不用删除数据记录了