tags:

views:

95

answers:

6

Hey, I am trying to put a little logic into my C# app that will create a table called Import, IF it doesn't already exist.. here is my code, it doesn't seem to work tho.

con.Open();
                    using (SqlCommand cmd = con.CreateCommand())
                    {
                        cmd.CommandText =
                            @" 
                       IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'RX_CMMData' AND TABLE_NAME = 'Import'))
BEGIN
CREATE TABLE Import (
  RowId     integer PRIMARY KEY NOT NULL,
  PartNumber  varchar(200) NOT NULL,
  CMMNumber   varchar(200) NOT NULL,
  Date        varchar(200) NOT NULL,
  FeatType    varchar(200) NOT NULL,
  FeatName    varchar(200) NOT NULL,
  Value       varchar(200) NOT NULL,
  Actual      decimal,
  Nominal     decimal,
  Dev         decimal,
  TolMin      decimal,
  TolPlus     decimal,
  OutOfTol    decimal,
  FileName    varchar(200) NOT NULL
); END";
                        cmd.ExecuteNonQuery();
                    }
                    con.Close();
A: 

Some DBMSes allow you to do CREATE TABLE IF NOT EXISTS <tablename> inside the SQL. I'd check the syntax for your DBMS.

sheepsimulator
Oracle allows CREATE OR REPLACE
pm_2
If you do a CREATE OR REPLACE in Oracle, will that delete the data that is currently stored in the table, or only alter the table definition? It doesn't feel like quite the same thing.
sheepsimulator
No - if the table exists then the data will be fine (providing you don't make any changes that would affect the data, like removing a column or something)
pm_2
A: 

syntax of that under sql server 2005 is

  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tablename]') AND type in (N'U'))
  BEGIN
   create
  END

in your code, where is the = FALSE or NOT keyword

Fredou
+7  A: 

Your SQL is creating the table if it does exist and not if it doesn't.

Change the SQL to read "IF NOT EXISTS".

Marc Tidd
+1 Well Spotted!
Martin Smith
Still doesn't work.. Once it is created, I run the app again and it fails stating 'table Import already exists.
jakesankey
What do you see if you run `SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Import'`? (See Andomar's answer)
Martin Smith
Yes, you are querying INFORMATION SCHEMA looking for TABLE_SCHEMA = 'RX_CMMData' but your create statement is creating the table in the default schema which is probably dbo. See syntax in Andomar's answer.
Marc Tidd
A: 

If you're using Oracle, you could try

select count(*) from user_tables
where table_name = ''

which should tell you if the table exists

pm_2
+2  A: 

You're probably creating the table in the default schema (typically dbo.)

Specify the full name, like:

CREATE TABLE RX_CMMData.Import
Andomar
A: 

You can also look into using Microsoft SMO objects. I prefer to work SMO objects and let them do the work for me if possible instead of executing SQL text through code.

Using the SMO Database object you can do the following.

Database db = myServer.Databases["MyDB"];

if (! db.Tables.Contains("NewTable"))
{

    Table tbl = new Table(db, "NewTable");

    Column col1 = new Column(tbl, "Column1", DataType.Varchar(10));
    col1.Nullable = true;
    tbl.Columns.Add(col1);

    tbl.Create();

}

http://www.mssqltips.com/tip.asp?tip=1826

dretzlaff17
Different strokes and all that but I find it much easier to just write the SQL Script than need to create all the objects and set the properties as in your example code.
Martin Smith