use tempdb
--[1] 테이블생성
Create Table dbo.Products
(
ProductID Int Identity(1,1) Primary Key,
ModelName VarChar(25) Null,
SellPrice Int Null
)
--[2] 예시문 입력
Insert Into Products Values
('좋은책',5000),
('준철책',6000),
('세창책',7000),
('용원책',8000),
('좋은책',9000)
--[3]상품의 가격을 2배로 업데이트, 업데이트된 레코드의 개수를 반환
Create Proc UpdateSellPrice
@ProductID Int,
@RecordCount Int Output --결과값 리턴
As
Update Products Set SellPrice = SellPrice * 2 Where
ProductID = @ProductID
Go
Exec UpdateSellPrice 1,1
Go
Declare @RecordCount Int
Exec UpdateSellPrice 1, @RecordCount OutPut
Select @RecordCount
Go
--[4]Products 테이블에 있는 모든 레코드의 개수 반환
--1. 일반적인 방식
Select Count(*) From Products;
Create Proc GetProductCount
As
Select COUNT(*) From Products;
Go
-- 2. Output 키워드 사용하여 결과값을 변수로 담아오는 방식
Execute GetProductCount --결과값을 레코드셋, 스칼라값(집계함수)
Go
Create Proc GetProductCountUp
@RecordCount Int Output
As
Select @RecordCount = Count(*) From Products;
Go
Delclare @RecordCount Int
Exec GetProductCountUp @RecordCount Output
Select @RecordCount
Go
--[5]-3. 상품의 가격을 반값으로 조정한 후 영향 받은 레코드 수 반환(Return)
--Return 사용하여 레코드 개수 방환 하는 방식
Create Proc UpdateSellPrice
@ProductID Int
As
Update Products Set SellPrice = SellPrice / 2 Where ProductID > @ProductID
--Select @@RowCount
Return @@RowCount --함수 자체를 리턴해주고 싶을때
Go
Declare @RecordCount Int
Exec @RecoudCount = UpdateSellPriceHalf 1
Select @RecordCount |