FakeDataConnection.sql
· 14 KiB · MySQL
Eredeti
-- ============================================================================
-- Setup Growth Plan Activity Demographic for Survey
-- ============================================================================
-- This script creates and populates a demographic to track Growth Plan activity
-- (Not Started, Draft, Shared) for survey participants.
--
-- USAGE: Set the variables below for your target company/survey
-- ============================================================================
-- ========== CONFIGURATION VARIABLES ==========
DECLARE @CompanyId INT = ; -- Analytics CompanyId
DECLARE @SurveyId INT = ; -- Target Survey ID
DECLARE @DemographicName NVARCHAR(100) = 'Growth Plan Activity';
-- ========== INTERNAL VARIABLES (Auto-populated) ==========
DECLARE @OrganizationId INT;
DECLARE @DemographicSlot INT;
DECLARE @CompanyDemographicId INT;
DECLARE @CompanyDemographicNameMappingId INT;
DECLARE @CompanyDemographicValueId INT;
DECLARE @CompanyDemographicValueMappingId_NotStarted INT;
DECLARE @CompanyDemographicValueMappingId_Draft INT;
DECLARE @CompanyDemographicValueMappingId_Shared INT;
DECLARE @SurveyDemographicId INT;
DECLARE @DynamicSQL NVARCHAR(MAX);
DECLARE @DemographicColumn NVARCHAR(50);
BEGIN TRANSACTION;
BEGIN TRY
-- ========== STEP 1: Lookup OrganizationId from CompanyId ==========
SELECT @OrganizationId = o.Id
FROM Analytics.survey.Organization o
WHERE o.CompanyId = @CompanyId;
IF @OrganizationId IS NULL
BEGIN
RAISERROR('Unable to find OrganizationId for CompanyId: %d', 16, 1, @CompanyId);
END
ELSE
BEGIN
PRINT 'Found OrganizationId: ' + CAST(@OrganizationId AS VARCHAR) + ' for CompanyId: ' + CAST(@CompanyId AS VARCHAR);
END;
-- ========== STEP 3: Check if CompanyDemographic exists ==========
SELECT
@CompanyDemographicId = cd.Id,
@CompanyDemographicNameMappingId = cdnm.Id
FROM Analytics.survey.CompanyDemographic cd
INNER JOIN Analytics.survey.CompanyDemographicNameMapping cdnm ON cdnm.CompanyDemographicId = cd.Id
WHERE cd.CompanyId = @CompanyId
AND cdnm.Text = @DemographicName;
IF @CompanyDemographicId IS NULL
BEGIN
-- ========== Create all Company Demographic structures at once ==========
PRINT 'CompanyDemographic not found. Creating new demographic structure for CompanyId: ' + CAST(@CompanyId AS VARCHAR);
-- Create CompanyDemographic
INSERT INTO Analytics.survey.CompanyDemographic (CompanyId)
VALUES (@CompanyId);
SET @CompanyDemographicId = SCOPE_IDENTITY();
PRINT ' Created CompanyDemographicId: ' + CAST(@CompanyDemographicId AS VARCHAR);
-- Create CompanyDemographicNameMapping
INSERT INTO Analytics.survey.CompanyDemographicNameMapping (CompanyDemographicId, Text)
VALUES (@CompanyDemographicId, @DemographicName);
SET @CompanyDemographicNameMappingId = SCOPE_IDENTITY();
PRINT ' Created CompanyDemographicNameMappingId: ' + CAST(@CompanyDemographicNameMappingId AS VARCHAR);
-- Create CompanyDemographicValue
INSERT INTO Analytics.survey.CompanyDemographicValue (CompanyDemographicId)
VALUES (@CompanyDemographicId);
SET @CompanyDemographicValueId = SCOPE_IDENTITY();
PRINT ' Created CompanyDemographicValueId: ' + CAST(@CompanyDemographicValueId AS VARCHAR);
-- Create all three CompanyDemographicValueMappings
INSERT INTO Analytics.survey.CompanyDemographicValueMapping (Text, CompanyDemographicValueId)
VALUES ('Not Started', @CompanyDemographicValueId);
SET @CompanyDemographicValueMappingId_NotStarted = SCOPE_IDENTITY();
PRINT ' Created CompanyDemographicValueMapping "Not Started": ' + CAST(@CompanyDemographicValueMappingId_NotStarted AS VARCHAR);
INSERT INTO Analytics.survey.CompanyDemographicValueMapping (Text, CompanyDemographicValueId)
VALUES ('Draft', @CompanyDemographicValueId);
SET @CompanyDemographicValueMappingId_Draft = SCOPE_IDENTITY();
PRINT ' Created CompanyDemographicValueMapping "Draft": ' + CAST(@CompanyDemographicValueMappingId_Draft AS VARCHAR);
INSERT INTO Analytics.survey.CompanyDemographicValueMapping (Text, CompanyDemographicValueId)
VALUES ('Shared', @CompanyDemographicValueId);
SET @CompanyDemographicValueMappingId_Shared = SCOPE_IDENTITY();
PRINT ' Created CompanyDemographicValueMapping "Shared": ' + CAST(@CompanyDemographicValueMappingId_Shared AS VARCHAR);
END
ELSE
BEGIN
-- ========== CompanyDemographic exists, retrieve all related IDs ==========
PRINT 'Found existing CompanyDemographic structure:';
PRINT ' CompanyDemographicId: ' + CAST(@CompanyDemographicId AS VARCHAR);
PRINT ' CompanyDemographicNameMappingId: ' + CAST(@CompanyDemographicNameMappingId AS VARCHAR);
-- Get CompanyDemographicValueId
SELECT @CompanyDemographicValueId = Id
FROM Analytics.survey.CompanyDemographicValue
WHERE CompanyDemographicId = @CompanyDemographicId;
PRINT ' CompanyDemographicValueId: ' + CAST(@CompanyDemographicValueId AS VARCHAR);
-- Get all three CompanyDemographicValueMapping IDs
SELECT @CompanyDemographicValueMappingId_NotStarted = Id
FROM Analytics.survey.CompanyDemographicValueMapping
WHERE CompanyDemographicValueId = @CompanyDemographicValueId
AND Text = 'Not Started';
PRINT ' ValueMapping "Not Started": ' + CAST(@CompanyDemographicValueMappingId_NotStarted AS VARCHAR);
SELECT @CompanyDemographicValueMappingId_Draft = Id
FROM Analytics.survey.CompanyDemographicValueMapping
WHERE CompanyDemographicValueId = @CompanyDemographicValueId
AND Text = 'Draft';
PRINT ' ValueMapping "Draft": ' + CAST(@CompanyDemographicValueMappingId_Draft AS VARCHAR);
SELECT @CompanyDemographicValueMappingId_Shared = Id
FROM Analytics.survey.CompanyDemographicValueMapping
WHERE CompanyDemographicValueId = @CompanyDemographicValueId
AND Text = 'Shared';
PRINT ' ValueMapping "Shared": ' + CAST(@CompanyDemographicValueMappingId_Shared AS VARCHAR);
END
-- ========== STEP 4: Check if SurveyDemographic exists ==========
SELECT @SurveyDemographicId = Id
FROM Analytics.survey.SurveyDemographic
WHERE SurveyId = @SurveyId
AND CompanyDemographicNameMappingId = @CompanyDemographicNameMappingId;
IF @SurveyDemographicId IS NULL
BEGIN
-- ========== Create SurveyDemographic and all SurveyDemographicValues at once ==========
PRINT 'SurveyDemographic not found. Creating demographic for SurveyId: ' + CAST(@SurveyId AS VARCHAR) + ', Slot: ' + CAST(@DemographicSlot AS VARCHAR);
-- ========== Find next available demographic slot (<=200) ==========
WITH UsedSlots AS (
SELECT Slot
FROM Analytics.survey.SurveyDemographic
WHERE SurveyId = @SurveyId
),
AvailableSlots AS (
SELECT TOP 1 n AS Slot
FROM (
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS n
FROM sys.all_objects
) Numbers
WHERE n <= 200
AND n NOT IN (SELECT Slot FROM UsedSlots)
ORDER BY n
)
SELECT @DemographicSlot = Slot
FROM AvailableSlots;
IF @DemographicSlot IS NULL
BEGIN
RAISERROR('No available demographic slots found (all slots 1-200 are in use for SurveyId: %d)', 16, 1, @SurveyId);
END
ELSE
BEGIN
PRINT 'Selected available demographic slot: ' + CAST(@DemographicSlot AS VARCHAR);
END
-- Create SurveyDemographic
INSERT INTO Analytics.survey.SurveyDemographic (
SurveyId,
Slot,
Name,
DemographicType,
SliceAccessLevel,
FilterAccessLevel,
IsSelfReported,
SurveyQuestionId,
VisibilityLevel,
CompanyDemographicNameMappingId
)
VALUES (
@SurveyId,
@DemographicSlot,
@DemographicName,
1, -- DemographicType
2, -- SliceAccessLevel
2, -- FilterAccessLevel
0, -- IsSelfReported
NULL, -- SurveyQuestionId
1, -- VisibilityLevel
@CompanyDemographicNameMappingId
);
SET @SurveyDemographicId = SCOPE_IDENTITY();
PRINT ' Created SurveyDemographicId: ' + CAST(@SurveyDemographicId AS VARCHAR);
-- Create all three SurveyDemographicValues
INSERT INTO Analytics.survey.SurveyDemographicValue (
SurveyDemographicId,
SurveyId,
Value,
Code,
TextValue1,
CompanyDemographicValueMappingId
)
VALUES (
@SurveyDemographicId,
@SurveyId,
'Not Started',
0,
'Not Started',
@CompanyDemographicValueMappingId_NotStarted
);
PRINT ' Created SurveyDemographicValue "Not Started"';
INSERT INTO Analytics.survey.SurveyDemographicValue (
SurveyDemographicId,
SurveyId,
Value,
Code,
TextValue1,
CompanyDemographicValueMappingId
)
VALUES (
@SurveyDemographicId,
@SurveyId,
'Draft',
1,
'Draft',
@CompanyDemographicValueMappingId_Draft
);
PRINT ' Created SurveyDemographicValue "Draft"';
INSERT INTO Analytics.survey.SurveyDemographicValue (
SurveyDemographicId,
SurveyId,
Value,
Code,
TextValue1,
CompanyDemographicValueMappingId
)
VALUES (
@SurveyDemographicId,
@SurveyId,
'Shared',
2,
'Shared',
@CompanyDemographicValueMappingId_Shared
);
PRINT ' Created SurveyDemographicValue "Shared"';
-- Update the slot variable to the one we used
SELECT @DemographicSlot = Slot
FROM Analytics.survey.SurveyDemographic
WHERE Id = @SurveyDemographicId;
END
ELSE
BEGIN
-- ========== SurveyDemographic exists, retrieve the slot ==========
PRINT 'Found existing SurveyDemographic:';
PRINT ' SurveyDemographicId: ' + CAST(@SurveyDemographicId AS VARCHAR);
SELECT @DemographicSlot = Slot
FROM Analytics.survey.SurveyDemographic
WHERE Id = @SurveyDemographicId;
PRINT ' Using existing Slot: ' + CAST(@DemographicSlot AS VARCHAR);
END
-- ========== STEP 5: Update SurveyParticipant with Growth Plan State ==========
PRINT 'Updating SurveyParticipant Growth Plan state for demographic slot ' + CAST(@DemographicSlot AS VARCHAR) + '...';
SET @DemographicColumn = 'DemographicValue' + CAST(@DemographicSlot AS VARCHAR);
SET @DynamicSQL = N'
UPDATE sp
SET sp.' + @DemographicColumn + N' = growthUser.GrowthPlanState
FROM Analytics.survey.SurveyParticipant sp
JOIN (
SELECT au.Id,
CASE
WHEN gp.PublishedAt IS NOT NULL THEN 2
WHEN gp.LastActivityAt > gp.CreatedAt OR cv.UpdatedAt > cv.CreatedAt THEN 1
ELSE 0
END AS GrowthPlanState
FROM Growth.develop.GrowthPlan gp
JOIN Growth.ec.[User] u ON u.Id = gp.OwnerId
JOIN Analytics.survey.Users au ON au.MembershipId = u.Id
LEFT JOIN Growth.develop.CareerVision cv on cv.UserId = u.Id
WHERE u.OrganizationId = @OrganizationId
) growthUser ON sp.UserId = growthUser.Id AND sp.SurveyId = @SurveyId';
EXEC sp_executesql @DynamicSQL,
N'@OrganizationId INT, @SurveyId INT',
@OrganizationId = @OrganizationId,
@SurveyId = @SurveyId;
PRINT 'SurveyParticipant update complete.';
COMMIT TRANSACTION;
PRINT '';
PRINT '========== SUCCESS: All operations completed successfully ==========';
PRINT '';
PRINT 'Summary:';
PRINT ' CompanyId: ' + CAST(@CompanyId AS VARCHAR);
PRINT ' OrganizationId: ' + CAST(@OrganizationId AS VARCHAR);
PRINT ' SurveyId: ' + CAST(@SurveyId AS VARCHAR);
PRINT ' Demographic Slot: ' + CAST(@DemographicSlot AS VARCHAR);
PRINT ' Demographic Name: ' + @DemographicName;
PRINT '';
PRINT 'Generated IDs:';
PRINT ' CompanyDemographicId: ' + CAST(@CompanyDemographicId AS VARCHAR);
PRINT ' CompanyDemographicNameMappingId: ' + CAST(@CompanyDemographicNameMappingId AS VARCHAR);
PRINT ' CompanyDemographicValueId: ' + CAST(@CompanyDemographicValueId AS VARCHAR);
PRINT ' ValueMapping - Not Started: ' + CAST(@CompanyDemographicValueMappingId_NotStarted AS VARCHAR);
PRINT ' ValueMapping - Draft: ' + CAST(@CompanyDemographicValueMappingId_Draft AS VARCHAR);
PRINT ' ValueMapping - Shared: ' + CAST(@CompanyDemographicValueMappingId_Shared AS VARCHAR);
PRINT ' SurveyDemographicId: ' + CAST(@SurveyDemographicId AS VARCHAR);
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION;
PRINT '';
PRINT '========== ERROR: Transaction rolled back ==========';
PRINT 'Error Message: ' + ERROR_MESSAGE();
PRINT 'Error Line: ' + CAST(ERROR_LINE() AS VARCHAR);
PRINT 'Error Number: ' + CAST(ERROR_NUMBER() AS VARCHAR);
PRINT 'Error Severity: ' + CAST(ERROR_SEVERITY() AS VARCHAR);
THROW;
END CATCH;
| 1 | -- ============================================================================ |
| 2 | -- Setup Growth Plan Activity Demographic for Survey |
| 3 | -- ============================================================================ |
| 4 | -- This script creates and populates a demographic to track Growth Plan activity |
| 5 | -- (Not Started, Draft, Shared) for survey participants. |
| 6 | -- |
| 7 | -- USAGE: Set the variables below for your target company/survey |
| 8 | -- ============================================================================ |
| 9 | |
| 10 | -- ========== CONFIGURATION VARIABLES ========== |
| 11 | DECLARE @CompanyId INT = ; -- Analytics CompanyId |
| 12 | DECLARE @SurveyId INT = ; -- Target Survey ID |
| 13 | DECLARE @DemographicName NVARCHAR(100) = 'Growth Plan Activity'; |
| 14 | |
| 15 | -- ========== INTERNAL VARIABLES (Auto-populated) ========== |
| 16 | DECLARE @OrganizationId INT; |
| 17 | DECLARE @DemographicSlot INT; |
| 18 | DECLARE @CompanyDemographicId INT; |
| 19 | DECLARE @CompanyDemographicNameMappingId INT; |
| 20 | DECLARE @CompanyDemographicValueId INT; |
| 21 | DECLARE @CompanyDemographicValueMappingId_NotStarted INT; |
| 22 | DECLARE @CompanyDemographicValueMappingId_Draft INT; |
| 23 | DECLARE @CompanyDemographicValueMappingId_Shared INT; |
| 24 | DECLARE @SurveyDemographicId INT; |
| 25 | DECLARE @DynamicSQL NVARCHAR(MAX); |
| 26 | DECLARE @DemographicColumn NVARCHAR(50); |
| 27 | |
| 28 | BEGIN TRANSACTION; |
| 29 | BEGIN TRY |
| 30 | |
| 31 | -- ========== STEP 1: Lookup OrganizationId from CompanyId ========== |
| 32 | SELECT @OrganizationId = o.Id |
| 33 | FROM Analytics.survey.Organization o |
| 34 | WHERE o.CompanyId = @CompanyId; |
| 35 | |
| 36 | IF @OrganizationId IS NULL |
| 37 | BEGIN |
| 38 | RAISERROR('Unable to find OrganizationId for CompanyId: %d', 16, 1, @CompanyId); |
| 39 | END |
| 40 | ELSE |
| 41 | BEGIN |
| 42 | PRINT 'Found OrganizationId: ' + CAST(@OrganizationId AS VARCHAR) + ' for CompanyId: ' + CAST(@CompanyId AS VARCHAR); |
| 43 | END; |
| 44 | |
| 45 | -- ========== STEP 3: Check if CompanyDemographic exists ========== |
| 46 | SELECT |
| 47 | @CompanyDemographicId = cd.Id, |
| 48 | @CompanyDemographicNameMappingId = cdnm.Id |
| 49 | FROM Analytics.survey.CompanyDemographic cd |
| 50 | INNER JOIN Analytics.survey.CompanyDemographicNameMapping cdnm ON cdnm.CompanyDemographicId = cd.Id |
| 51 | WHERE cd.CompanyId = @CompanyId |
| 52 | AND cdnm.Text = @DemographicName; |
| 53 | |
| 54 | IF @CompanyDemographicId IS NULL |
| 55 | BEGIN |
| 56 | -- ========== Create all Company Demographic structures at once ========== |
| 57 | PRINT 'CompanyDemographic not found. Creating new demographic structure for CompanyId: ' + CAST(@CompanyId AS VARCHAR); |
| 58 | |
| 59 | -- Create CompanyDemographic |
| 60 | INSERT INTO Analytics.survey.CompanyDemographic (CompanyId) |
| 61 | VALUES (@CompanyId); |
| 62 | SET @CompanyDemographicId = SCOPE_IDENTITY(); |
| 63 | PRINT ' Created CompanyDemographicId: ' + CAST(@CompanyDemographicId AS VARCHAR); |
| 64 | |
| 65 | -- Create CompanyDemographicNameMapping |
| 66 | INSERT INTO Analytics.survey.CompanyDemographicNameMapping (CompanyDemographicId, Text) |
| 67 | VALUES (@CompanyDemographicId, @DemographicName); |
| 68 | SET @CompanyDemographicNameMappingId = SCOPE_IDENTITY(); |
| 69 | PRINT ' Created CompanyDemographicNameMappingId: ' + CAST(@CompanyDemographicNameMappingId AS VARCHAR); |
| 70 | |
| 71 | -- Create CompanyDemographicValue |
| 72 | INSERT INTO Analytics.survey.CompanyDemographicValue (CompanyDemographicId) |
| 73 | VALUES (@CompanyDemographicId); |
| 74 | SET @CompanyDemographicValueId = SCOPE_IDENTITY(); |
| 75 | PRINT ' Created CompanyDemographicValueId: ' + CAST(@CompanyDemographicValueId AS VARCHAR); |
| 76 | |
| 77 | -- Create all three CompanyDemographicValueMappings |
| 78 | INSERT INTO Analytics.survey.CompanyDemographicValueMapping (Text, CompanyDemographicValueId) |
| 79 | VALUES ('Not Started', @CompanyDemographicValueId); |
| 80 | SET @CompanyDemographicValueMappingId_NotStarted = SCOPE_IDENTITY(); |
| 81 | PRINT ' Created CompanyDemographicValueMapping "Not Started": ' + CAST(@CompanyDemographicValueMappingId_NotStarted AS VARCHAR); |
| 82 | |
| 83 | INSERT INTO Analytics.survey.CompanyDemographicValueMapping (Text, CompanyDemographicValueId) |
| 84 | VALUES ('Draft', @CompanyDemographicValueId); |
| 85 | SET @CompanyDemographicValueMappingId_Draft = SCOPE_IDENTITY(); |
| 86 | PRINT ' Created CompanyDemographicValueMapping "Draft": ' + CAST(@CompanyDemographicValueMappingId_Draft AS VARCHAR); |
| 87 | |
| 88 | INSERT INTO Analytics.survey.CompanyDemographicValueMapping (Text, CompanyDemographicValueId) |
| 89 | VALUES ('Shared', @CompanyDemographicValueId); |
| 90 | SET @CompanyDemographicValueMappingId_Shared = SCOPE_IDENTITY(); |
| 91 | PRINT ' Created CompanyDemographicValueMapping "Shared": ' + CAST(@CompanyDemographicValueMappingId_Shared AS VARCHAR); |
| 92 | END |
| 93 | ELSE |
| 94 | BEGIN |
| 95 | -- ========== CompanyDemographic exists, retrieve all related IDs ========== |
| 96 | PRINT 'Found existing CompanyDemographic structure:'; |
| 97 | PRINT ' CompanyDemographicId: ' + CAST(@CompanyDemographicId AS VARCHAR); |
| 98 | PRINT ' CompanyDemographicNameMappingId: ' + CAST(@CompanyDemographicNameMappingId AS VARCHAR); |
| 99 | |
| 100 | -- Get CompanyDemographicValueId |
| 101 | SELECT @CompanyDemographicValueId = Id |
| 102 | FROM Analytics.survey.CompanyDemographicValue |
| 103 | WHERE CompanyDemographicId = @CompanyDemographicId; |
| 104 | PRINT ' CompanyDemographicValueId: ' + CAST(@CompanyDemographicValueId AS VARCHAR); |
| 105 | |
| 106 | -- Get all three CompanyDemographicValueMapping IDs |
| 107 | SELECT @CompanyDemographicValueMappingId_NotStarted = Id |
| 108 | FROM Analytics.survey.CompanyDemographicValueMapping |
| 109 | WHERE CompanyDemographicValueId = @CompanyDemographicValueId |
| 110 | AND Text = 'Not Started'; |
| 111 | PRINT ' ValueMapping "Not Started": ' + CAST(@CompanyDemographicValueMappingId_NotStarted AS VARCHAR); |
| 112 | |
| 113 | SELECT @CompanyDemographicValueMappingId_Draft = Id |
| 114 | FROM Analytics.survey.CompanyDemographicValueMapping |
| 115 | WHERE CompanyDemographicValueId = @CompanyDemographicValueId |
| 116 | AND Text = 'Draft'; |
| 117 | PRINT ' ValueMapping "Draft": ' + CAST(@CompanyDemographicValueMappingId_Draft AS VARCHAR); |
| 118 | |
| 119 | SELECT @CompanyDemographicValueMappingId_Shared = Id |
| 120 | FROM Analytics.survey.CompanyDemographicValueMapping |
| 121 | WHERE CompanyDemographicValueId = @CompanyDemographicValueId |
| 122 | AND Text = 'Shared'; |
| 123 | PRINT ' ValueMapping "Shared": ' + CAST(@CompanyDemographicValueMappingId_Shared AS VARCHAR); |
| 124 | END |
| 125 | |
| 126 | -- ========== STEP 4: Check if SurveyDemographic exists ========== |
| 127 | SELECT @SurveyDemographicId = Id |
| 128 | FROM Analytics.survey.SurveyDemographic |
| 129 | WHERE SurveyId = @SurveyId |
| 130 | AND CompanyDemographicNameMappingId = @CompanyDemographicNameMappingId; |
| 131 | |
| 132 | IF @SurveyDemographicId IS NULL |
| 133 | BEGIN |
| 134 | -- ========== Create SurveyDemographic and all SurveyDemographicValues at once ========== |
| 135 | PRINT 'SurveyDemographic not found. Creating demographic for SurveyId: ' + CAST(@SurveyId AS VARCHAR) + ', Slot: ' + CAST(@DemographicSlot AS VARCHAR); |
| 136 | |
| 137 | |
| 138 | -- ========== Find next available demographic slot (<=200) ========== |
| 139 | WITH UsedSlots AS ( |
| 140 | SELECT Slot |
| 141 | FROM Analytics.survey.SurveyDemographic |
| 142 | WHERE SurveyId = @SurveyId |
| 143 | ), |
| 144 | AvailableSlots AS ( |
| 145 | SELECT TOP 1 n AS Slot |
| 146 | FROM ( |
| 147 | SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS n |
| 148 | FROM sys.all_objects |
| 149 | ) Numbers |
| 150 | WHERE n <= 200 |
| 151 | AND n NOT IN (SELECT Slot FROM UsedSlots) |
| 152 | ORDER BY n |
| 153 | ) |
| 154 | SELECT @DemographicSlot = Slot |
| 155 | FROM AvailableSlots; |
| 156 | |
| 157 | IF @DemographicSlot IS NULL |
| 158 | BEGIN |
| 159 | RAISERROR('No available demographic slots found (all slots 1-200 are in use for SurveyId: %d)', 16, 1, @SurveyId); |
| 160 | END |
| 161 | ELSE |
| 162 | BEGIN |
| 163 | PRINT 'Selected available demographic slot: ' + CAST(@DemographicSlot AS VARCHAR); |
| 164 | END |
| 165 | |
| 166 | -- Create SurveyDemographic |
| 167 | INSERT INTO Analytics.survey.SurveyDemographic ( |
| 168 | SurveyId, |
| 169 | Slot, |
| 170 | Name, |
| 171 | DemographicType, |
| 172 | SliceAccessLevel, |
| 173 | FilterAccessLevel, |
| 174 | IsSelfReported, |
| 175 | SurveyQuestionId, |
| 176 | VisibilityLevel, |
| 177 | CompanyDemographicNameMappingId |
| 178 | ) |
| 179 | VALUES ( |
| 180 | @SurveyId, |
| 181 | @DemographicSlot, |
| 182 | @DemographicName, |
| 183 | 1, -- DemographicType |
| 184 | 2, -- SliceAccessLevel |
| 185 | 2, -- FilterAccessLevel |
| 186 | 0, -- IsSelfReported |
| 187 | NULL, -- SurveyQuestionId |
| 188 | 1, -- VisibilityLevel |
| 189 | @CompanyDemographicNameMappingId |
| 190 | ); |
| 191 | SET @SurveyDemographicId = SCOPE_IDENTITY(); |
| 192 | PRINT ' Created SurveyDemographicId: ' + CAST(@SurveyDemographicId AS VARCHAR); |
| 193 | |
| 194 | -- Create all three SurveyDemographicValues |
| 195 | INSERT INTO Analytics.survey.SurveyDemographicValue ( |
| 196 | SurveyDemographicId, |
| 197 | SurveyId, |
| 198 | Value, |
| 199 | Code, |
| 200 | TextValue1, |
| 201 | CompanyDemographicValueMappingId |
| 202 | ) |
| 203 | VALUES ( |
| 204 | @SurveyDemographicId, |
| 205 | @SurveyId, |
| 206 | 'Not Started', |
| 207 | 0, |
| 208 | 'Not Started', |
| 209 | @CompanyDemographicValueMappingId_NotStarted |
| 210 | ); |
| 211 | PRINT ' Created SurveyDemographicValue "Not Started"'; |
| 212 | |
| 213 | INSERT INTO Analytics.survey.SurveyDemographicValue ( |
| 214 | SurveyDemographicId, |
| 215 | SurveyId, |
| 216 | Value, |
| 217 | Code, |
| 218 | TextValue1, |
| 219 | CompanyDemographicValueMappingId |
| 220 | ) |
| 221 | VALUES ( |
| 222 | @SurveyDemographicId, |
| 223 | @SurveyId, |
| 224 | 'Draft', |
| 225 | 1, |
| 226 | 'Draft', |
| 227 | @CompanyDemographicValueMappingId_Draft |
| 228 | ); |
| 229 | PRINT ' Created SurveyDemographicValue "Draft"'; |
| 230 | |
| 231 | INSERT INTO Analytics.survey.SurveyDemographicValue ( |
| 232 | SurveyDemographicId, |
| 233 | SurveyId, |
| 234 | Value, |
| 235 | Code, |
| 236 | TextValue1, |
| 237 | CompanyDemographicValueMappingId |
| 238 | ) |
| 239 | VALUES ( |
| 240 | @SurveyDemographicId, |
| 241 | @SurveyId, |
| 242 | 'Shared', |
| 243 | 2, |
| 244 | 'Shared', |
| 245 | @CompanyDemographicValueMappingId_Shared |
| 246 | ); |
| 247 | PRINT ' Created SurveyDemographicValue "Shared"'; |
| 248 | |
| 249 | -- Update the slot variable to the one we used |
| 250 | SELECT @DemographicSlot = Slot |
| 251 | FROM Analytics.survey.SurveyDemographic |
| 252 | WHERE Id = @SurveyDemographicId; |
| 253 | END |
| 254 | ELSE |
| 255 | BEGIN |
| 256 | -- ========== SurveyDemographic exists, retrieve the slot ========== |
| 257 | PRINT 'Found existing SurveyDemographic:'; |
| 258 | PRINT ' SurveyDemographicId: ' + CAST(@SurveyDemographicId AS VARCHAR); |
| 259 | |
| 260 | SELECT @DemographicSlot = Slot |
| 261 | FROM Analytics.survey.SurveyDemographic |
| 262 | WHERE Id = @SurveyDemographicId; |
| 263 | PRINT ' Using existing Slot: ' + CAST(@DemographicSlot AS VARCHAR); |
| 264 | END |
| 265 | |
| 266 | |
| 267 | -- ========== STEP 5: Update SurveyParticipant with Growth Plan State ========== |
| 268 | PRINT 'Updating SurveyParticipant Growth Plan state for demographic slot ' + CAST(@DemographicSlot AS VARCHAR) + '...'; |
| 269 | |
| 270 | SET @DemographicColumn = 'DemographicValue' + CAST(@DemographicSlot AS VARCHAR); |
| 271 | SET @DynamicSQL = N' |
| 272 | UPDATE sp |
| 273 | SET sp.' + @DemographicColumn + N' = growthUser.GrowthPlanState |
| 274 | FROM Analytics.survey.SurveyParticipant sp |
| 275 | JOIN ( |
| 276 | SELECT au.Id, |
| 277 | CASE |
| 278 | WHEN gp.PublishedAt IS NOT NULL THEN 2 |
| 279 | WHEN gp.LastActivityAt > gp.CreatedAt OR cv.UpdatedAt > cv.CreatedAt THEN 1 |
| 280 | ELSE 0 |
| 281 | END AS GrowthPlanState |
| 282 | FROM Growth.develop.GrowthPlan gp |
| 283 | JOIN Growth.ec.[User] u ON u.Id = gp.OwnerId |
| 284 | JOIN Analytics.survey.Users au ON au.MembershipId = u.Id |
| 285 | LEFT JOIN Growth.develop.CareerVision cv on cv.UserId = u.Id |
| 286 | WHERE u.OrganizationId = @OrganizationId |
| 287 | ) growthUser ON sp.UserId = growthUser.Id AND sp.SurveyId = @SurveyId'; |
| 288 | |
| 289 | EXEC sp_executesql @DynamicSQL, |
| 290 | N'@OrganizationId INT, @SurveyId INT', |
| 291 | @OrganizationId = @OrganizationId, |
| 292 | @SurveyId = @SurveyId; |
| 293 | |
| 294 | PRINT 'SurveyParticipant update complete.'; |
| 295 | |
| 296 | COMMIT TRANSACTION; |
| 297 | PRINT ''; |
| 298 | PRINT '========== SUCCESS: All operations completed successfully =========='; |
| 299 | PRINT ''; |
| 300 | PRINT 'Summary:'; |
| 301 | PRINT ' CompanyId: ' + CAST(@CompanyId AS VARCHAR); |
| 302 | PRINT ' OrganizationId: ' + CAST(@OrganizationId AS VARCHAR); |
| 303 | PRINT ' SurveyId: ' + CAST(@SurveyId AS VARCHAR); |
| 304 | PRINT ' Demographic Slot: ' + CAST(@DemographicSlot AS VARCHAR); |
| 305 | PRINT ' Demographic Name: ' + @DemographicName; |
| 306 | PRINT ''; |
| 307 | PRINT 'Generated IDs:'; |
| 308 | PRINT ' CompanyDemographicId: ' + CAST(@CompanyDemographicId AS VARCHAR); |
| 309 | PRINT ' CompanyDemographicNameMappingId: ' + CAST(@CompanyDemographicNameMappingId AS VARCHAR); |
| 310 | PRINT ' CompanyDemographicValueId: ' + CAST(@CompanyDemographicValueId AS VARCHAR); |
| 311 | PRINT ' ValueMapping - Not Started: ' + CAST(@CompanyDemographicValueMappingId_NotStarted AS VARCHAR); |
| 312 | PRINT ' ValueMapping - Draft: ' + CAST(@CompanyDemographicValueMappingId_Draft AS VARCHAR); |
| 313 | PRINT ' ValueMapping - Shared: ' + CAST(@CompanyDemographicValueMappingId_Shared AS VARCHAR); |
| 314 | PRINT ' SurveyDemographicId: ' + CAST(@SurveyDemographicId AS VARCHAR); |
| 315 | |
| 316 | END TRY |
| 317 | BEGIN CATCH |
| 318 | IF @@TRANCOUNT > 0 |
| 319 | ROLLBACK TRANSACTION; |
| 320 | |
| 321 | PRINT ''; |
| 322 | PRINT '========== ERROR: Transaction rolled back =========='; |
| 323 | PRINT 'Error Message: ' + ERROR_MESSAGE(); |
| 324 | PRINT 'Error Line: ' + CAST(ERROR_LINE() AS VARCHAR); |
| 325 | PRINT 'Error Number: ' + CAST(ERROR_NUMBER() AS VARCHAR); |
| 326 | PRINT 'Error Severity: ' + CAST(ERROR_SEVERITY() AS VARCHAR); |
| 327 | |
| 328 | THROW; |
| 329 | END CATCH; |