Drop Index with SQL Variable

1

I am an inexperienced grasshopper and am looking to make an automatic script for myself. This script I created in batch and I can go without problems until the installation of SQL , restoration of the database and execution of some Querys.

My problem is that my second step requires that I delete Indexes and Rowguide. Well, my worst problem is that the index is mutable, so it will never have the same name (as it seems to me)

So I created something more or less like this

Declare @indexeee varchar(50) =(select i.name from sys.indexes i
inner join sys.tables t
on i.object_id = t.object_id
where t.name = 'Colecao'
and i.name like '%MSm%')

select @indexeee

Return

  

MSmerge_index_1662628966

Perfect for what I need, because it is this index that I need to delete after the rowguide linked to it. but I can not function as a linux script (where I have a bit more experience) or batch windows (I grew up with them)

"COMANDOCITADOACIMA"

DROP INDEX @indexeee
    ON pdv.produto.colecao;  
GO  
  

Message 102, Level 15, State 1, Line 8 Incorrect syntax next to   '@indexeee'.

By placing the mouse on Indexeee we have:

  

Incorrect syntax next to '@indexeee'. Waiting for '.', 'ID', or   QUOTE_ID.

If possible I can explain the answer, because I still do not know much about SQL , basically retaining the Select and inner join.

    
asked by anonymous 28.06.2016 / 21:58

1 answer

0

Please follow the example below.

EXEC SP_EXECUTESQL 'DROP INDEX ' + @indexeee + ' ON pdv.produto.colecao';
    
09.01.2017 / 13:20