By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,856 Members | 2,149 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,856 IT Pros & Developers. It's quick & easy.

If exists table using dynamic sql

P: 31
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
Jul 30 '09 #1
Share this Question
Share on Google+
3 Replies


ck9663
Expert 2.5K+
P: 2,878
What do you have so far?

-- CK
Jul 30 '09 #2

P: 31
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?
Jul 31 '09 #3

ck9663
Expert 2.5K+
P: 2,878
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
Jul 31 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.