Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Find table exists in database and if yes then check if field exists in the table

Posted on 2010-09-13
11
Medium Priority
?
322 Views
Last Modified: 2012-05-10
Hi,
Need to combine two queries in one .
example:      SQL = "SELECT Name AS TABLE_NAME FROM sysobjects WHERE type = 'U' ORDER BY Name"
             if yes then check if 'refno' field exists in the table

Cheers
0
Comment
Question by:RIAS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 33661850
select * from information_schema.columns
where table_name = 'tablename' and column_name = 'columnname'
0
 

Author Comment

by:RIAS
ID: 33661905
Hi,
Thanks for the reply.But need to get all tables from sysobjects and check if field'refno' exists in them .At the moment I have query

sql= "SELECT Name AS TABLE_NAME FROM sysobjects WHERE type = 'U' ORDER BY Name"

strSql = " Select COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS Where TABLE_NAME = '" & Table & "'" & " AND COLUMN_NAME ='" & Field & "'"
 
Need to combine in 1 query ?
Cheers
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 33661921
Ah, in that case just take out your table_name criteria:

strSql = " Select Table_name, COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS Where COLUMN_NAME ='" & Field & "'"
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:RIAS
ID: 33661957
Cheers mate will try and get back
0
 

Author Comment

by:RIAS
ID: 33662386
Hi,
The query returns only tables with field 'refno' whereas I need to know both the cases.One with refno field and one without.
Cheers
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 33662405
I am not sure I follow.  You want to see ALL tables, and an indication if the table has that field or not?
0
 

Author Comment

by:RIAS
ID: 33662440
yes correct.
But  the query return all the table which have 'refno' field in them  and does not give the table names with no refno field in them
 
0
 
LVL 60

Accepted Solution

by:
chapmandew earned 1000 total points
ID: 33662461
SELECT Table_Name,
CASE WHEN EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS C WHERE T.Table_Name = c.Table_name AND
c.Column_name = 'RefNo') THEN 'EXISTS' ELSE 'DOES NOT EXIST' END
FROM INFORMATION_SCHEMA.Tables T
0
 

Author Comment

by:RIAS
ID: 33662528
Hi,
Got this error :<

The multi-part identifier "T.Table_Name" could not be bound.
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 33662539
show me your query
0
 

Author Closing Comment

by:RIAS
ID: 33662732
Cheers mate!!!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question