SQL 判断表是否存在?
如果不存在就创建表
存在就不创建了,大家帮忙看看
解决马上给分!!!

解决方案 »

  1.   

    if not exists(select name from sysobjects where name='+@DBTableName+' and type='u')   
      

  2.   

    if object_id('[Table1]') is null 
    create table [Table1] ([col1] int,[col2] int)
      

  3.   


    if not exists (select * from sysobjects where [name] = '表名' and xtype='U')
    begin
    --这里创建表
    end
      

  4.   

    if not exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Table1]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
       create table [Table1] ([col1] int,[col2] int)
      

  5.   

    汗 少了个单引号
    if not exists(select name from sysobjects where name=''+@name+'' and type='u') 
    begin
    -- 创建
    end
     
      

  6.   

    这是个例子if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[result]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[result]
    GOCREATE TABLE [dbo].[result] (
    [rid] [int] IDENTITY (1, 1) NOT NULL ,
    [rdate] [datetime] NOT NULL ,
    [name] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
    [software_e] [numeric](18, 1) NOT NULL ,
    [rule] [numeric](18, 1) NOT NULL ,
    [platform] [numeric](18, 1) NOT NULL ,
    [recorder] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL 
    ) ON [PRIMARY]
    GO
      

  7.   


    请教 xtype 和type 有什么区别
      

  8.   

    Type是在6.0就有的,XType在7.0才出现:Type
    对象类型。可以是下列值之一:
    C = CHECK 约束
    D = 默认值或 DEFAULT 约束
    F = FOREIGN KEY 约束
    FN = 标量函数
    IF = 内嵌表函数
    K = PRIMARY KEY 或 UNIQUE 约束
    L = 日志
    P = 存储过程
    R = 规则
    RF = 复制筛选存储过程
    S = 系统表
    TF = 表函数
    TR = 触发器
    U = 用户表
    V = 视图
    X = 扩展存储过程XType
    对象类型。可以是下列对象类型中的一种:
    C = CHECK 约束
    D = 默认值或 DEFAULT 约束
    F = FOREIGN KEY 约束
    L = 日志
    FN = 标量函数
    IF = 内嵌表函数
    P = 存储过程
    PK = PRIMARY KEY 约束(类型是 K)
    RF = 复制筛选存储过程
    S = 系统表
    TF = 表函数
    TR = 触发器
    U = 用户表
    UQ = UNIQUE 约束(类型是 K)
    V = 视图
    X = 扩展存储过程貌似在这里用哪个都行……
      

  9.   


    if object_id('表名') is not null
      drop table 表名
    go
    create table 表名(colname coltype,....)
      

  10.   

    if not exists (select * from sysobjects where [name] = '表名' and xtype='U')
    begin
    --这里创建表
    end
      

  11.   

    还有个问题,这个在 SYBASE 数据库里不行
    麻烦各位再看看
      

  12.   

    http://topic.csdn.net/t/20011211/16/415243.html
      

  13.   

    既然执行成功了,那你刷新一下数据库试试,或者创建完马上从这个新表中select 1 from 新表名,看有没有错
      

  14.   

    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[表名]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
    drop procedure [dbo].[表名]
    GO
      

  15.   

    那select 1 from 新表名报错没?
    有没有select出1来?
      

  16.   

       你建立的表是在你使用的数据库么?在建立表的前面加一个
     use 你要使用的数据库
    /*使用数据库*/
    use ComManageDB
    go/*判断该表是否已经存在*/
    if exists ( select * from sys.objects where name ='UserInfo')
    drop table UserInfo
    go/*创建UserInfo表*/
    create table UserInfo
    (
    ........
    这样看看
      

  17.   

    Select   1   from   sysobjects   where   name   =   ‘yourTableName’;   
        
      或者:   
        
      Select   1   from   sysobjects   where   id   =   object_id('yourTableName');   
        
      顺便说一下:SYBASE   ANYWHERE可以用object_id函数.   
      但是不能用   
      if   object_id(‘yourTableName’)   is   null   
      create   table   yourTableName   
      …   
      这样的方法来处理.但是在SQL   Server中可以.   
      

  18.   

    /*使用数据库*/
    use ComManageDB
    go/*判断该表是否已经存在*/
    if exists ( select * from sys.objects where name ='UserInfo')
    drop table UserInfo
    go/*创建UserInfo表*/
    create table UserInfo
    (
    ........你这样写那不是如果UserInfo存在,就先删除这个表
    然后再创建?
      

  19.   

    其实表, 视图, 存储过程, 函数等等, 都可以用 Object_id函数来判断
    表的if( object_id('LZ的表') is not null )
    drop table LZ的表
    go
    create table LZ的表
    (
    字段1 int ,
    字段2 int
    )
    go
    存储过程的if( object_id('LZ的存储过程') is not null )
    drop procedure LZ的存储过程
    go
    create procedure LZ的存储过程
    @参数1 int ,
    @参数2 int
    as
    begin tran-- 过程而已commit
    go
      

  20.   

    if not exists(select name from sysobjects where name='+@DBTableName+' and type='u')   
      

  21.   

    if not exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[表名]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    beginend
    GO
      

  22.   

    if not exists(select name from sysobjects where name='+@DBTableName+' and type='u')   
      

  23.   


    if not exists(select name from sysobjects where name='+@DBTableName+' and type='u')