top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to Check existence of any table using PL-SQL in SQL server 2008?

0 votes
428 views
How to Check existence of any table using PL-SQL in SQL server 2008?
posted Mar 19, 2016 by Sathyasree

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

In SQL server 2008 we can check existence of any table by its name using If Exist function and after that we can perform any operation based on result of if exist function. It is very simple to implement in PL-SQL

Query:

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

BEGIN

Drop table Users 

END

In this query we are checking table existence before dropping it.

We can also check other way around in this query it is also very much simple

Query:

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

BEGIN

CREATE TABLE [dbo].[Login](

      [UserID] [varchar](50) NOT NULL,

      [Password] [varchar](20) NULL,

      [Type] [varchar](20) NULL,

PRIMARY KEY CLUSTERED

(

      [UserID] ASC

)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]

) ON [PRIMARY]

END
answer Mar 19, 2016 by Shivaranjini
...