-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcedure - SelectAll_OLD.sql
116 lines (105 loc) · 3.02 KB
/
Procedure - SelectAll_OLD.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
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
-- =============================================
-- Author: Tim Isabella
-- Create date:
-- Description: Select all Snippets -- output JSON of snippets assigned to snippet alliance through composite table
-- Code Reviewer:
-- =============================================
ALTER PROC [dbo].[Snippets_SelectAll]
@Index INT
,@PageSize INT
AS
/*-----------Test Code-----------
DECLARE @Index INT = 0
,@PageSize INT = 300
EXECUTE [dbo].[Snippets_SelectAll]
@Index
,@PageSize
SELECT *
FROM [dbo].[Snippets]
---------------------------------*/
BEGIN
SELECT [Id]
,[RegionTypeId]
,RegionName = (
SELECT [Name]
FROM [dbo].[RegionTypes]
WHERE [RegionTypes].[Id] = [RegionTypeId]
)
,[Name]
,[Logo]
,[Code]
,[LocationId]
--Working JSON output for Locations
--,LocationDetails = (
-- SELECT [Name]
-- ,[LocationTypeId]
-- ,[LineOne]
-- ,[LineTwo]
-- ,[City]
-- ,[Zip]
-- ,[StateId]
-- ,[Latitude]
-- ,[Longitude]
-- ,[DateCreated]
-- ,[DateModified]
-- FROM [dbo].[Locations]
-- WHERE [Locations].[Id] = [LocationId]
-- FOR JSON AUTO
-- )
,[SiteUrl]
,[RegistrationDate]
,[StatusId]
,StatusName = (
SELECT [Name]
FROM [dbo].[StatusType]
WHERE [StatusType].[Id] = [Snippets].StatusId
)
,[IsAlliance]
,AssignedAlliances = (
CASE WHEN [Snippets].[IsAlliance] = 0
THEN (
SELECT
AllianceName = (
SELECT [Name]
FROM [dbo].[Snippets]
WHERE [AllianceId] = [Snippets].Id
)
,AllianceLogoUrl = (
SELECT [Logo]
FROM [dbo].[Snippets]
WHERE [AllianceId] = [Snippets].Id
)
,CompositeRef = (SELECT [AllianceId], [Id] AS SnippetId FOR JSON PATH)
FROM [dbo].[SnippetAlliances]
WHERE [SnippetAlliances].SnippetId = [Id]
FOR JSON AUTO
)
END
)
,AllianceMembers = (
CASE WHEN [Snippets].[IsAlliance] = 1
THEN (
SELECT
SnippetName = (
SELECT [Name]
FROM [dbo].[Snippets]
WHERE [SnippetId] = [Snippets].Id
)
,SnippetLogoUrl = (
SELECT [Logo]
FROM [dbo].[Snippets]
WHERE [SnippetId] = [Snippets].Id
)
,CompositeRef = (SELECT [Id] AS AllianceId, [SnippetId] FOR JSON PATH)
FROM [dbo].[SnippetAlliances]
WHERE [SnippetAlliances].AllianceId = [Id]
FOR JSON AUTO
)
END
)
,TotalCount = COUNT(1) OVER()
FROM [dbo].[Snippets]
ORDER BY [Id]
OFFSET @Index ROW
FETCH NEXT @PageSize ROWS ONLY
END