You are on page 1of 1

Compte-Rendu TP3

Exercice1 :
create procedure gettotalsalary
@ManagerID int
as
begin
with employeehierarchy as(
select EmployeeID,ManagerID,Salary
from Employee
where ManagerID=@ManagerID

union all

select e.EmployeeID,e.ManagerID,e.Salary
from employeehierarchy eh inner join Employee e
on eh.EmployeeID=e.ManagerID)
select sum(Salary) as Totalsalary
from employeehierarchy
end
-----------------------------------------
EXEC gettotalsalary @ManagerID = 1

Exercice2 :
CREATE PROCEDURE GetOrdersByColumn
@ColumnName NVARCHAR(50)
as
begin
if not exists (select * from INFORMATION_SCHEMA.columns where COLUMN_NAME=@ColumnName)
begin
print 'La colonne spécifiée non existante.';
return;
end
DECLARE @sql NVARCHAR(MAX);
SET @sql = 'SELECT TotalAmount FROM Orders ORDER BY ' + QUOTENAME(@ColumnName);
EXEC sp_executesql @sql;
END
-------------------------------------------
EXEC GetOrdersByColumn 'customerID';

You might also like