0

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

这段代码会起作用吗?

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
4

1 回答 1

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
于 2013-10-03T10:34:34.530 回答