-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtility.prInsertNewServerAndInstance.StoredProcedure.sql
More file actions
90 lines (70 loc) · 4.18 KB
/
Copy pathUtility.prInsertNewServerAndInstance.StoredProcedure.sql
File metadata and controls
90 lines (70 loc) · 4.18 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
USE [DBAdmin]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*
Object: [Utility].[prInsertNewServerAndInstance]
Date: 2016-07-26
Author: Charlton Julius
Notes: Requires DBAdmin_QA Database with Utility Schema, [dbo].[ServerList] and [dbo].[InstanceList]
*/
CREATE PROCEDURE [Utility].[prInsertNewServerAndInstance]
@ServerName VARCHAR(MAX)
,@InstanceName VARCHAR(MAX) = 'MSSQLSERVER'
AS
DECLARE @ErrorMsg VARCHAR(MAX)
SET NOCOUNT ON;
BEGIN
BEGIN TRANSACTION [InsertNewServerAndInstance];
BEGIN TRY
BEGIN
IF EXISTS ( SELECT TOP ( 1 )
[sl].[Id]
FROM [dbo].[ServerList] AS [sl]
INNER JOIN [dbo].[InstanceList] AS [il] ( NOLOCK ) ON [il].[ServerListId] = [sl].[Id]
WHERE [sl].[ServerName] = @ServerName
AND [il].[InstanceName] = @InstanceName )
BEGIN
SET @ErrorMsg = 'Server ' + @ServerName + ' and Instance ' + @InstanceName + ' Already Exists.';
THROW 51000, @ErrorMsg ,1;
END
IF NOT EXISTS ( SELECT TOP ( 1 )
[Id]
FROM [dbo].[ServerList] AS [sl]
WHERE [sl].[ServerName] = @ServerName )
BEGIN
INSERT INTO [dbo].[ServerList]
( [ServerName] )
VALUES ( @ServerName -- ServerName - varchar(max)
);
END;
INSERT INTO [dbo].[InstanceList]
(
[InstanceName]
,[ServerListId]
)
SELECT @InstanceName
,[sl].[Id]
FROM [dbo].[ServerList] [sl]
WHERE [sl].[ServerName] = @ServerName;
END;
COMMIT TRANSACTION [InsertNewServerAndInstance];
END TRY
BEGIN CATCH
DECLARE @ErrorMessage NVARCHAR(4000);
DECLARE @ErrorSeverity INT;
DECLARE @ErrorState INT;
SELECT @ErrorMessage = ERROR_MESSAGE()
,@ErrorSeverity = ERROR_SEVERITY()
,@ErrorState = ERROR_STATE();
RAISERROR (@ErrorMessage,
@ErrorSeverity,
@ErrorState
);
ROLLBACK TRANSACTION [InsertNewServerAndInstance];
PRINT 'Transaction Rolled Back';
END CATCH;
END;
GO