-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathUpdate_Subscription_Owner.sql
66 lines (58 loc) · 2.04 KB
/
Update_Subscription_Owner.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*------------------------------------------------------------------------------+
| Purpose: How to Update the owner of deployed reports and subscriptions
| Note: SQLCmdMode Script
| Reference: http://www.andrewjbillings.com/ssrs-migration-subscriptions-dont-work-if-owner-no-longer-exists/
+--------------------------------------------------------------------------------
:setvar _server "Server1"
:setvar _user "***username***"
:setvar _password "***password***"
:setvar _database "ReportServer"
:connect $(_server) -U $(_user) -P $(_password)
USE [$(_database)];
GO
*/
:SETVAR OldUser "DOMAIN\OldUserName"
:SETVAR NewUser "DOMAIN\NewUserName"
SET XACT_ABORT ON;
BEGIN TRANSACTION;
PRINT '=====================================================================';
PRINT 'Update subscriptions...';
PRINT '====================================================================='
WITH
[new_owner]
AS
(
SELECT [UserID], [UserName] FROM [dbo].[Users] WHERE [UserName] = N'$(NewUser)'
)
,
[subscription_source]
AS
(
SELECT DISTINCT
[s].[Report_OID]
, [OldOwner] = [ou].[UserName]
, [OldOwnerID] = [ou].[UserID]
, [NewOwner] = [nu].[UserName]
, [NewOwnerID] = [nu].[UserID]
FROM
[dbo].[Subscriptions] AS [s]
INNER JOIN [dbo].[Users] AS [ou] ON [ou].[UserID] = [s].[OwnerID]
, [new_owner] AS [nu]
WHERE
1=1
AND [ou].[UserName] = N'$(OldUser)'
)
--SELECT * FROM subscription_source
MERGE [dbo].[Subscriptions] AS [T]
USING [subscription_source] AS [S] ON [T].[Report_OID] = [S].[Report_OID]
WHEN MATCHED
THEN UPDATE SET
[T].[OwnerID] = [S].[NewOwnerID]
OUTPUT @@ServerName AS [ServerName], DB_NAME() AS [DatabaseName], $action, [inserted].*, [deleted].*;
PRINT '******* ROLLBACK TRANSACTION ******* ';
ROLLBACK TRANSACTION;
--PRINT '******* COMMIT TRANSACTION ******* ';
--COMMIT TRANSACTION;
PRINT '=====================================================================';
PRINT 'Finished...';
PRINT '=====================================================================';