-
Notifications
You must be signed in to change notification settings - Fork 1
/
AddCMSEntry.sql.txt
77 lines (68 loc) · 2.21 KB
/
AddCMSEntry.sql.txt
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
67
68
69
70
71
72
73
74
75
76
77
USE [msdb]
GO
/****** Object: StoredProcedure [dbo].[AddCMSEntry] Script Date: 3/9/2021 12:34:15 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Chad Estes
-- Create date: 2018-12-10
-- Description: Add a server to CMS
-- =============================================
CREATE PROCEDURE [dbo].[AddCMSEntry]
-- Add the parameters for the stored procedure here
@branch sysname = NULL,
@group sysname = NULL,
@server sysname = NULL
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SET CONCAT_NULL_YIELDS_NULL ON;
-- Insert statements for procedure here
IF ( CONCAT(@branch,@group,@server) IS NOT NULL )
BEGIN
DECLARE @pID INT
DECLARE @gID INT
DECLARE @sID INT
--Make sure Server Group exists
SET @pID = (SELECT [server_group_id] FROM [msdb].[dbo].[sysmanagement_shared_server_groups_internal] WHERE [name] = @branch)
IF (@pID IS NULL)
BEGIN
SET @pID = (SELECT [server_group_id] FROM [dbo].[sysmanagement_shared_server_groups_internal] WHERE [server_type]=0 AND [is_system_object]=1)
EXEC [dbo].[sp_sysmanagement_add_shared_server_group]
@name=@branch,
@description=N'',
@parent_id=@pID,
@server_type=0,
@server_group_id=@pID OUTPUT
END
SET @gID = (SELECT [server_group_id] FROM [dbo].[sysmanagement_shared_server_groups_internal] WHERE [name] = @group AND parent_id = @pID)
IF (@gID IS NULL)
BEGIN
EXEC [dbo].[sp_sysmanagement_add_shared_server_group]
@name=@group,
@description=N'',
@parent_id=@pID,
@server_type=0,
@server_group_id=@gID OUTPUT
END
IF NOT EXISTS (SELECT 1 FROM [dbo].[sysmanagement_shared_registered_servers_internal] WHERE [server_name]=@server AND [server_group_id]=@gID)
BEGIN
EXEC [dbo].[sp_sysmanagement_add_shared_registered_server]
@name=@server,
@server_group_id=@gID,
@server_name=@server,
@description=N'',
@server_type=0,
@server_id=@gID OUTPUT
END
END
ELSE
BEGIN
RAISERROR (N'NULL values are not allowed, please specify all parameters.', 10, 1) WITH NOWAIT
END
END
GO