2013-10-03 47 views
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,[email protected]_ageLow,@d) 
    @v_dateHigh = DATEADD(year,[email protected]_ageHigh,@d) 

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

存儲過程的語法因DBMS而異,因此請通過添加相應的標記來指定使用哪個DBMS。 –

+0

T-sql,SQL SERVER 2008 R2 – isl65

回答

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