Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check the existence of a table in a stored procedure

Status
Not open for further replies.

choic

MIS
Apr 27, 2001
5
HK
Hi there

Is there any way to check a table's existence before its creation in one stored procedure?? I did something as follows but it does not work. Please help me ...

Declare @sql varchar(255)
Select @sql = 'create table test (column_1 int primary key)
If (Exec @sql) <> 0
print 'Failed'
Else
print 'Done'
 
Hiya,

Try the following

IF EXISTS (SELECT * FROM sysobjects WHERE name = 'table_name' AND type +'U')
DROP TABLE table_name

IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'table_name' AND type +'U')
CREATE TABLE table_name (
blah blah blah)

You will need to make sure that the table does NOT exist when you create the stored proc, or Sybase will throw an error at the create table command.

HTH

Tim
 
Sorry,

fingers don't work today!!!

That should have read and TYPE = 'U')

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top