I need to create a stored procedure that allows our password custodian to recreate the symmetric key in our DR database, but somehow it always complains about incorrect syntax. Is this allowed in SQL Server 2008 R2 or it is just incorrect syntax ? Thanks
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_BCPRecreateEncryption]
@Password varchar(255)
AS
BEGIN
DROP SYMMETRIC KEY SymmetricKeyName
DROP CERTIFICATE EncryptCert
DROP MASTER KEY
CREATE MASTER KEY ENCRYPTION BY PASSWORD = @Password
CREATE CERTIFICATE EncryptCert
WITH SUBJECT = N'EncryptCert', START_DATE = N'08/06/2014 07:16:08', EXPIRY_DATE = N'08/06/2042 07:16:08'
ACTIVE FOR BEGIN_DIALOG = ON;
CREATE SYMMETRIC KEY SymmetricKeyName
WITH KEY_SOURCE = @Password,
IDENTITY_VALUE = @Password,
ALGORITHM = AES_256
ENCRYPTION BY CERTIFICATE EncryptCert;
END
I also tried to use executesql but still get the same error
DECLARE @ParmDefinition nvarchar(500);
SET @ParmDefinition = N'@Password string' ;
EXECUTE sp_executesql N'CREATE MASTER KEY ENCRYPTION BY PASSWORD = @DCMPassword', @ParmDefinition , @DCMPassword=@Password
Edited to add error :
Msg 102, Level 15, State 1, Procedure sp_BCPRecreateEncryption, Line 13
Incorrect syntax near '@Password'.Msg 102, Level 15, State 1, Procedure sp_BCPRecreateEncryption, Line 20
Incorrect syntax near '@Password'.
stringisn't a valid SQL datatype ;-)EXECbut you'd need to watch out for any'characters in the password