How to determine if an index already exists for a table?

Posted on 2006-07-08
Last Modified: 2012-06-21
Hi... I have a table which I want to programmatically create a clustered index using the following statement:


which works just fine.  Now, I want to check if the index already exists before I issue the command again (from within a C# program).  I was using the following command:

SELECT name FROM sysindexes WHERE name = 'PK_BookNbr'

but it doesn't find the existing index and tries to re-create it.

What is the proper syntax to see if the index already exists in a table?

Thanks for your help in advance.
Question by:rmmarsh
  • 4
  • 3
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17066210
the PRIMARY KEY is a constraint, so you have to look here:
SELECT name FROM sysconstraints WHERE name = 'PK_BookNbr'

there will also be an entry in sysindexes, but with a system-generated name

Author Comment

ID: 17066229
When I run the SELECT statement in the SQL Server Manager, I get an error saying:

 invalid column name 'name'

LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17066240

SELECT object_name( constid) FROM sysconstraints WHERE object_name(id) = 'PK_BookNbr'
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.


Author Comment

ID: 17066293
nope... that's not it either... nothing is returned from that command

My command -> SELECT name FROM sysindexes WHERE name = 'PK_BookNbr'
works, and shows the name.  However, when I run it in the program, it returns no rows (it should return 1 row)...

Author Comment

ID: 17066295, and shows the name (in the SQL Server Manager).
LVL 142

Accepted Solution

Guy Hengel [angelIII / a3] earned 500 total points
ID: 17066368
> However, when I run it in the program, it returns no rows
are you connected to the correct database?

SELECT name FROM <yourdatabase>.dbo.sysindexes WHERE name = 'PK_BookNbr'

Author Comment

ID: 17066400
Ahhh... thank you so much... forgot about that small item  :D

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity and sql server 4 35
Find results from sql within a time span 11 30
T-SQL: New to using transactions 9 25
SQL Function NOT ROUND 9 10
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

770 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