Solved

Check if column exists on SQL 2005

Posted on 2007-03-22
6
12,231 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
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.
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 shrink a transaction log file down to a reasonable size.

919 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now