블로그 이미지
Magic_kit
study 관련자료를 한곳으로 자기 개발 목적으로 재태크 재무 관리 목적으로 일상생활의 팁을 공유 하기 위하여 블로그를 개설 하였습니다.

calendar

1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
반응형

Category

Recent Post

Recent Comment

Archive

2009. 9. 23. 18:45 .Net Project/WindowServer2008
반응형

찜(WishList) : 회원전용

 



-- 상품 등록 저장 프로시저 작성

 --[1]상품등록 : ProductAdd.aspx에서 사용
--ProductsAdd : Products 테이블에 데이터 이입력 후
--현재 입력된 레코드의 ProductID값을 반환

use Market 
Create Procedure dbo.ProductsAdd
                @CategoryID Int,
                @ModelNumber VarChar(50),
                @ModelName VarChar(50),
                @Company VarChar(50),
                @OriginPrice Int,
                @SellPrice Int,
                @EventName VarChar(50),
                @ProductImage VarChar(50),
                @Explain VarChar(400), --Text
                @Description VarChar(8000),
                @Encoding VarChar(10),
                @ProductCount Int,
                @Mileage Int,
                @Absence Int, 
                @ProductID Int OUTPUT --Output 매겨변수
As

 Insert Products ( CategoryID, ModelNumber,  ModelName,
        Company, OriginPrice, SellPrice,
        EventName, ProductImage, Explain,
        Description, Encoding, ProductCount,
        Mileage, Absence
  )
  Values ( @CategoryID, @ModelNumber, @ModelName,
        @Company, @OriginPrice, @SellPrice,
        @EventName, @ProductImage,  @Explain,
        @Description, @Encoding, @ProductCount,
        @Mileage, @Absence
  )

--output 매개변수인 ProductID에 마지막 Identity값 반환
  Select @ProductID = @@IDENTITY
  Go

Declare @ProductID Int
Exec ProductsAdd 1, 'COM-01','최고컴퓨터','우리집',3000,2000,'HIT',
'COM-01.jpg','i7 CPU 사용',  '어쩌구 저쩌구 ~^^;;', 'Text', 1000,0,0,
@ProductID Output
 
Select @ProductID 


-- 상품 카테고리 리스트 저장 프로시저 작성

 
--[2] 상품 카테고리 리스트 : CategoryList.aspx 에서 사용
Create Procedure ProductCategoryList
As
   Select
          CategoryID,
          CategoryName
   From 
          Categories
   Order By 
    
   CategoryName Asc    
Go

--[!]테스트 : 모든 카테고리 리스트 출력

Execute ProductCategoryList
Go


--[3] 카테고리에 따른 상품리스트 : ProductsList.aspx에서 사용
Create Procedure ProductsByCategory
(
         @CategoryID Int
)    
As
    Select
            ProductID,
            ModelName,
            SellPrice,
            ProductImage
    From
            Products
    Where
           CategoryID = @CategoryID
    Order By 
   
    ModelName,
    ModelNumber   
Go


--[4] 상품 상세 설명
Create Procedure ProductDetail
(
          @ProductID Int,
          @ModelNumber VarChar(50) Output,
          @ModelName VarChar(50) Output,
          @Company VarChar(50) Output, 
          @ProductImage VarChar(50) Output,
          @OriginPrice Int Output,
          @SellPrice Int Output,
          @Description NVarChar(4000) Output,
          @ProductCount Int Output
)

As
 Select
      @ProductID = ProductID,
      @ModelNumber = ModelNumber,
      @ModelName = ModelName,
      @Company = Company,
      @ProductImage = ProductImage,
      @OriginPrice = originPrice,
      @SellPrice = SellPrice,
      @Description = --Description,
         CONVERT(NVarChar, Description),
      @ProductCount = ProductCount
 From
        Products 
 Where
        ProductID = @ProductID
Go

--전체 상품 리스트 관련 저장 프로시저 작성

 
--[!]전체 상품 리스트 : ProductPages.ascs에서 사용
create Proc dbo.GetProducts --SQL Server 2005 사용
         @Page Int -- 1페이지, 2페이지와 같이 보여줄 페이지 번호를 넘겨준다
As
 --아래 구문은 최근 제품에 대해서 페이징 처리를 해서 보여줌
 --인터넷을 활용해서 더 많은 성능향상 페이징 로직 검색 권장 

 
Select Top 10 *From Products --한페이지에 10개 레코드 출력 기본
  Where
        ProductID Not In
        (
          Select Top (10 * @Page) ProductID From Products
          Order By ProductID Desc
        )
        Order By ProductID Desc  
        Go 


--GetProducts 저장 프로시저를 동적 쿼리문으로 변경
Alter Proc dbo.GetProducts --SQL Server 200이상 공용 
       @Page Int
As
    Declare @strSql VarChar(1000)
    Set @strSql = ' 
    Select Top 10 * From Products
    Where 
             ProductID Not In
             (
                    Select Top ' + CAST((10 * + @Page) As VarChar)
                     + ' ProductID From Products Order By ProductID Desc)
                     Order By ProductID Desc
                 '
    Exec(@strSql)
Go


결론 : 동적쿼리문 사용하지 않고 정적 쿼리문 저장 프로시저 이용하며,
        동적 SQL200사용시 사용되며, 정적쿼리문은 2008,2005, 모두 허용 및 사용
        (위의 쿼리문(정적) 사용)


--상품평 관련 저장 프로시저 작성  

 
--[5]상품평 리스트 : ReviewList.ascx에서 사용
Create Procedure ReviewsList
(
         @ProductID Int
)
As
   Select 
       ReviewID,
       CustomerName,
       Rating,
       Comments,
       AddDate
   From
       Reviews
 Where
       ProductID = @ProductID
Go


--[6] 상품평추가 : ReviewList.ascx에서 사용
Create Procedure ReviewsAdd
(
        @ProductID Int,
        @CustomerName VarChar(50),
        @CustomerEmail VarChar(50),
        @Rating Int,
        @Comments VarChar(3850),
        @ReviewID Int Output
)  

As
 Insert Into Reviews
 (
      ProductID,
      CustomerName,
      CustomerEmail,
      Rating,
      Comments
 )
 Values
 (
     @ProductID,
     @CustomerName,
     @CustomerEmail,
     @Rating,
     @Comments 
 )
 
 Select @ReviewID = @@IDENTITY
Go


--[7] 쇼핑카트 아이템 추가하기 : AddToCart.aspx에서 사용
Create Procedure ShoppingCartAddItem
(
     @CartID VarChar(50), --누가 
     @ProductID Int,  --어떤제품을
     @Quantity Int --장바구니에 담는지 ??
)
As
 --(1) 내가 보고 있는 제품이 이미 담아 있는지 확인
 Declare @CountItems Int
 
 Select 
       @CountItems = COUNT(ProductID)
 From
       ShoppingCart
 Where
        ProductID = @ProductID --장바구니에 담으려는 제품
 And
       CartID = @CartID --현재 접속자 : 회원 또는 비회원
 
 --(2) 이미 해당 제품이 담겨져 있다면, 수정
 IF @CountItems > 0
  Update
         ShoppingCart
  Set
         Quantity = (@Quantity + ShoppingCart.Quantity)
  Where
         ProductID = @ProductID
  And
         CartID = @CartID 

 --[3] 처음 구입 하는 제품이라면, 입력
 Else      
  Insert Into ShoppingCart
  (
      CartID, 
      ProductID, 
      Quantity
  )   
     Values
     (
        @CartID,
        @ProductID,
        @Quantity
    )
Go









반응형
posted by Magic_kit