Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 7.16 KB | None | 0 0
  1. IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PV_IRSaveBadgeQuestionsWithAnswersAndContent]') AND type in (N'P', N'PC'))
  2. DROP PROCEDURE [dbo].PV_IRSaveBadgeQuestionsWithAnswersAndContent
  3. GO
  4. PRINT 'Creating procedure PV_IRSaveBadgeQuestionsWithAnswersAndContent';
  5. GO
  6.  
  7. CREATE PROCEDURE [dbo].PV_IRSaveBadgeQuestionsWithAnswersAndContent
  8.     @BadgeDefinitionID BIGINT,
  9.     @ContentDataXML XML,
  10.     @BadgeContentType TINYINT,
  11.     @Result INT OUTPUT
  12. AS
  13. BEGIN
  14.     SET @Result = 0
  15.  
  16.     DECLARE @ContentData TABLE
  17.     (
  18.         QuestionID BIGINT,
  19.         QuestionText NVARCHAR(MAX),
  20.         QuestionType TINYINT,
  21.         QuestionImgID BIGINT
  22.     )
  23.  
  24.     DECLARE @ContentAnswersData TABLE
  25.     (
  26.         QuestionID BIGINT,
  27.         AnswerID BIGINT NULL,
  28.         AnswerText NVARCHAR(MAX) NULL,
  29.         AnswerPoints INT NULL
  30.     )
  31.  
  32.     INSERT INTO @ContentData (QuestionID, QuestionText, QuestionType, QuestionImgID)
  33.     SELECT Tab.Col.value('ID[1]', 'BIGINT') as QuestionID,
  34.         Tab.Col.value('Text[1]', 'NVARCHAR(MAX)') as QuestionText,
  35.         Tab.Col.value('Type[1]', 'TINYINT') as QuestionType,
  36.         Tab.Col.value('ImgID[1]', 'BIGINT') as QuestionImgID
  37.     FROM @ContentDataXML.nodes('/ArrayOfBadgeQuestionAnswersDto/BadgeQuestionAnswersDto') as Tab(Col)
  38.  
  39.     UPDATE cd --cd.QuestionImgID = 0 przy parsowaniu null przekształca na 0 i trzeba odwrucic
  40.     SET cd.QuestionImgID = NULL
  41.     FROM @ContentData as cd
  42.     WHERE cd.QuestionImgID = 0
  43.  
  44.     INSERT INTO @ContentAnswersData (QuestionID, AnswerID, AnswerText, AnswerPoints)
  45.     SELECT Tab.Col.value('ID[1]', 'BIGINT') as QuestionID,
  46.         Tab1.Col1.value('ID[1]', 'BIGINT') as AnswerID,
  47.         Tab1.Col1.value('Text[1]', 'NVARCHAR(MAX)') as AnswerText,
  48.         Tab1.Col1.value('Points[1]', 'INT') as AnswerPoints
  49.     FROM @ContentDataXML.nodes('/ArrayOfBadgeQuestionAnswersDto/BadgeQuestionAnswersDto') as Tab(Col)
  50.         OUTER APPLY Tab.Col.nodes('Answers/BadgeQuestionAnswerDto') as Tab1(Col1)
  51.  
  52.     -- delete not existing rows -------------------------------------------------------------------------
  53.  
  54.     UPDATE bq
  55.     SET bq.IsDeleted = 1,
  56.         bq.DeleteDateUTC = GETUTCDATE(),
  57.         bq.ModificationDateUTC = GETUTCDATE()
  58.     FROM IRBadgeQuestion as bq
  59.         LEFT JOIN @ContentData as cd ON cd.QuestionID = bq.ID
  60.     WHERE cd.QuestionID IS NULL
  61.         AND bq.IsDeleted = 0
  62.         AND bq.IRBadgeDefinitionID = @BadgeDefinitionID
  63.         AND bq.[Type] = @BadgeContentType
  64.  
  65.     IF @@ERROR<>0 GOTO ERROR_PROC;
  66.  
  67.     UPDATE baq
  68.     SET baq.IsDeleted = 1,
  69.         baq.DeleteDateUTC = GETUTCDATE(),
  70.         baq.ModificationDateUTC = GETUTCDATE()
  71.     FROM IRBadgeAnswerQuestion as baq
  72.         INNER JOIN IRBadgeQuestion as bq ON baq.IRBadgeQuestionID = bq.ID
  73.     WHERE baq.IsDeleted = 0
  74.         AND NOT EXISTS (SELECT 1 FROM @ContentAnswersData as cad WHERE cad.QuestionID = baq.IRBadgeQuestionID AND cad.AnswerID = baq.ID)
  75.         AND bq.IRBadgeDefinitionID = @BadgeDefinitionID
  76.         AND @BadgeContentType = 1 /*GameQuestion = 1*/
  77.         AND bq.[Type] = @BadgeContentType
  78.  
  79.     IF @@ERROR<>0 GOTO ERROR_PROC;
  80.  
  81.     -- insert new rows --------------------------------------------------------------------------------
  82.  
  83.     IF EXISTS (SELECT 1 FROM @ContentAnswersData) AND @BadgeContentType = 1 /*GameQuestion = 1*/
  84.     BEGIN
  85.         DECLARE @NewContentData TABLE
  86.         (
  87.             Idx INT IDENTITY(1,1),
  88.             QuestionID BIGINT,
  89.             QuestionText NVARCHAR(MAX),
  90.             QuestionType TINYINT,
  91.            QuestionImgID BIGINT,
  92.             NewQuestionID BIGINT
  93.         )
  94.         INSERT INTO @NewContentData (QuestionID, QuestionText, QuestionType, QuestionImgID, NewQuestionID)
  95.         SELECT QuestionID, QuestionText, QuestionType, QuestionImgID, NULL
  96.         FROM @ContentData
  97.         WHERE QuestionID < 1
  98.  
  99.         DECLARE @i INT = 1
  100.         DECLARE @max INT = (SELECT COUNT(1) FROM @NewContentData)
  101.         DECLARE @id BIGINT
  102.  
  103.         WHILE @i <= @max
  104.         BEGIN
  105.             -- insert new question
  106.             INSERT INTO IRBadgeQuestion (IRBadgeDefinitionID, [Text], ModificationDateUTC, CreateDateUTC, IsDeleted, DeleteDateUTC, [Type], IRFileID)
  107.             SELECT @BadgeDefinitionID, cd.QuestionText, GETUTCDATE(), GETUTCDATE(), 0, NULL, cd.QuestionType, cd.QuestionImgID
  108.             FROM @NewContentData as cd
  109.             WHERE cd.Idx = @i
  110.  
  111.             IF @@ERROR<>0 GOTO ERROR_PROC;
  112.  
  113.             SET @id = @@IDENTITY
  114.  
  115.             -- get new ID
  116.             UPDATE @NewContentData
  117.             SET NewQuestionID = @id
  118.             WHERE Idx = @i
  119.  
  120.             SET @i = @i + 1
  121.         END
  122.  
  123.         IF @@ERROR<>0 GOTO ERROR_PROC;
  124.  
  125.         -- insert new answers
  126.         INSERT INTO IRBadgeAnswerQuestion (IRBadgeQuestionID, [Text], Points, ModificationDateUTC, CreateDateUTC, IsDeleted, DeleteDateUTC)
  127.         SELECT ncd.NewQuestionID, cad.AnswerText, cad.AnswerPoints, GETUTCDATE(), GETUTCDATE(), 0, NULL
  128.         FROM @ContentAnswersData as cad
  129.             INNER JOIN @NewContentData as ncd ON cad.QuestionID = ncd.QuestionID
  130.         WHERE cad.AnswerID < 1
  131.             AND cad.QuestionID < 1
  132.             AND ncd.NewQuestionID IS NOT NULL
  133.  
  134.         IF @@ERROR<>0 GOTO ERROR_PROC;
  135.  
  136.         INSERT INTO IRBadgeAnswerQuestion (IRBadgeQuestionID, [Text], Points, ModificationDateUTC, CreateDateUTC, IsDeleted, DeleteDateUTC)
  137.         SELECT cad.QuestionID, cad.AnswerText, cad.AnswerPoints, GETUTCDATE(), GETUTCDATE(), 0, NULL
  138.         FROM @ContentAnswersData as cad
  139.         WHERE cad.AnswerID < 1
  140.             AND cad.QuestionID > 0
  141.  
  142.         IF @@ERROR<>0 GOTO ERROR_PROC;
  143.     END
  144.     ELSE
  145.     BEGIN
  146.         INSERT INTO IRBadgeQuestion (IRBadgeDefinitionID, [Text], ModificationDateUTC, CreateDateUTC, IsDeleted, DeleteDateUTC, [Type], IRFileID)
  147.         SELECT @BadgeDefinitionID, cd.QuestionText, GETUTCDATE(), GETUTCDATE(), 0, NULL, cd.QuestionType, cd.QuestionImgID
  148.         FROM @ContentData as cd
  149.             LEFT JOIN IRBadgeQuestion bq ON bq.IRBadgeDefinitionID = @BadgeDefinitionID
  150.                                                      AND bq.Text = cd.QuestionText
  151.                                                      AND bq.Type = @BadgeContentType
  152.                                                      AND bq.IsDeleted = 0
  153.         WHERE cd.QuestionID < 1
  154.                 AND bq.ID IS NULL
  155.                 AND ISNULL(cd.QuestionText, '') != ''
  156.  
  157.         IF @@ERROR<>0 GOTO ERROR_PROC;
  158.     END
  159.  
  160.     -- update existing rows -----------------------------------------------------------------------------
  161.  
  162.     UPDATE bq
  163.     SET bq.[Text] = cd.QuestionText,
  164.         bq.[Type] = cd.QuestionType,
  165.         bq.ModificationDateUTC = GETUTCDATE(),
  166.         bq.IRFileID = cd.QuestionImgID
  167.     FROM IRBadgeQuestion as bq
  168.         INNER JOIN @ContentData as cd ON cd.QuestionID = bq.ID
  169.     WHERE (
  170.             ISNULL(bq.[Text], '') <> cd.QuestionText
  171.             OR bq.[Type] <> cd.QuestionType
  172.             OR bq.IRFileID <> cd.QuestionImgID
  173.         )
  174.         AND bq.IsDeleted = 0
  175.  
  176.     IF @@ERROR<>0 GOTO ERROR_PROC;
  177.  
  178.     UPDATE bq
  179.     SET bq.ModificationDateUTC = GETUTCDATE()
  180.     FROM IRBadgeAnswerQuestion as baq
  181.         INNER JOIN @ContentAnswersData as cad ON cad.AnswerID = baq.ID
  182.         INNER JOIN IRBadgeQuestion as bq ON bq.ID = baq.IRBadgeQuestionID
  183.     WHERE (
  184.             ISNULL(baq.[Text], '') <> cad.AnswerText
  185.             OR baq.Points <> cad.AnswerPoints
  186.         )
  187.         AND baq.IsDeleted = 0
  188.  
  189.     IF @@ERROR<>0 GOTO ERROR_PROC;
  190.  
  191.     UPDATE baq
  192.     SET baq.[Text] = cad.AnswerText,
  193.         baq.Points = cad.AnswerPoints,
  194.         baq.ModificationDateUTC = GETUTCDATE()
  195.     FROM IRBadgeAnswerQuestion as baq
  196.         INNER JOIN @ContentAnswersData as cad ON cad.AnswerID = baq.ID
  197.     WHERE (
  198.             ISNULL(baq.[Text], '') <> cad.AnswerText
  199.             OR baq.Points <> cad.AnswerPoints
  200.         )
  201.         AND baq.IsDeleted = 0
  202.    
  203.     IF @@ERROR<>0 GOTO ERROR_PROC;
  204.  
  205.     SET @Result = 1
  206.  
  207.     --------------------------------------------------------------------------
  208.     -- exit point of the store proc
  209.     EXIT_PROC:
  210.     RETURN(0);
  211.  
  212.     ERROR_PROC:
  213.     RETURN(99);
  214. END;
  215. GO
  216. -- end PV_IRSaveBadgeQuestionsWithAnswersAndContent
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement