Solved

How do I select a value from a column only if the column exists?

Posted on 2008-10-14
3
215 Views
Last Modified: 2010-04-21
I need to develop a DDL that retrieves a value from a column of a table.  However, when the DDL is executed on some databases, the column may or may not exist.  My below code is not working:

DECLARE @TSN VARCHAR(50)  --to store theater server url from THEATER_LK
SET @TSN = NULL
IF EXISTS (SELECT TABLE_NAME, COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'THEATER_LK' AND COLUMN_NAME = N'DTHEATER_SERVER_NAME')
        SELECT @TSN=DTHEATER_SERVER_NAME FROM THEATER_LK

This returns the following error when the column does not exist.  What's wrong with my syntax?
"Invalid column name 'DTHEATER_SERVER_NAME'

Thanks.
0
Comment
Question by:spazjr01
3 Comments
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 500 total points
ID: 22716960
The SQL is parsed at run time and it sees what you are trying to do and errors out.  Try this.

Also, no need to set @TSN=null because an uninitialized variable is NULL.

Try this approach:


DECLARE @TSN VARCHAR(50)  --to store theater server url from THEATER_LK
          ,@SQL nvarchar(max)
          ,@parmList nvarchar(max)
IF EXISTS (SELECT TABLE_NAME, COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME = N'THEATER_LK' AND COLUMN_NAME = N'DTHEATER_SERVER_NAME')
begin
     set @SQL = 'SELECT @TSN=DTHEATER_SERVER_NAME FROM THEATER_LK'
     set @parmList = '@TSN varchar(50) output'
 
exec sp_ExecuteSQL @SQL, @parmList, @TSN output
end

Open in new window

0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22716961
this is because the whole script is parsed on start, and hence it fails with the column which does not exist.

workaround is dynamic sql:

DECLARE @TSN VARCHAR(50)  --to store theater server url from THEATER_LK 
SET @TSN = NULL 
IF EXISTS (SELECT TABLE_NAME, COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME = N'THEATER_LK' AND COLUMN_NAME = N'DTHEATER_SERVER_NAME')
BEGIN
   EXEC SP_EXECUTESQL N'SELECT @TSN=DTHEATER_SERVER_NAME FROM THEATER_LK', N'@TSN VARCHAR(50) OUTPUT', @TSN OUTPUT
 END

Open in new window

0
 

Author Closing Comment

by:spazjr01
ID: 31506135
Thanks much.
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

Suggested Solutions

Title # Comments Views Activity
SQl server restarts itself 6 37
SQL Query with Sum and Detail rows 2 49
View SQL 2005 Job package 16 41
why sql server only update some statistics in the database ? 3 19
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

786 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