Solved

Drop and add index usig a variable

Posted on 2014-03-27
2
493 Views
Last Modified: 2014-03-27
Hello,

Trying to drop index on a table. But doesn't seem to work

DECLARE @indexName VARCHAR(50);
SELECT @indexName =  i.name
FROM sysobjects o, sysindexes i
WHERE  (o.id = i.id and o.name = 'MyTable') AND i.name like ('%NonClustered%')

DROP INDEX [@indexName] ON [dbo].[MyTable]

ALTER TABLE [dbo].[Link]
ALTER COLUMN [myColumn] [nvarchar](120)

CREATE INDEX [@indexName] ON [dbo].[MyTable] (myColumn)

"Cannot drop the index 'dbo.MyTable.@indexName', because it does not exist or you do not have permission."

I am able to get the correct index name but unable to execute drop index command with a variable.
0
Comment
Question by:sansoftura
[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 Comments
 
LVL 34

Accepted Solution

by:
ste5an earned 250 total points
ID: 39960276
You cannot use a variable in the DROP statement directly. Use dynamic SQL instead. E.g.

DECLARE @indexName VARCHAR(50);
SELECT @indexName =  i.name
FROM sysobjects o, sysindexes i
WHERE  (o.id = i.id and o.name = 'MyTable') AND i.name like ('%NonClustered%');

DECLARE @Sql NVARCHAR(MAX) = N'DROP INDEX ' + QUOTENAME(@indexName) +' ON [dbo].[MyTable]';
EXECUTE (@Sql);

ALTER TABLE [dbo].[Link]
ALTER COLUMN [myColumn] [nvarchar](120);

SET @Sql = N'CREATE INDEX ' + QUOTENAME(@indexName) +'  ON [dbo].[MyTable] (myColumn);';
EXECUTE (@Sql);

Open in new window

0
 
LVL 6

Author Closing Comment

by:sansoftura
ID: 39960287
Perfect!
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

635 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