Connecting Tech Pros Worldwide Help | Site Map

If exists table using dynamic sql

Newbie
 
Join Date: Apr 2009
Posts: 28
#1: Jul 30 '09
Hi
I want to check if a particular table exists or not in my DB.But that I have to check using dynamic sql.Can anyone provide me with the syntax
ck9663's Avatar
Expert
 
Join Date: Jun 2007
Posts: 1,925
#2: Jul 30 '09

re: If exists table using dynamic sql


What do you have so far?

-- CK
Newbie
 
Join Date: Apr 2009
Posts: 28
#3: Jul 31 '09

re: If exists table using dynamic sql


I didn't get u?
What I am trying to do is use the following query if that particular table exist in my DB.
The query is

select * from @Tablename

@Tablename is my parameter passed from my prog.
before using the select query I just wanna check if that table exists in my DB if Yes then go for select else return from my SP

could u help out with this?
ck9663's Avatar
Expert
 
Join Date: Jun 2007
Posts: 1,925
#4: Jul 31 '09

re: If exists table using dynamic sql


try:
Expand|Select|Wrap|Line Numbers
  1. IF OBJECT_ID(@Tablename) is not null
  2.   then proceed
  3. else
  4.   table does not exist
  5.  
or

Expand|Select|Wrap|Line Numbers
  1. if exists(select 1 from dbname..sysobjects where name = @Tablename and xtype = 'U')
  2.    then proceed
  3. else
  4.   table does not exists
  5.  
  6.  
Happy Coding!!!

--- CK
Reply