Prady Prady - 5 months ago 6
SQL Question

how to check if table exist and if it doesnt exist create table in sql server 2008

I am writing a

Stored procedure
in sql server 2008
I need to check if a
table
exist in the DB, if it doesnt then i need to create it.

How do I do it?

Thanks
Prady

Answer

Something like this

IF  NOT EXISTS (SELECT * FROM sys.objects 
WHERE object_id = OBJECT_ID(N'[dbo].[YourTable]') AND type in (N'U'))

BEGIN
CREATE TABLE [dbo].[YourTable](
    ....
    ....
    ....
) 

END
Comments