MS SQL Tips & Tricks

How to test index existance

If You want to create index unless it already exists You can use script as below:

if not exists (	SELECT i.name, o.name 
		FROM sysindexes as i 
		INNER JOIN sysobjects as o ON 
			o.Id=i.Id AND 
			i.Name='Dict_Name'
		WHERE o.XType='U' AND o.Name='Dict')
begin
	print 'Creating index Dict_Name';
	CREATE	INDEX Dict_Name ON Dict(Name asc)
end;

Learn more tricks

Add Comment

Comments