Advertisement
Guest User

DB2 Partials

a guest
Nov 15th, 2018
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 2.65 KB | None | 0 0
  1. /*Increase Pilot salaries using cursor and print the results*/
  2. DECLARE @pilotID INT;
  3. DECLARE @pilotName VARCHAR(20);
  4. DECLARE @salary FLOAT;
  5.  
  6. SET NOCOUNT ON;
  7. DECLARE cur CURSOR FOR
  8. SELECT pilotid, firstname, salary FROM pilots
  9. OPEN cur
  10.     print 'Opened cursor';
  11.  
  12.     IF(@@CURSOR_ROWS > 0)
  13.     BEGIN
  14.         FETCH NEXT FROM cur INTO
  15.         @pilotID, @pilotName, @salary;
  16.  
  17.         WHILE @@FETCH_STATUS = 0
  18.         BEGIN
  19.             UPDATE pilots
  20.             SET salary=@salary + 1000
  21.             WHERE pilotid = @pilotID;
  22.  
  23.             print (CONVERT(VARCHAR(20),@pilotID) + ' ' + @pilotName + ', former salary: ' + CONVERT(VARCHAR(20),@salary) + '
  24.                         New salary: ' + CONVERT(VARCHAR(20),@salary+1000));
  25.  
  26.             FETCH NEXT FROM cur INTO
  27.             @pilotID, @pilotName, @salary;
  28.         END
  29.     END
  30. CLOSE cur
  31. DEALLOCATE cur
  32. SET NOCOUNT OFF
  33.  
  34. ------------------------------------------------------------------------------------------------
  35.  
  36. /*I think triggers are required in the question but I didn't use them because I wasn't sure if they were really required*/
  37. /*Example IDs, not the ones in the question. Just change them as needed*/
  38. DECLARE @oldID INT = 2;
  39. DECLARE @newID INT = 3;
  40.  
  41. UPDATE flights
  42. SET pilotid = @newID
  43. WHERE pilotid = @oldID;
  44.  
  45. DELETE FROM pilots
  46. WHERE pilots.pilotid = @oldID;
  47.  
  48. --------------------------------------------------------------------------------------------------
  49.  
  50. /*Use this procedure to create the pilot user from his ID. You can get thi IDs by using a cursor on Pilots table*/
  51. CREATE PROC dbo.grant_pilots
  52.     @pilotID INT
  53. AS
  54. BEGIN
  55.     DECLARE @pilotUsername VARCHAR(30) = 'pilot' + @pilotID;
  56.     DECLARE @grant_query VARCHAR(MAX);
  57.     DECLARE @view_query  VARCHAR(MAX);
  58.    
  59.     DECLARE @create_user VARCHAR(MAX) = 'CREATE LOGIN ' + @pilotUsername + ' WITH PASSWORD = ''' + @pilotID + '''';
  60.     EXEC @create_user;
  61.  
  62.     SET @grant_query = 'GRANT SELECT ON Airplanes TO ' + @pilotUsername;
  63.     EXEC @grant_query;
  64.     SET @grant_query = 'GRANT SELECT ON FLIGHTS TO ' + @pilotUsername;
  65.     EXEC @grant_query;
  66.     SET @grant_query = 'GRANT SELECT(pilotid, lastname, firstname, address, experience) ON Pilots TO ' + @pilotUsername;
  67.     EXEC @grant_query;
  68.  
  69.     SET @view_query =
  70.     'CREATE VIEW ' + @pilotUsername + '_SalaryView AS
  71.     SELECT salary
  72.     FROM Pilots
  73.     WHERE pilotid = ' + @pilotID;
  74.     EXEC @view_query;
  75.  
  76.     SET @grant_query = 'GRANT SELECT ON ' + @pilotUsername + '_SalaryView' + ' TO ' + @pilotUsername;
  77.     EXEC @grant_query;
  78.    
  79.     SET @view_query =
  80.     'CREATE VIEW ' + @pilotUsername + '_FlightsView AS
  81.     SELECT departureTime, arrivalTime
  82.     FROM Flights
  83.     WHERE pilotid = ' + @pilotID;
  84.     EXEC @view_query;
  85.  
  86.     SET @grant_query = 'GRANT UPDATE ON ' + @pilotUsername + 'FlightsView' + ' TO ' + @pilotUsername;
  87.     EXEC @grant_query;
  88. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement