Guest User

Untitled

a guest
Oct 20th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 3.70 KB | None | 0 0
  1. USE [SDS_DevSchoolDistrict]
  2. GO
  3. /****** Object:  StoredProcedure [dbo].[AddUserDefinedStudentGroup]    Script Date: 09/07/2012 18:15:55 ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. -- =================================================================================
  9. -- Copyright:   School Data Solutions
  10. -- =================================================================================
  11. ALTER PROCEDURE [dbo].[AddUserDefinedStudentGroup]
  12.     @studentGroupId     int OUTPUT,
  13.     @isDuplicate     bit OUTPUT,
  14.     @ReplaceExisting    bit = 0,
  15.  
  16.  
  17.     @ApplicationUserId  int,
  18.     @StudentGroupLabel  varchar(100),
  19.     @StudentIdList      varchar(4000) = NULL
  20. AS
  21. BEGIN  
  22.         --DECLARE @StudentIdList        varchar(4000) = '1, 22, 333, 4444, 55555, 666666'
  23.        
  24.       -------------------------------------------------
  25.       --
  26.       -------------------------------------------------
  27.       DECLARE @UpdateMembership bit = 0
  28.       -------------------------------------------------
  29.        
  30.         -----------------------------------------------
  31.         -- Check for existence of Students group with same name for user
  32.         -----------------------------------------------
  33.         SELECT  @StudentGroupId = SG.StudentGroupId
  34.         FROM    StudentGroups SG
  35.         WHERE   SG.ApplicationUserId = @ApplicationUserId
  36.           AND   SG.StudentGroupName = @StudentGroupLabel
  37.         -----------------------------------------------
  38.         IF(@StudentGroupId IS NOT NULL)
  39.         BEGIN -----------------------------------------
  40.             SET @IsDuplicate = 1
  41.             IF(@ReplaceExisting = 1)       
  42.             BEGIN ---------------------------------
  43.                     UPDATE SG
  44.                     SET     StudentGroupIsStatic = 1,
  45.                             SmartGroupAttributesJson = NULL,
  46.                             MembershipCriteriaFilter = NULL
  47.                     FROM    StudentGroups SG
  48.                     WHERE   SG.StudentGroupId = @StudentGroupId
  49.                     ---------------------------------
  50.                     SET @UpdateMembership = 1
  51.             END  ---------------------------------
  52.         END  -----------------------------------------
  53.         ELSE
  54.         BEGIN   -----------------------------------------
  55.                 SET @IsDuplicate = 0
  56.                 -----------------------------------------------
  57.                 --
  58.                 -----------------------------------------------
  59.                 IF(@StudentGroupId IS NULL)
  60.                 BEGIN
  61.                         INSERT  StudentGroups (
  62.                                 StudentGroupLabel,
  63.                                 StudentGroupName,
  64.                                 ApplicationUserId,
  65.                                 StudentGroupIsStatic,
  66.                                 StudentGroupIsActive,
  67.                                 StudentGroupIsAutomatic,
  68.                                 StudentGroupIsShared,
  69.                                 SaveSnapshotAtCurrentSchoolYearTransition
  70.                         )      
  71.                         VALUES  (
  72.                                 @StudentGroupLabel,
  73.                                 @StudentGroupLabel,
  74.                                 @ApplicationUserId,
  75.                                 1,1,0,0,0
  76.                         )      
  77.                         -----------------------------------------------
  78.                         SET @StudentGroupId = SCOPE_IDENTITY()
  79.                         -----------------------------------------------
  80.                         SET @UpdateMembership = 1
  81.                         -----------------------------------------------
  82.                 END
  83.                 -----------------------------------------------
  84.         END
  85.         -----------------------------------------------
  86.        
  87.  
  88.         -------------------------------------------
  89.         -- if Student-Id's were passed in
  90.         -------------------------------------------
  91.         IF(@UpdateMembership = 1 AND RTRIM(LTRIM(ISNULL(@StudentIdList, ''))) <> '')
  92.         BEGIN   -------------------------------------------
  93.                 --
  94.                 -------------------------------------------
  95.                 DECLARE @StudentIds TABLE ( StudentId   int, Ordinal int IDENTITY )
  96.                 INSERT  @StudentIds
  97.                 SELECT  CAST(Value as int)
  98.                 FROM    dbo.SplitList(@StudentIdList, ',')
  99.                 -------------------------------------------
  100.                 --SELECT * FROM @StudentIds
  101.                 -------------------------------------------
  102.                 INSERT  Students_Groups (StudentId, StudentGroupId)
  103.                 SELECT  StudentId, @StudentGroupId
  104.                 FROM    @StudentIds
  105.                 -------------------------------------------
  106.         END    
  107.        
  108.        
  109.        
  110.        
  111. END
Add Comment
Please, Sign In to add comment