Groups | Blog | Home
all groups > vb.net data > june 2005 >

vb.net data : SQL Logic to test if exists part 2


Programmer
6/30/2005 12:00:00 AM
After the check that database exist
Write this in SQL

USE MyDatabase
if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[MY_TABLE]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[MY_TABLE]



;)


[quoted text, click to view]

RSH
6/30/2005 9:10:05 AM
I am having to accomplish two things in a single SQL statement.

I need to test first to see if a DATABASE Exists and if it Does...Drop it
and create it again (changes were made)...if it doesnt exist just create it.

This code seems to work good:
IF NOT EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name =
'MyDatabase5')
CREATE DATABASE MyDatabase5
GO

The second part is to check if a table exists in the database...if it does
Drop it...and then recreate it...if it doesnt create it.

This is the code I have but it keeps giving me an error saying the table
already exists so it appears the logic is not working:



AddThis Social Bookmark Button