Solved

Check if column exists on SQL 2005

Posted on 2007-03-22
6
12,232 Views
Last Modified: 2009-03-12
I found this script to check if a column exist in a table and to create the col if not exisiting:

IF NOT EXISTS (SELECT TABLE_NAME,COLUMN_NAME
                         FROM master.dbo.INFORMATION_SCHEMA_COLUMNS
                         WHERE TABLE_NAME=N'myTable' AND COLUMN_NAME = N'myNewCol'
ALTER TABLE myTable
ADD myNewCol int NOT NULL

but it is not working on SQL 2005 Standard.
Why
0
Comment
Question by:chriswies
  • 3
  • 2
6 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 18769564
you were missing the closing )
also, it's master.INFORMATON_SCHEMA.Columns, and not master.dbo.information_schema_columns
also, you should NOT specify master.dbo for the information_schema views:

IF NOT EXISTS (SELECT TABLE_NAME,COLUMN_NAME
                         FROM INFORMATION_SCHEMA.COLUMNS
                         WHERE TABLE_NAME=N'myTable' AND COLUMN_NAME = N'myNewCol' )
BEGIN
ALTER TABLE myTable
ADD myNewCol int NOT NULL
END
0
 

Author Comment

by:chriswies
ID: 18769580
thanks,
my first try was now only the select.

SELECT TABLE_NAME,COLUMN_NAME
 FROM INFORMATION_SCHEMA.COLUMNS
 WHERE TABLE_NAME=N'tkey_wdReplacements'

but I got no results? I checked the table name and expected to get a list of the table fields.
Chris
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18769636
are you in the database itself (ie the context).
the information_schema views refer to the currently connected database...

does the full query:
SELECT DISTINCT TABLE_NAME
 FROM INFORMATION_SCHEMA.COLUMNS

return your table(s) of the database?
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.

 

Author Comment

by:chriswies
ID: 18769648
OK, I got it. I was in Master, not in my db.
Thanks
Chris
0
 

Expert Comment

by:scollege
ID: 21691608
Doesn't work for me either, and I am most certainly in the correct database.

0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21691900
scollege: you shall open your own question if this "solution" does not help you.
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

785 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