Solved

How to check if a column exists in a tablle

Posted on 2008-10-16
8
1,244 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 250 total points
ID: 22735097
I think it will.
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

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.
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

707 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