Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Create index via SQL stored procedure

Posted on 2010-11-16
3
Medium Priority
?
552 Views
Last Modified: 2012-06-21
I am attempting to create a stored procedure which will generate indexes on SQL tables, if they do not already exist
Code is below.

However, I keep getting the error:
Msg 1913, Level 16, State 1, Line 1
The operation failed because an index or statistics with name 'IX_d_StudentsCurrentShortCS_studentNumber' already exists on table 'REAdw.dbo.d_StudentsCurrentShortCS'.

I know that I cannot create an index if the index name alreay exists in sys.indexex, but I am using code to check for it and delete if already there, before creating/re-creating.

Any guidance would be most appreciated

- - - - -
USE [REAdw]
GO
/****** Object:  StoredProcedure [dbo].[spd_cube_transcript]    Script Date: 11/16/2010 10:23:43 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
alter proc spd_MakeIndexes
as
IF EXISTS (SELECT name FROM sys.indexes
            WHERE name = N'IX_d_StudentsCurrentShortCS_studentNumber')
    DROP INDEX IX_d_StudentsCurrentShortCS_studentNumber ON REAdw.dbo.d_StudentsCurrentShortCS;
GO
CREATE INDEX IX_d_StudentsCurrentShortCS_studentNumber
    ON REAdw.dbo.d_StudentsCurrentShortCS (studentNumber);
GO
0
Comment
Question by:JEClark1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
EvilPostIt earned 2000 total points
ID: 34147032
You are creating the index when you create the stored procedure. GO denotes the end of the stored procedure and the subsequent command is then processed on its own.

Instead do the following.

alter proc spd_MakeIndexes
as
IF EXISTS (SELECT name FROM sys.indexes
            WHERE name = N'IX_d_StudentsCurrentShortCS_studentNumber')
BEGIN TRAN
DROP INDEX IX_d_StudentsCurrentShortCS_studentNumber ON REAdw.dbo.d_StudentsCurrentShortCS;
COMMIT TRAN
BEGIN TRAN
CREATE INDEX IX_d_StudentsCurrentShortCS_studentNumber ON REAdw.dbo.d_StudentsCurrentShortCS (studentNumber); 
COMMIT TRAN
GO 

Open in new window

0
 

Author Closing Comment

by:JEClark1
ID: 34147122
Thank you for a quick and concise response!  Worked perfectly.
0
 
LVL 16

Expert Comment

by:EvilPostIt
ID: 34148065
Glad to be of assistance.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

670 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