제목 : 15.1. 저장 프로시저 사용 예제 : 카테고리 테이블
    
    
        
            
                | 
                    글번호:
                 | 
                
                 | 
                
                    38
                 | 
            
            
                | 
                    작성자:
                 | 
                
                 | 
                
                    
                        레드플러스
                        
                        
                    
                 | 
            
            
                | 
                    작성일:
                 | 
                
                 | 
                
                    
                        2005/12/27 오전 11:19:00 
                    
                 | 
            
            
            
                | 
                    조회수:
                 | 
                
                 | 
                
                    
                        12528
                    
                 | 
            
            
        
     
 
    
	
	
    
	--카테고리 응용 프로그램 설계
--[0] 테이블 설계 : 프린트물 참고
Create Table dbo.Categories
(
   CategoryID Int Identity(1, 1) Primary Key,
   CategoryName VarChar(50),
   SuperCategory Int Null,
   Align SmallInt Default(0)
)
Go
--[1] 입력
Insert Categories Values('컴퓨터', NULL, 0)
Insert Categories Values('노트북', 1, 1)
Insert Categories Values('핸드폰', NULL, 2)
Insert Categories Values('신규', 3, 3)
--[2] 출력
Select CategoryName
From Categories
Order By Align Asc
--[3] 상세
Select * From Categories Where CategoryID = 1
--[4] 수정
Begin Transaction
    Update Categories 
    Set
        CategoryName = '콤퓨타'
    Where CategoryID = 1
--RollBack Tran
Commit Tran
--[5] 삭제
Delete Categories Where CategoryID = 2
--[6] 검색
Select * From Categories
Where 
    CategoryName Like '%퓨%'
    And 
    SuperCategory Is NULL --널(NULL)값 비교
--[!] 테이블 삭제
Drop Table dbo.Categories
Go
--[7] 뷰(View) 생성 : Select문 전용
--[a] 대분류 데이터를 출력하는 구문을 줄여서 출력
Select CategoryID, CategoryName 
From Categories 
Where SuperCategory Is NULL
--Order By CategoryName Asc
--[b] 위 구문을 줄여주는 뷰(View) 생성
Create View dbo.TopCategory
As
    Select CategoryID, CategoryName 
    From Categories 
    Where SuperCategory Is NULL
--    Order By CategoryName Asc
Go
--[c] 뷰(가상 테이블) 사용 : 약간 줄어들죠???
Select * From TopCategory
Order By CategoryName Asc
--[d] 뷰(가상 테이블) 수정 : 암호화
sp_helptext TopCategory --뷰 구문 보기
Alter View dbo.TopCategory
With Encryption -- 개체 암호화 옵션
As
    Select CategoryID, CategoryName 
    From Categories 
    Where SuperCategory Is NULL
--    Order By CategoryName Asc
Go
sp_helptext TopCategory --안 보임
--[e] 뷰 삭제
Drop View dbo.TopCategory
Go
--[8] 저장 프로시저 생성 : 모든 명령어
--[a] 저장 프로시저 생성
Create Procedure dbo.GetCategoryList
As
    Select CategoryID, CategoryName 
    From Categories 
    Where SuperCategory Is NULL
    Order By CategoryName Asc
Go
--[b] 저장 프로시저 실행
Execute GetCategoryList --기본
Exec GetCategoryList     --단축
GetCategoryList     --이것도 가능
--[c] 저장 프로시저 수정
sp_helptext GetCategoryList --암호화 전
Alter Proc dbo.GetCategoryList
With Encryption
As
    Select CategoryID, CategoryName 
    From Categories 
    Where SuperCategory Is NULL
    Order By CategoryName Asc
Go
sp_helptext GetCategoryList --암호화 후
--[d] 저장 프로시저 삭제
Drop Proc dbo.GetCategoryList
Go