PDA

View Full Version : The Database Table DOES Exist!


wylbur
06-07-2008, 03:10 AM
Hi;


I'm trying to determine if a database table exists, and create it if
it doesn't.

I have tried both of the following (with various variations thereof):
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

cmd.CommandText
= "IF EXISTS (SELECT * FROM sys.objects"
+ " WHERE object_id = OBJECT_ID('[dbo].[dbo.MyDatabaseTable]')"
+ " AND type in ('U')) "
+ " select 1 else select 0 ";


cmd.CommandText
= "IF EXISTS (SELECT * FROM sysobjects"
+ " WHERE name='dbo.MyDatabaseTable' AND Type='U')"
+ " select 1 else select 0";
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-



When I ExecuteScalar, I get a value of zero - even though the table is there:

test = (int) cmd.ExecuteScalar ();


The Database Table DOES Exist!


That (of course) throws the following exception:

### Exception Number : 2714
### (There is already an object named 'MyDatabaseTable' in the database.) has occurred
### Message: There is already an object named 'MyDatabaseTable' in the database. Number: 2714
### Procedure: [] Server: SIMPELSO-EML8V3\SQL2005DEV01
### Source: .Net SqlClient Data Provider State: 6 Severity: 16 LineNumber: 1


I'm running ASP.NET 2.0, with SQL Server version 5, on Windows XP Pro.


Any help would be appreciated.


Thanks In Advance.

wylbur
06-07-2008, 08:45 PM
This problem has been solved.

vinyl-junkie
06-08-2008, 05:30 AM
How did you solve it? Your solution might be useful to others who might run across this thread.