CREATE PROCEDURE pr_test
@empName VARCHAR(10)
AS
DECLARE @hireDate DATE
SELECT @hireDate = hiredate
FROM employee
WHERE empName = @empName
IF (@hireDate >='2000/01/01')
BEGIN
PRINT '2000년 이후 입사자'
END
ELSE
BEGIN
PRINT '2000년 전 입사자'
END
EXEC pr_test '김장비';
위와같이
PROCEDURE
를 통해 SQL 프로그래밍이 가능한다.
CREATE PROC pr_test2
@empName VARCHAR(10)
AS
DECLARE @salary INT
declare @salgrade INT
SELECT @salary = salary
FROM employee
WHERE empName = @empName;
SET @salgrade =
CASE
WHEN (@salary >200 and @salary <= 300) THEN 5
WHEN (@salary >300 and @salary <= 400) THEN 4
WHEN (@salary >400 and @salary <= 500) THEN 3
WHEN (@salary >500 and @salary <= 900) THEN 2
WHEN (@salary >900 and @salary <= 1000) THEN 1
END;
PRINT @empName + '의 급여등급은 ' + CAST(@salGrade AS VARCHAR(2))+'등급'
EXEC pr_test2 '신동수';
select * from salGrade;
select * from employee;
CREATE PROC pr_return
@empName VARCHAR(10)
AS
DECLARE @empNo INT
SELECT @empNo = empNo
FROM employee
WHERE empName = @empName;
IF( @empNo <> '')
RETURN 0;
ELSE
RETURN -1; -- empNo가 없을 경우
DECLARE @returnVal INT;
EXEC @returnVal = pr_return '신장수';
SELECT @returnVal;
employee
에신장수
가 존재하지 않으므로-1
이 출력된다.
강의: ms sql 2014 제대로 배우기 2