SQL Server:alter stored procedure接受参数

时间:2016-10-29 16:12:22

标签: sql-server stored-procedures sqldatatypes

Employee

Employee Table

编写一条SQL语句,该语句将改变下面提到的预先存在的存储过程以完成以下操作:

  • 允许提供包含经理Emp_ID
  • 的参数
  • 使用提供的参数来限定查询中的Emp_ID
  • 如果未提供参数值,则将参数赋值为零
  • 将结果语句重写为包含管理员姓氏的JOIN
  • 确保结果显示如下:

    Manager's Last Name
    Employee's Last Name
    Employees Hire Date
    
  • 按经理的姓氏,最近的雇员和雇员的姓氏订购查询结果

这是预先存在的存储过程代码:

CREATE PROCEDURE dbo.sp_GetEmployeeNm 
AS
   SELECT  
       Last_Nm, Hire_Dt
   FROM 
       dbo.Employee
   WHERE 
       Mgr_ID IN (SELECT Emp_ID 
                  FROM dbo.Employee 
                  WHERE Emp_ID = 12345)

有没有人知道如何正确更改此存储过程?

这就是我的尝试:

ALTER PROCEDURE dbo.sp_GetEmployeeNm (@Emp_ID INT = 0) 
AS 
BEGIN
    SELECT 
        m.Last_Nm AS Manager's Last Name, 
        e.Last_Nm AS Employee's Last Name, 
        e.Hire_Dt AS Employee's Hire Date
    FROM
        Employee  e
    INNER JOIN 
        Employee m ON e.Emp_ID = m.Mgr_ID
    WHERE 
        e.Emp_ID = 12345
    ORDER BY 
        (m.Last_Nm, e.Hire_Dt, e.Last_Nm) DESC
END

1 个答案:

答案 0 :(得分:3)

ALTER PROCEDURE dbo.sp_GetEmployeeNm 
    @Emp_ID INT = 0 
AS 
BEGIN
    SELECT 
        m.Last_Nm AS 'Manager''s Last Name', 
        e.Last_Nm AS 'Employee''s Last Name', 
        e.Hire_Dt AS 'Employee''s Hire Date'
    FROM 
        Employee  e
    INNER JOIN 
        Employee m ON e.Emp_ID = m.Mgr_ID
    WHERE 
        e.Emp_ID = @Emp_ID
    ORDER BY 
        (m.Last_Nm, e.Hire_Dt, e.Last_Nm) DESC
END
GO

使用

EXEC dbo.sp_GetEmployeeNm 12345