May 4, 2005 at 8:43 am
I'm having a hard time trying to figure out what best area to look for using a google search so figured I would try here as well.
I need the syntax to simply check if a column (or index) exists on a table.
I'm wanting to automate some import processes and don't want every alter statement firing off if any portion of the import has already been completed anyhow.
Thanks for your help.
May 4, 2005 at 9:09 am
Not elegant, but works for a column...
if exists (select sc.name from syscolumns sc(nolock) inner join sysobjects so(nolock) on sc.id = so.id where so.name = 'TABLEA' and sc.name = 'COLB')
print 'YAY'
else
print 'NAY'
For an index
if exists (select * from dbo.sysindexes where name = N'INDEX' and id = object_id(N'[dbo].[TABLEA]'))
print 'Yep, there is an index'
else
print 'No Index of that name'
May 5, 2005 at 2:26 am
without using system tables as the system tables will not be available in SQLServer 2005...
use Northwind
GO
set nocount on
declare @table_owner sysname, @table_name sysname, @column_or_index_name sysname
declare @result varchar(25),@param nvarchar(261)
--set the variables
select
@table_owner = 'dbo'
, @table_name = 'Categories'
, @column_or_index_name = 'CategoryName'
, @result = 'No index or column exists'
--first check for the existance of a column with the name
if exists(select * from INFORMATION_SCHEMA.[COLUMNS] where TABLE_SCHEMA=@table_owner and TABLE_NAME=@table_name and COLUMN_NAME=@column_or_index_name)
begin
set @result = 'Column exists'
end else begin
if object_id('tempdb..#results')is not null drop table #results
create table #results (index_name sysname,index_description varchar(210),index_keys nvarchar(2078))
set @param = '[' + @table_owner + '].[' + @table_name + ']'
insert into #results(index_name,index_description,index_keys)
exec sp_helpindex @objname = @param
if exists(select * from #results where index_name=@column_or_index_name)
set @result = 'Index exists'
end
select result = @result
regards,
Mark Baekdal
+44 (0)208 241 1762
Build, Comparison and Synchronization from Source Control = Database change management for SQL Server
May 5, 2005 at 6:33 am
For an index
if exists (select * from dbo.sysindexes where name = N'INDEX' and id = object_id(N'[dbo].[TABLEA]'))
print 'Yep, there is an index'
else
print 'No Index of that name'
Lowell
May 5, 2005 at 7:09 am
Try this...
create table #results (index_name sysname, index_description varchar(200), index_keys varchar(300))
insert into #results exec sp_helpindex TABLE
if exists(select * from #results where index_keys like '%COLUMN%')
print 'Yep, theres an index'
else
print 'Nope, no index'
May 5, 2005 at 9:06 am
The procedure below will add the ID column if it doesn't exist in the Mcrl table. You could easily substitute parameters for the table name and column name.
IF NOT EXISTS (SELECT * FROM sysobjects obj
INNER JOIN syscolumns sys ON obj.ID=sys.ID
WHERE obj.Name = 'Mcrl' and sys.Name='ID')
ALTER TABLE Mcrl
ADD ID int IDENTITY(1,1)
CONSTRAINT PK_Mcrl PRIMARY KEY
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply