我需要编写年龄为两人的程序,并从表格中选择年龄内所有人的名字和姓氏
此代码是否有效?
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
答案 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