SQl server 2005, reporting services, change extentions field in subscriptions table

In ReportServer database where are my parameters held in the subscriptions table.  (ie. the parameters i use to call my report).

And how could I update the ReplyTo part of the Extensions field based on the value in one of my parameters.

The reason I want to do this is because I need to change the reply to email address for all reports that are being run for a certain location, and i don't feel like editing every record, since I have a lot.
Who is Participating?
Megan BrooksSQL Server ConsultantCommented:
Here is T-SQL that modifies ReplyTo in the Subscriptions table:

declare      @id uniqueidentifier,
      @tp binary(16),
      @es xml,
      @esl int,
      @params xml,
      @oldReplyTo nvarchar(4000),
      @paramVal nvarchar(4000),
      @newEs nvarchar(4000)

declare ChangeReplyTo cursor for
SELECT TEXTPTR(ExtensionSettings), ExtensionSettings, datalength(ExtensionSettings), Parameters
    FROM Subscriptions WHERE (DataSettings IS NULL)

open ChangeReplyTo
fetch next from ChangeReplyTo into @tp, @es, @esl, @params

while @@fetch_status = 0
    set @oldReplyTo = @es.value(
        '(/ParameterValues/ParameterValue[Name="ReplyTo"]/Value)[1]', 'nvarchar(4000)')

    set @paramVal = @params.value(
        '(/ParameterValues/ParameterValue[Name="ShowProgress"]/Value)[1]', 'nvarchar(4000)')

    if @esl < 7800 AND NOT @paramVal IS NULL
        print @oldReplyTo
        print @paramVal
        set @es.modify('replace value of (/ParameterValues/ParameterValue[Name="ReplyTo"]/Value/text())[1] with "somebody@somewhere.com" ')
        set @newEs = N'WRITETEXT dbo.Subscriptions.ExtensionSettings @tp N''' + CONVERT(nvarchar(4000), @es) + N''''
        exec sp_executesql @newEs, N'@tp binary(16)', @tp

    fetch next from ChangeReplyTo into @tp, @es, @esl, @params

close ChangeReplyTo
deallocate ChangeReplyTo

Whether it is a good idea to do this or not depends on how 'private' the report server is. If it is used only for a specific application, this solution should be OK. If it is used by many different people for many different things, it may not be.

The two main things to be concerned about are selecting the right subscription records and keeping the length of ExtensionSettings under 4000 characters for those subscriptions that must be modified. This example ignores subscriptions if ExtensionSettings is not less than 3900 characters (7800 bytes), to leave a little room for the ReplyTo field to grow and to leave room for the rest of the WRITETEXT. It also skips records that don't have a particular parameter name (ShowProgress). You can modify the SQL to select the parameters you need, and to further restrict which subscriptions are modified.

The length of ExtensionSettings can become a problem if the subscription contains long strings (TO string, Comment string, etc.). The approach shown here is limited to 4000 characters for the argument to sp_executesql. Slightly more could be squeezed out by using EXECUTE instead, but it would be more work to format the TEXTPTR value. The length check could be refined. It might also be possible to do a partial write back to ExtensionSettings, overwriting just the ReplyTo value, but this could be quite a bit of extra work and would be even more kludgey, and I didn't want to attempt it.

There might be a better way to do this -- I HOPE there is -- but this is what came to my mind, and it worked when I tried it. It would not be this messy if Report Server were not using ntext columns, which are deprecated in SQL Server 2005, to store the XML fragments.
Megan BrooksSQL Server ConsultantCommented:
You might be able to do this using a data driven subscription, especially if you can generate the correct list of email addresses for each copy of the report using an SQL query. The output of the query can both supply the reply-to list and supply the same value to a report parameter, if necessary, so that both values match.

I wouldn't recommend querying the ReportServer DB directly if there is a more straightforward way to accomplish the same thing.

I am answering this in a bit of a rush. I will check back later to see if I understood, and if you still need the other information.
Fraser_AdminAuthor Commented:
I don't have the ability to do data driven subscriptions since I don't have enterprise edition.

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Megan BrooksSQL Server ConsultantCommented:
That does make it harder to do. The parameters are stored in the Parameters column, in an XML fragment similar to that used for ExtensionSettings (used in the solution for your other question 22492290). A given parameter can be extracted using an XPATH query, as in
SELECT     CONVERT(xml, ExtensionSettings ).value(
        '(/ParameterValues/ParameterValue[Name="ReplyTo"]/Value)[1]', 'varchar(255)'
    ) AS ReplyTo,
    CONVERT(xml, Parameters ).value(
        '(/ParameterValues/ParameterValue[Name="ShowProgress"]/Value)[1]', 'varchar(255)')
    AS ShowProgress
FROM         Subscriptions
WHERE     (NOT (CONVERT(xml, ExtensionSettings ).value('(/ParameterValues/ParameterValue[Name="ReplyTo"]/Value)[1]', 'varchar(255)') IS NULL))
This query finds values for the "Reply To" field and for the parameter "ShowProgress" in subscriptions for which "Reply To" is specified.

Changing ReplyTo is difficult because ExtensionSettings is defined as the depricated type 'ntext' instead of 'xml', and I don't know of a way to modify such a column directly. The only way I know of to update an ntext field is with WRITETEXT, which only accepts data from a literal or a parameter. Short of connecting from an application, I don't know how it can be changed, so that would be a question for someone more familiar with that datatype.

The XML itself is easy enough to change, though. For example,
declare @ReplyTo xml
SELECT     @ReplyTo = ExtensionSettings
FROM         Subscriptions
WHERE     SubscriptionID = '967AEA59-3F8E-4A32-8A46-E3D5D3D34445'

SET @ReplyTo.modify('replace value of (/ParameterValues/ParameterValue[Name="ReplyTo"]/Value/text())[1] with "somebody@somewhere.com" ')

You can change the WHERE clause in the first query to anything that selects a single row. The problem now is how to get the result back into Subscriptions.ExtensionSettings.

I suppose one other alternative would be to run a script against the web service and make the changes that way. That's not something I can put together easily right now.
Fraser_AdminAuthor Commented:
yea, i would like to be able to edit those xml values if possible.  do you have any idea how to do this?
Megan BrooksSQL Server ConsultantCommented:
As long as the ExtensionSettings column doesn't exceed 4000 characters, it should be possible to create WRITETEXT queries as a literals and execute them within a cursor loop to do what you want. The new XML data would be embedded as a literal within the literal. This is starting to look very kludgey, but I think it would work and it would be easier than writing an application or a script. I will see what I can come up with...
Fraser_AdminAuthor Commented:
thanks that would be great.  
Fraser_AdminAuthor Commented:
ok i will give it a try and let you know.  thanks
Fraser_AdminAuthor Commented:
OK, first thing I noticed that I forgot to mention to you is that i have several reports that have that same parameter.  I would only like to change just one report.  Where do I find the report name in the subscriptions table?
Megan BrooksSQL Server ConsultantCommented:
To obtain information about the report itself, as opposed to the subscription, join Subscriptions to _ using the Subscriptions.Report_OID and Catalog.ItemID. If you use a query designer, the join should be added automatically. The report name is Catalog.Name.

For example,
SELECT     Catalog.Path, Catalog.Name, Subscriptions.Description
FROM         Subscriptions INNER JOIN
                      Catalog ON Subscriptions.Report_OID = Catalog.ItemID

That's good if you only need to change one report; you are unlikely to encounter problems with things changing unexpectedly, and you will probably be able to ensure that the ExtensionSettings column doesn't grow too large.

I won't repeat the entire example I gave earlier but the SELECT query in the DECLARE CURSOR now becomes something like
SELECT TEXTPTR(S.ExtensionSettings), S.ExtensionSettings,
      datalength(S.ExtensionSettings), S.Parameters
FROM Subscriptions S INNER JOIN Catalog C ON
      S.Report_OID = C.ItemID
WHERE (S.DataSettings IS NULL) AND (C.Name LIKE 'NameOrPattern')

You can substitute either a single report name or a pattern for 'NameOrPattern'.

Megan BrooksSQL Server ConsultantCommented:
Oops, "_" should have been "Catalog". Lots of finger trouble today. "To obtain information about the report itself, as opposed to the subscription, join Subscriptions to Catalog using the Subscriptions.Report_OID and Catalog.ItemID. If you use a query designer, the join should be added automatically. The report name is Catalog.Name."
Fraser_AdminAuthor Commented:
works perfect.  thanks
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.