Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

i checked this sql code with swl enterprise manager and it says "Syntax check successful!". But if i run my code it says unhandled exception, system error on the executenonquery line...

 

string createtbl = "CREATE TABLE  typicals (typical varchar(200),typicalnr int);";
SqlCommand myinsertcommand = new SqlCommand(createtbl, mycon);
myinsertcommand.ExecuteNonQuery();

 

actually i want to use "create table if not exists", but if i check the syntax from this command it won't accept the "if not exists" part...

 

can someone help me?

 

thx in advance.

  • *Experts*
Posted

To check for the table before you create, you need to check the sysobjects table. Below is a simple check:

if 0 = (select count(*) from sysobjects where name = 'typicals')
begin
CREATE TABLE  typicals (typical varchar(200),typicalnr int)
end 

 

I'd leave off the semicolon, only because it's not necessary.

 

If you want a more robust "object exists" check, use Enterprise Manager to script out the create/drop statement for you. It does a lot more to check if a table exists including checking the type of the object in the sysobjects table.

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...