Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How do I check if an index exists before creating it in a script?

Posted on 2005-04-02
2
Medium Priority
?
262 Views
Last Modified: 2012-08-14
I'm trying to add an index via a script, but want to check if it exists first.

I use this for procedures:
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[spCreateScenarioNHrooms]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)

but it doesn't seem as if I can use " IsIndex".

The larger question is how to alter a table completely and save the data. What's the normal practice for adding via a script? I don't what the dba to slit my throat if the script blows up. But I also don't want to re-create the entire table (saving data, re-importing data, whatever) just to add one measley index.

So,
a. how to check if an index exists before creating it?
b. what's the normal practice for creating an index in a script?

Thanks !

Bill
0
Comment
Question by:Alaska Cowboy
2 Comments
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 200 total points
ID: 13688034
try using the sysindex table

IF EXISTS (SELECT * FROM dbo.sysindexes WHERE id = object_id(N'[dbo].[spCreateScenarioNHrooms]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)

just look up CREATE INDEX in BOL...

CREATE NONCLUSTERED INDEX zip_ind
   ON authors (zip)
   WITH FILLFACTOR = 100
0
 
LVL 3

Assisted Solution

by:Satyabodhi
Satyabodhi earned 100 total points
ID: 13688283
if not exists (select * from sysindexes where name = 'IX_Your_Table_columnA)
      create unique index IX_Your_Table_columnA
      on Your_Table(ColumnA)

0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

564 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