Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 16th, 2006, 01:29 PM
Authorized User
 
Join Date: Aug 2004
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to test if a column exists in a table

I need to some how check to see if a specific column exists in a table. If it doesn't then I would add the new column.

Here is what I have so far but sysobjects doesn't seem to support at the column level.

if not exists (select * from sysobjects where id = object_id('[dbo].[Timesheet].[Approve]') and OBJECTPROPERTY(id, 'IsUserTable') = 1)
ALTER TABLE [dbo].[Timesheet] ADD Approve VARCHAR(15) NULL
GO

Any Ideas ?
J

 
Old February 16th, 2006, 01:55 PM
SQLScott's Avatar
Wrox Author
 
Join Date: Dec 2004
Posts: 338
Thanks: 0
Thanked 2 Times in 2 Posts
Default

You need to query syscolumns. First, get the object id from sysobjects for your table name, then with that object id, query the syscolumns table, looking for that colummn name.

 
Old February 16th, 2006, 02:25 PM
Authorized User
 
Join Date: Sep 2005
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Default

very easy, use the ANSI system views
IF NOT EXISTS(
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME='Timesheet'
AND COLUMN_NAME ='Approve')



“I sense many useless updates in you... Useless updates lead to fragmentation... Fragmentation leads to downtime...Downtime leads to suffering..Fragmentation is the path to the darkside.. DBCC INDEXDEFRAG and DBCC DBREINDEX are the force...May the force be with you" -- http://sqlservercode.blogspot.com/





Similar Threads
Thread Thread Starter Forum Replies Last Post
What’s the .NET Way to test if Folder Exists? BrianWren Pro Visual Basic 2005 3 March 14th, 2007 08:35 AM
What’s the .NET Way to test if Folder Exists? BrianWren Visual Basic 2005 Basics 3 January 12th, 2007 07:32 PM
check if a column exists in the table sands SQL Server 2000 2 April 21st, 2006 11:08 PM
howto test document exists folklayer XSLT 2 October 19th, 2005 05:56 PM
Test to see if a SQL Server exists Mitch Access 2 July 18th, 2003 04:29 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.