SELECT * FROM titles
DECLARE curs_1author_titles CURSOR
-- 변수선언 curs_1author_titles 커서명령어
-- LOCAL -- 지역 커서로 지정하기 위해 필수적
-- STATIC -- 정확한 @@CURSOR_ROWS결과를 위해
FOR
SELECT title_id
FROM titleauthor
-- titleauthor 테이블의 title_id를 보여달라
WHERE royaltyper = 100
ORDER BY title_id
-- 그리고 royaltyper = 100인값의 title_id 목록을 보여달라
DECLARE @title_id varchar(6), @subtotal int, @price int, @qtyy int
-- @title_id, @subtotal, @price 변수선언
OPEN curs_1author_titles
-- curs_1author_titles 를 열어라
FETCH NEXT FROM curs_1author_titles INTO @title_id
--curs_1author_titles 커서에서 title_id의 값을 가져달라
WHILE(@@FETCH_STATUS=0)
-- FETCH STATUS가 0인동안 데이터를 계속 돌려라
BEGIN
-- while문 실행점
SELECT @subtotal = ISNULL(ISNULL(t.price,0)*ISNULL(s.qty,0),0),
-- t.price, q.qty Null값이 있으면 0을 넣어라.
-- 그리고 t.price * q.qty의 값을 @subtotal 정의해라
@price = t.price,
@qtyy = s.qty
-- subtotal, price, qtyy의 값들을 @subtotal,price, qtyy에 넣어라
FROM titles AS t
JOIN sales AS s
ON t.title_id = s.title_id
-- INNER 조인해줘라.
WHERE t.title_id = @title_id
PRINT '단일 저자 책(' + @title_id + ')판매 금액: '
+ CONVERT(varchar(10), @price*@qtyy) + ', 서브토탈: ' +
CONVERT(varchar(20), @subtotal)
/*
화면에 표시해라 단일저자책 + title_id + 판매금액 +
varchar(10)형으로 @price*@qtyy값 + 서브토탈 +
varchar(20)형으로 @subtotal값
*/
FETCH NEXT FROM curs_1author_titles INTO @title_id
-- while 문이 끝날때는 한번더 써줘야함
-- 지금은 19번째줄에 써져있음
END
-- while문 종료
close curs_1author_titles
-- 커서 닫아주기
DEALLOCATE curs_1author_titles
-- 커서를 메모리에서 해제