程序按年龄获取人名

时间:2013-10-03 10:20:29

标签: sql-server

我需要编写年龄为两人的程序,并从表格中选择年龄内所有人的名字和姓氏

此代码是否有效?

CREATE PROCEDURE dbo.getEmployeeRange
@v_ageLow int,
@v_ageHigh int

AS
BEGIN
DECLARE
     @v_dateLow date,
     @v_dateHigh date
SET @d = CURDATE()

--convert age to date 

    @v_dateLow = DATEADD(year,-@v_ageLow,@d)
    @v_dateHigh = DATEADD(year,-@v_ageHigh,@d) 

SELECT firstName,lastName FROM employee
 WHERE birthDate BETWEEN @v_dateLow and @v_dateHigh

1 个答案:

答案 0 :(得分:0)

您可以使用以下脚本;

CREATE PROCEDURE dbo.getEmployeeRange
@v_ageLow INT,
@v_ageHigh INT
AS
BEGIN
SELECT firstName,
       lastName
FROM   employee
WHERE  birthDate BETWEEN DATEADD(YEAR, -1 * @v_ageLow, GETDATE()) AND 
           DATEADD(YEAR, -1 * @v_ageHigh, GETDATE())
END