Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 2.54 KB | None | 0 0
  1.  
  2. CREATE TYPE WorkoutEntryType
  3.    AS TABLE
  4.       (      Bodyweight DECIMAL(18,2) NULL,
  5.             Comments NVARCHAR(500) NULL,
  6.             Date DATETIME2(7) NOT NULL,
  7.             Energy INT NULL,
  8.             Id UNIQUEIDENTIFIER NOT NULL,
  9.             Minutes FLOAT NULL,
  10.             Rating INT NULL,
  11.             Title NVARCHAR(50) NOT NULL,
  12.             UserId UNIQUEIDENTIFIER NOT NULL
  13.         );
  14.        
  15. GO
  16.  
  17. CREATE TYPE WorkoutItemType
  18.    AS TABLE
  19.       ( Comments NVARCHAR(100) NULL,
  20.             [Order] INT NOT NULL,
  21.             Reps INT NOT NULL,
  22.             RPE INT NULL,
  23.             Sets INT NOT NULL,
  24.             UserExerciseId UNIQUEIDENTIFIER NOT NULL,
  25.             Weight DECIMAL(18,2) NOT NULL,
  26.              WorkoutEntryId UNIQUEIDENTIFIER NOT NULL  
  27.         );
  28. GO
  29.  
  30. CREATE TYPE UserExerciseType
  31.    AS TABLE
  32.       (   Id UNIQUEIDENTIFIER NOT NULL,
  33.           [Name] NVARCHAR(128) NOT NULL,
  34.           ProtocolName NVARCHAR(450) NOT NULL,  
  35.           UserId UNIQUEIDENTIFIER NOT NULL
  36.        );
  37. GO
  38.  
  39. CREATE TYPE IdMappingType
  40.    AS TABLE
  41.       ([Id1] UNIQUEIDENTIFIER NOT NULL,
  42.        [Id2] UNIQUEIDENTIFIER DEFAULT NEWSEQUENTIALID())
  43. GO
  44.  
  45. CREATE PROCEDURE InsertWorkouts(
  46. @Workouts WorkoutEntryType READONLY,
  47. @Items WorkoutItemType READONLY,
  48. @Exercises UserExerciseType READONLY)
  49. AS
  50. BEGIN
  51.  
  52.  
  53.     DECLARE @NewWorkoutIds IdMappingType
  54.  
  55.     INSERT INTO @NewWorkoutIds(Id1)
  56.     SELECT Id as [Id1] FROM @Workouts
  57.  
  58.     INSERT INTO WorkoutEntry(Id,Title,Date,Comments,UserId,Rating,Energy,Bodyweight,Minutes)
  59.     SELECT [Id2],Title,Date,Comments,UserId,Rating,Energy,Bodyweight,Minutes
  60.     FROM @Workouts w
  61.     JOIN @NewWorkoutIds n ON n.[Id1] = w.[Id]
  62.  
  63.     -- Insert completely brand new exercises
  64.     INSERT INTO Exercise(Name)
  65.     SELECT pE.Name FROM @Exercises pE
  66.     LEFT JOIN Exercise e on e.Name = pE.Name
  67.     WHERE e.Name IS NULL
  68.  
  69.     -- Insert new UserExercise for this user
  70.     INSERT INTO UserExercise (UserId,ExerciseId,ExerciseProtocolId)
  71.     SELECT pE.UserId,e.Id,ep.Id FROM @Exercises pE
  72.     JOIN ExerciseProtocol ep on ep.Name = pE.ProtocolName
  73.     JOIN Exercise e on e.Name = pE.Name
  74.     LEFT JOIN UserExercise ue on ue.ExerciseId = e.Id and ue.UserId = pE.UserId
  75.     WHERE ue.Id IS NULL
  76.  
  77.     DECLARE @NewUserExerciseIds IdMappingType
  78.  
  79.     INSERT INTO @NewUserExerciseIds
  80.     SELECT pE.[Id],ue.Id FROM @Exercises pE
  81.     JOIN Exercise e on e.Name = pE.Name
  82.     JOIN UserExercise ue on ue.ExerciseId = e.Id
  83.     WHERE ue.UserId = pE.UserId
  84.  
  85.     INSERT INTO WorkoutItem(WorkoutEntryId,UserExerciseId,Weight,Sets,Reps,Comments,[Order],RPE)
  86.     SELECT n.[Id2],ne.Id2,Weight,Sets,Reps,Comments,[Order],RPE
  87.     FROM @Items wi
  88.     JOIN @NewWorkoutIds n ON n.[Id1] = wi.[WorkoutEntryId]
  89.     JOIN @NewUserExerciseIds ne on ne.Id1 = wi.UserExerciseId
  90.  
  91. END
  92. GO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement