Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 2.30 KB | None | 0 0
  1. USE [Havsdrottningen]
  2. GO
  3. /****** Object:  StoredProcedure [dbo].[AddNewSalongWithChairs]    Script Date: 06/29/2011 19:42:08 ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8.  
  9. ALTER PROCEDURE [dbo].[AddNewSalongWithChairs]
  10.     @Name nvarchar(50)
  11.     , @Description nvarchar(50)
  12.     , @ChairList VARCHAR(8000)
  13.  
  14. AS
  15.     BEGIN TRY
  16.         BEGIN TRAN
  17.        
  18.             IF @Name = '' OR @Name = NULL
  19.                 OR @Description = '' OR @Description = NULL
  20.                 OR @ChairList = '' OR @ChairList = NULL
  21.             BEGIN
  22.                 RAISERROR (N'All fields need to have data'
  23.                     , 15 -- Error level between 11 to 19 will make script go to CATCH.
  24.                     , 1)
  25.             END
  26.        
  27.             INSERT INTO Salong
  28.                 VALUES(@name, @Description)
  29.            
  30.             DECLARE @SalongID INT;
  31.             DECLARE @COUNT INT;
  32.             DECLARE @numofseats INT;
  33.             DECLARE @spot INT;
  34.             DECLARE @seatID INT;
  35.             DECLARE @seatNum INT;
  36.            
  37.             SET @SalongID = CAST(SCOPE_IDENTITY() AS INT)
  38.             SET @COUNT = 0
  39.             SET @seatNum = 0;
  40.  
  41.             WHILE @ChairList <> '' --An loop to go true an list of numbers seperated with an ,
  42.             BEGIN
  43.                 SET @spot = CHARINDEX(',', @ChairList)
  44.                 IF @spot>0
  45.                     BEGIN --Find the first , and take the number before it.
  46.                         SET @numofseats = CONVERT(INT, LEFT(@ChairList, @spot-1))
  47.                         SET @ChairList = RIGHT(@ChairList, LEN(@ChairList)-@spot)
  48.                     END
  49.                 ELSE
  50.                     BEGIN --If there is no , left.
  51.                         SET @numofseats = CONVERT(INT, @ChairList)
  52.                         SET @ChairList = ''
  53.                     END
  54.                
  55.                 IF @numofseats > 999 OR @numofseats <= 0
  56.                 BEGIN
  57.                     RAISERROR (N'Number of seats per row can only be between 1 and 999'
  58.                         , 15 -- Error level between 11 to 19 will make script go to CATCH.
  59.                         , 1)
  60.                 END
  61.                
  62.                 IF @COUNT >= 99
  63.                 BEGIN
  64.                     RAISERROR (N'You cannot have more then 99 rows of seats.'
  65.                         , 15 -- Error level between 11 to 19 will make script go to CATCH.
  66.                         , 1)
  67.                 END
  68.                                
  69.                 INSERT INTO SalongRow
  70.                     VALUES(@SalongID, @COUNT+1);
  71.                    
  72.                 SET @seatID = CAST(SCOPE_IDENTITY() AS INT);
  73.                 SET @COUNT = @COUNT+1;
  74.  
  75.                 WHILE @numofseats > 0
  76.                 BEGIN
  77.                     INSERT INTO dbo.Seat
  78.                         VALUES(@seatID, @seatNum+1);
  79.                     SET @numofseats = @numofseats-1;
  80.                     SET @seatNum = @seatNum + 1;
  81.                 END
  82.                
  83.             END
  84.            
  85.             ;SELECT @SalongID AS ID;
  86.         COMMIT TRAN
  87.     END TRY
  88.     BEGIN CATCH
  89.         ;SELECT ERROR_MESSAGE();
  90.         ROLLBACK TRAN
  91.     END CATCH
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement