-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcedure - Insert.sql
More file actions
145 lines (124 loc) · 3.51 KB
/
Procedure - Insert.sql
File metadata and controls
145 lines (124 loc) · 3.51 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
-- =============================================
-- Author: Tim Isabella
-- Create date:
-- Description: Insert snippet into 'Snippets' table and batch insert snippet members if alliance
-- Code Reviewer:
-- =============================================
ALTER PROC [dbo].[Snippet_Insert]
@Name NVARCHAR(200)
,@Code NVARCHAR(50)
,@Logo NVARCHAR(255)
,@SiteUrl NVARCHAR(255)
,@ColorHEX nchar(7)
,@StatusId INT
,@RegionTypeId INT
,@LocationId INT
,@IsAlliance BIT
,@snippetCompositeUDT AS [dbo].[SnippetCompositeUDT] READONLY
,@Id INT OUTPUT
AS
/*-----------Test Code-----------
DECLARE @Name NVARCHAR(200) = 'Test Snippet'
,@Code NVARCHAR(50) = 'BP'
,@Logo NVARCHAR(255) = 'https://SnippetLogo.url'
,@SiteUrl NVARCHAR(255) = 'https://SnippetSiteUrl.com'
,@ColorHEX nchar(7) = '#f28500'
,@StatusId INT = 2
,@RegionTypeId INT = 1
,@LocationId INT = 3
,@IsAlliance BIT = 1
,@snippetCompositeUDT AS [dbo].[SnippetCompositeUDT]
,@Id INT
INSERT INTO @snippetCompositeUDT (SnippetId) VALUES (1)
INSERT INTO @snippetCompositeUDT (SnippetId) VALUES (2)
INSERT INTO @snippetCompositeUDT (SnippetId) VALUES (3)
INSERT INTO @snippetCompositeUDT (SnippetId) VALUES (4)
EXECUTE [dbo].[Snippets_Insert]
@Name
,@Code
,@Logo
,@SiteUrl
,@ColorHEX
,@StatusId
,@RegionTypeId
,@LocationId
,@IsAlliance
,@snippetCompositeUDT
,@Id
SELECT *
FROM [dbo].[Snippets]
---------------------------------*/
BEGIN
--Snippet name check
IF EXISTS (SELECT [Name] FROM [dbo].[Snippets] WHERE [Name] = @Name)
BEGIN
PRINT '"' + CONVERT(varchar(200), @Name) + '" already exists -- Canceling insert.'
RETURN
END
BEGIN TRY
BEGIN TRANSACTION;
PRINT 'Inserting snippet...'
DECLARE @RegistrationDate DATETIME2(7) = GETUTCDATE()
INSERT INTO
[dbo].[Snippets] (
[Name]
,[Code]
,[Logo]
,[SiteUrl]
,[ColorHEX]
,[StatusId]
,[RegionTypeId]
,[LocationId]
,[IsAlliance]
,RegistrationDate
)
VALUES(
@Name
,@Code
,@Logo
,@SiteUrl
,@ColorHEX
,@StatusId
,@RegionTypeId
,@LocationId
,@IsAlliance
,@RegistrationDate
)
SET @Id = SCOPE_IDENTITY()
--Alliance check for snippets batch insert
IF(@IsAlliance = 1)
BEGIN
PRINT 'Beginning composite table update...'
--Remove all snippets from coaliton snippet with matching alliance snippet Id
DELETE
FROM [dbo].[SnippetAlliances]
WHERE [AllianceId] = @Id
DECLARE @fSnippetId INT = (SELECT TOP 1 [SnippetId] FROM @snippetCompositeUDT)
--Insert all snippets into alliance snippet from UDT input if record #1 is greater than zero
IF(@fSnippetId > 0)
BEGIN
--Combine current Id and batch UDT snippets columns together, then input batch results into composite table
PRINT 'Batch inserting into composite table...'
INSERT INTO [dbo].[SnippetAlliances] (
[AllianceId]
,[SnippetId]
)
SELECT AllianceId = @Id
,pCUDT.[SnippetId]
FROM @snippetCompositeUDT AS pCUDT
END
ELSE PRINT 'Empty batch and no insert.'
END
COMMIT TRANSACTION;
DECLARE @Success INT = 1
END TRY
BEGIN CATCH
PRINT 'Error! Rolling back transaction -- ' + ERROR_MESSAGE()
ROLLBACK TRANSACTION;
SET @Success = 0
END CATCH
IF(@Success = 1)
BEGIN
PRINT '"' + CONVERT(varchar(200), @Name) + '" has been successfully inserted!'
END
END