?
Solved

How to check if a column exists in a tablle

Posted on 2008-10-16
8
Medium Priority
?
1,249 Views
Last Modified: 2012-05-05
I have a proc p_GetData that is invoked by many other procs,
 A temporary table is passed to the proc p_GetData as a paramater, @TableName  

I would like to know if a particular column exists in the temp table, can someone provide the sql for that

CREATE PROCEDURE p_GetData
    @TableName  varchar(100),
    @ServerName varchar(50),
    @UpdateFlag int,
    @SuccessFlag tinyint OUTPUT
AS



0
Comment
Question by:countrymeister
[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
  • 4
  • 3
8 Comments
 
LVL 8

Expert Comment

by:rpkhare
ID: 22734972
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE
TABLE_NAME = 'MyTable' AND COLUMN_NAME = 'abcd'
0
 
LVL 1

Author Comment

by:countrymeister
ID: 22735081
rpkhare:

Doe this work in SQL Server 2000
0
 
LVL 8

Accepted Solution

by:
rpkhare earned 750 total points
ID: 22735097
I think it will.
0
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 1

Author Comment

by:countrymeister
ID: 22735710
Can you please let me know which is better
SELECT Count(*) FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = '@TableName' AND COLUMN_NAME = 'Action'

select object_name(id) as TableName, * from syscolumns where lower(name) like 'action'
and object_name(id) = '@TableName'
0
 
LVL 8

Expert Comment

by:rpkhare
ID: 22737860
The former seems to be more close to what you are desiring.
0
 
LVL 2

Expert Comment

by:Deepika_Rastogi
ID: 22739095
Hi

here in this query
SELECT Count(*) FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = '@TableName' AND COLUMN_NAME = 'Action'

you are putting @TableName within quotes which will produce error because @TableName is a variable that is containing the name of your table, so remove those quotes.

SELECT Count(*) FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @TableName AND COLUMN_NAME = 'Action'



0
 
LVL 1

Author Comment

by:countrymeister
ID: 22762207
For whatever reason it does not work, I would like to know if I create a global table in another  proc and then pass this to proc p_GetData , will it detect if the @TableName  has the column I am looking for
0
 
LVL 1

Author Closing Comment

by:countrymeister
ID: 31506903
This does not seem to work for global tables so I need to close this qt
0

Featured Post

Application Discovery Service in AWS

In the era of the cloud, customers migrating away from their existing on-premise infrastructure. This requires lots of planning, strategies, and effort to identify their existing resources and determine how best to migrate.  Datacenter migrations happen in four phases -

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

762 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