SQL Server – Error ‘Incorrect Syntax Near @certificate’

sql serversql-server-2008sql-server-2008-r2t-sql

I have created a certificate and now I want to remove certificate signature and drop the certificate. When I supply the certificate name directly it is working but when I'm making use of local variables to supply the name its giving me syntax error.

I have tried with different datatypes as well

text, varbinary(max), char(200), varchar(200), nvarchar(200)

None of these datatypes are working.

declare @certificate nvarchar(200) 
set @certificate = 'ITManagerCertificate'

begin transaction

use DBwork
-- remove signature from stored procedure
drop signature from Report_Manager
by certificate @certificate

-- drop certificate
--DROP CERTIFICATE N@certificate 

if @@ERROR > 0
begin 
   rollback transaction
end 
else begin
   commit transaction
end

Best Answer

I don't know if you can pass a paramater in this situation. Please try to create your query in a variable, like:

declare @query as nvarchar(255);
set @query = 'drop signature from Report_Manager by certificate ' + @certificate;

and then execute the query like:

EXECUTE sp_executesql @query