3

I am trying to generated the script of a database on SQL Server 2008 R2

I got this error

Microsoft.SqlServer.Management.Smo.PropertyCannotBeRetrievedException: Property TextHeader is not available for StoredProcedure '[dbo].[p_SYS_GetQDNPassword]'. This property may not exist for this object, or may not be retrievable due to insufficient access rights. The text is encrypted. at Microsoft.SqlServer.Management.SqlScriptPublish.GeneratePublishPage.worker_DoWork(Object sender, DoWorkEventArgs e) at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e) at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)

Could you help please?

I don't know what other information you need, but whatever you need, tell me please to give you.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user3432257
  • 415
  • 3
  • 10
  • 22
  • 1
    It would appear that this stored procedure is **encrypted** and thus cannot be scripted out to a text file – marc_s Mar 22 '14 at 10:02
  • It looks like you don't have permissions on the table. I'm not sure exactly what permissions are required. Can you run a SELECT against the table? If you don't have any permissions on the table I'm not sure you'd able to script it. – Vignesh Kumar A Mar 22 '14 at 10:04
  • @user3432257..Could you please tel me ,whether have you used Encryption for any Database objects – Revan Mar 22 '14 at 10:05
  • This might help you [Decrypting encrypted stored procedures, views, functions in SQL server 2005,2008 &R2](http://sqljunkieshare.com/2012/03/07/decrypting-encrypted-stored-procedures-views-functions-in-sql-server-20052008-r2/) – jpw Mar 22 '14 at 10:05
  • @marc_s so what do you suggest? to not generate it at all? or what please? thanks – user3432257 Mar 22 '14 at 10:05
  • @VigneshKumar I am using windows authentication user. So, I am sure that I have all the permissions. – user3432257 Mar 22 '14 at 10:06
  • @tHiNk_OuT_oF_bOx Actually, this database is not mine. I am working and another employee has made it in the past. and now I have to work on it. Unfortunately, this employee left the work – user3432257 Mar 22 '14 at 10:06
  • @marc_s please what is the mean of encrypted stored procedure ? i know that we encrypt the password. not the stored prcoedure. right? – user3432257 Mar 22 '14 at 10:07
  • @user3432257.. Try sp_helptext stored_procedure_name and check – Revan Mar 22 '14 at 10:10
  • @jpw thanks for the link, but that link doesn't state how to generate the script of encrypted stored procedure – user3432257 Mar 22 '14 at 10:10
  • 1
    @tHiNk_OuT_oF_bOx The stored procedure is `dbo.p_SYS_GetQDNPassword` so do you mean I have to try this: `sp_helptext dbo.p_SYS_GetQDNPassword` and execute it? – user3432257 Mar 22 '14 at 10:12
  • @user3432257..Yes exactly..then you will get some information.. – Revan Mar 22 '14 at 10:13
  • @tHiNk_OuT_oF_bOx I will try and let you know – user3432257 Mar 22 '14 at 10:14
  • @tHiNk_OuT_oF_bOx I got this result `The text for object 'p_SYS_GetQDNPassword' is encrypted.` – user3432257 Mar 22 '14 at 10:18
  • If the stored procedure is encrypted, you cannot script it out. Hopefully the author of the stored procedure was prudent enough to store the **source code** of it somewhere (ideally in a source control system) – marc_s Mar 22 '14 at 10:19
  • @marc_s so how can I generate the script of the whole database without this stored procedure please? – user3432257 Mar 22 '14 at 10:26
  • In Object Explorer > (your database) > Tasks > Generate Scripts you need to select the individual objects to script out (instead of everything) and de-select all those stored procedures that are encrypted – marc_s Mar 22 '14 at 13:24

2 Answers2

2

Using the sp_helptext dbo.p_SYS_GetQDNPassword should give you the result:

"The text for object 'YourProcedureName' is encrypted."

This means that stored procedure is created using WITH ENCRYPTION, and SQL Server internally stores the text with the definition of the object in an obfuscated format

The actual definition of an object is stored in system table sys.sysobjvalues which is not directly accessible. By connecting to SQL Server using the Dedicated Administrator Connection (DAC) you can select the imageval column in which the information is stored

Click on Database Engine Query button:

enter image description here

The Connect To Database Engine dialog will pop out. Type the "admin:" prefix before the server name and you'll be connected to DAC

enter image description here

You can find useful info here in this article http://www.mssqltips.com/sqlservertip/2964/encrypting-and-decrypting-sql-server-stored-procedures-views-and-userdefined-functions/

When you're connected to DAC run the following script from the article:

   SET NOCOUNT ON
GO

ALTER PROCEDURE dbo.TestDecryption WITH ENCRYPTION AS
BEGIN
 PRINT 'This text is going to be decrypted'
END 
GO

DECLARE @encrypted NVARCHAR(MAX)
SET @encrypted = ( 
 SELECT imageval 
 FROM sys.sysobjvalues
 WHERE OBJECT_NAME(objid) = 'TestDecryption' )
DECLARE @encryptedLength INT
SET @encryptedLength = DATALENGTH(@encrypted) / 2

DECLARE @procedureHeader NVARCHAR(MAX)
SET @procedureHeader = N'ALTER PROCEDURE dbo.TestDecryption WITH ENCRYPTION AS '
SET @procedureHeader = @procedureHeader + REPLICATE(N'-',(@encryptedLength 
-   LEN(@procedureHeader)))
 EXEC sp_executesql @procedureHeader
 DECLARE @blankEncrypted NVARCHAR(MAX)
 SET @blankEncrypted = ( 
 SELECT imageval 
 FROM sys.sysobjvalues
 WHERE OBJECT_NAME(objid) = 'TestDecryption' )

SET @procedureHeader = N'CREATE PROCEDURE dbo.TestDecryption WITH ENCRYPTION AS '
SET @procedureHeader = @procedureHeader + REPLICATE(N'-',(@encryptedLength 
-   LEN(@procedureHeader)))

DECLARE @cnt SMALLINT
DECLARE @decryptedChar NCHAR(1)
DECLARE @decryptedMessage NVARCHAR(MAX)
SET @decryptedMessage = ''
SET @cnt = 1
WHILE @cnt <> @encryptedLength
BEGIN
  SET @decryptedChar = 
      NCHAR(
        UNICODE(SUBSTRING(
           @encrypted, @cnt, 1)) ^
        UNICODE(SUBSTRING(
           @procedureHeader, @cnt, 1)) ^
        UNICODE(SUBSTRING(
           @blankEncrypted, @cnt, 1))
     )
  SET @decryptedMessage = @decryptedMessage + @decryptedChar
 SET @cnt = @cnt + 1
END
SELECT @decryptedMessage

Of course change the dbo.TestDecryption with your procedure name in the script :)

This script works for me, hope it helps

Milica Medic Kiralj
  • 3,580
  • 31
  • 31
  • This problem will take a long time to be solved, meanwhile, could you check my question here please? http://stackoverflow.com/questions/22576818/what-is-the-connection-string-for-this-database Thanks in advance – user3432257 Mar 22 '14 at 11:40
1

Use

sp_helptext dbo.p_SYS_GetQDNPassword 

and Try to alter the stored procedure and try to get script

Revan
  • 1,104
  • 12
  • 27
  • I used that query and I got a results tells me that the object is encrypted. So, how can I alter it and make it decrypted. I doubt that there is a way. right? – user3432257 Mar 22 '14 at 10:29
  • @user3432257.. Have a look..It may help you.. http://forums.asp.net/t/1516587.aspx?How+can+i+decrypt+an+encrypted+stored+procedure+ – Revan Mar 22 '14 at 10:34
  • http://social.msdn.microsoft.com/Forums/sqlserver/en-US/e7056ca8-94cd-4d36-a676-04c64bf96330/decrypt-the-encrypted-store-procedure-through-the-tsql-programming-in-sql-server-2005 – Revan Mar 22 '14 at 10:37