Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 1.93 KB | None | 0 0
  1. -- 1
  2. drop function fun;
  3. GO
  4. Create function fun(@id int) returns int
  5. BEGIN
  6. return (SELECT count(*) from employees e where e.department_id=@id)
  7. END
  8. GO
  9. --Wywołanie
  10. SELECT d.department_id , dbo.fun(d.department_id) as ilosc_pracownikow
  11. FROM departments d;
  12.  
  13. -- 2 tego nie jestem pewien
  14. CREATE PROCEDURE podwyzka()
  15. AS
  16. BEGIN
  17. Update Employees e set e.salary= e.salary + j.max_salary*0.10 from jobs j where e.job_id=j.job_id
  18. END
  19. GO
  20. Exec podwyzka();
  21. GO
  22. Drop Procedure podwyzka
  23. GO
  24.  
  25. -- 3
  26. CREATE PROCEDURE  zwiększenie_płacy_dla_stanowiska( @nazwa char, @wartosc float)
  27. AS
  28. BEGIN
  29. UPDATE jobs j set j.max_salary = max_salary + @wartosc where j.job_title = @nazwa;
  30. END
  31. GO
  32. exec zwiększenie_płacy_dla_stanowiska('jakas nazwa', 20);
  33. GO
  34. drop procedure zwiększenie_płacy_dla_stanowiska
  35. GO
  36.  
  37. -- 4 jezeli dobrze zrozumialem to cos takiego
  38. declare @licznik int;
  39. set @licznik=0;
  40. declare last_name varchar(25);
  41. declare kursor cursor for Select e.LAST_NAM FROM employees e where e.salary> (select e1.salary from employees e1 where e1.last_name = 'Kowalski';
  42. BEGIN
  43. open kursor;
  44. fetch next from kursor into @last_name;
  45. WHILE(@@FETCH_STATUS=0)
  46. BEGIN
  47. print 'Wiecej od Kowaslkiego zarabia ' + @last_name
  48. set @licznik=@licznik+1;
  49. END
  50. print('Wiecej zarabialo ' + cast(@licznik as varchar) + ' pracownikow');
  51. END
  52. close kursor;
  53. deallocate kursor;
  54.  
  55. -- 5
  56. GO
  57. Create table historia_zmian_prac ( data date, nr_akt int IDENTITY(1,1), stara_placa float,nowa placa float);
  58. GO
  59. create trigger historia_plac on employees
  60. AFTER UPDATE
  61. AS
  62. BEGIN
  63. DECLARE @old_salary float;
  64. DECLARE @new_salary float;
  65. select @old_salary = delted.salary form deleted;
  66. select @new_salary = inserted.salary from inserted;
  67. Insert into historia_zmian_prac (data,stara_placa,nowa_placa) VALUES (getDate(),@old_salary, @new_salary);
  68. END
  69. GO
  70. -- test
  71. UPDATE employees set salary=20 where employee_id=2;
  72. Select * from historia_zmian_prac;
  73. GO
  74. Drop trigger wyzwalacz;
  75. GO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement