check if table exists sql server

Possible Duplicate:
SQL Server: Check if table exists

I'm working with Java and MS SQL Server 2008 and I want to create a table only after checking if it not already exists in the database.

 public void addTestTable(){
  jdbcTemplate.execute(
            "create table [mydatabase].[dbo].[test] (ID integer not null identity, CREATEDBY varchar(50), CREATEDAT datetime, TITLE varchar(50), NRQUEST int, FORGROUP int, primary key(id))"

            );

}

This is my createTable function, I need another boolean function for checking if the table already exists, but I don't know how to write the sql statement for it. Could anyone please help me?


IF OBJECT_ID('tablename','U') is not null
-- table exists 

要么

SELECT *
   FROM sys.tables
   WHERE name = 'mytable'
   AND schema_id = SCHEMA_ID('myschema')


if not exists(select 1 from sys.tables where name ='test' and schema_id = SCHEMA_ID('dbo'))
begin
  create table [dbo].[test] (ID integer not null identity, CREATEDBY varchar(50), 
  CREATEDAT datetime, TITLE varchar(50), NRQUEST int, FORGROUP int, primary key(id)) 
  print 'table created'
end
go

IF NOT EXISTS (SELECT * 
                 FROM INFORMATION_SCHEMA.TABLES 
                 WHERE TABLE_SCHEMA = 'testSchema' 
                 AND  TABLE_NAME = 'testTable')
BEGIN
    --create table
END
链接地址: http://www.djcxy.com/p/94442.html

上一篇: 使用存在1或存在的子查询*

下一篇: 检查表是否存在sql服务器