Drop Index with SQL variable

Asked

Viewed 89 times

1

I’m an inexperienced grasshopper and I’m looking to make an automatic script for myself. This script I created in batch and I can go smoothly to the installation of SQL, database restore and execution of some Querys.

My problem is that my second step requires me to delete Indexes and Rowguide. Well, my worst problem is that the Dice is mutable, so it will never have the same name (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 later the rowguide linked to it. but I can’t function as linux script (where I have a little more experience) or batch windows (which I grew up with them)

"COMMANDER"

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

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

By placing the mouse on top of Indexeee we have:

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

If possible I can explain the answer, because I don’t know much about SQL, basically keeping me in Select and Inner Join.

1 answer

0

Run as an example below.

EXEC SP_EXECUTESQL 'DROP INDEX ' + @indexeee + ' ON pdv.produto.colecao';

Browser other questions tagged

You are not signed in. Login or sign up in order to post.