If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Sybase > how to exit if the table already exists

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-02-08, 14:24
tiger66 tiger66 is offline
Registered User
 
Join Date: Dec 2007
Posts: 32
how to exit if the table already exists

Hi All
In my script to a create table, how can check if the table exists? If the table exists, don't show any error message and don't try to create table

How come the following doesn't work?

Code:
if (not exists (select 1 from sysobjects where name="test" and type="U"))
begin
        print "eeeeeeeeeeeeee"
create table test
(
        test char(1) not null,
)
end
else
        print "aaaaaaaaaa"
go
I get the following error message
"There is already an object named 'test' in the database"



Please help
Reply With Quote
  #2 (permalink)  
Old 05-02-08, 23:58
sco08y sco08y is offline
Registered User
 
Join Date: Oct 2002
Location: Baghdad, Iraq
Posts: 697
Not a sybase guy, but does your conditional work? I mean, without the CREATE TABLE statement, does it print "aaa" instead of "eee"?
Reply With Quote
  #3 (permalink)  
Old 05-05-08, 01:10
tiger66 tiger66 is offline
Registered User
 
Join Date: Dec 2007
Posts: 32
After I create the "test" table, I tried applying the code.
The screen prints aaaaaaaaa. However, before it prints aaaaaaaa, it also shows the error message that "test" table already exists error message (but eeeeeee doesn't get printed)

Is it possible to modify the code, so that it doesn't print the table already exists error message?
Reply With Quote
  #4 (permalink)  
Old 05-06-08, 16:02
corral corral is offline
Registered User
 
Join Date: Aug 2002
Location: Madrid, Spain
Posts: 83
Embed the create table statement in a exec() function:

exec ('create table...')

Regards,
Mariano
Reply With Quote
  #5 (permalink)  
Old 05-17-08, 05:23
samirbadhe samirbadhe is offline
Registered User
 
Join Date: May 2008
Posts: 10
RE: how to exit if the table already exists

Hi tiger66,

This is not a runtime error, this is a compilation error.

Try this ..

1. Make sure 'test' table does not exists on database initially.

2. Create a proc for this

CREATE PROC sp_tigers
as
if (not exists (select 1 from sysobjects where name="test" and type="U"))
begin
print "eeeeeeeeeeeeee"
create table test
(
test char(1) not null,
)
end
else
print "aaaaaaaaaa"
go

3. Once compiled you should be able run it any times, without that error ..

cheers...
-samir
TechFruits.com
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On