Solved

Create index via SQL stored procedure

Posted on 2010-11-16
3
545 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
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
EvilPostIt earned 500 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now