--------------------------------------------------------
--답변형 게시판 응용 프로그램
--작성날짜 : 2009년 10월 14일
--작성자 : 김용원
--------------------------------------------------------
-- 답변형 게시판(Reply)용 테이블 설계
Create Table dbo.Reply
(
Num Int Identity(1,1) Not Null Primary Key, --번호
Name VarChar(25) Not Null, --이름
Email VarChar(100) Null, --이메일
Title VarChar(150) Not Null, --제목
PostDate DateTime Default GetDate() Not Null, --작성일
PostIP VarChar(15) Not Null, --작성IP
Content Text Not Null, -- 내용
Password VarChar(20) Not Null, --비밀번호
ReadCount Int Default 0, --조회수
Encoding VarChar(10) Not Null, --인코딩(text/html)
Homepage VarChar(100) Null, --홈페이지
ModifyDate DateTime Null, --수정일
ModifyIP VarChar(15) Null, --수정IP
---------------------------
Ref Int Not Null, --참조(부모글)
Step Int Default 0, --답변길이(레벨)
RefOrder Int Default 0 --답변순서
)
Go
--글을 작성하는 저장 프로시저 만들기 : WriteReply
Create Proc dbo.WriteReply
@Name VarChar(25),
@Email VarChar(100),
@Title VarChar(150),
@PostIP VarChar(15),
@Content Text,
@Password VarChar(20),
@Encoding Varchar(10),
@Homepage VarChar(100)
AS
Declare @MaxRef Int
Select @MaxRef = Max(Ref) From Reply
If @MaxRef is Null
Set @MaxRef = 1
Else
Set @MaxRef = @MaxRef + 1
Insert Reply
(
Name, Email, Title, PostIP, Content,
Password, Encoding, Homepage, Ref
)
Values
(
@Name, @Email, @Title, @PostIP, @Content,
@Password, @Encoding, @Homepage, @MaxRef
)
Go
--데이터를 읽어오는 저장 프로시저 : ListReply
Create Procedure dbo.ListReply
As
Select *From Reply Order By Ref Desc, RefOrder Asc
Go
--해당 글을 세부적으로 읽어오는 저장 프로시저 만들기 : ViewReply
Create Procedure dbo.ViewReply
@Num Int
As
Update Reply Set ReadCount = ReadCount + 1
Where Num = @Num
Select *From Reply Where Num = @Num
Go
--해당 글을 수정하는 저장 프로시저 : ModifyReply
Create Procedure dbo.ModifyReply
@Name VarChar(25),
@Email VarChar(100),
@Title Varchar(150),
@ModifyIP VarChar(15),
@Content Text,
@Encoding Varchar(10),
@Homepage VarChar(100),
@Password VarChar(20),
@Num Int
As
Declare @cnt Int
Select @cnt = COUNT(*) From Reply
Where Num = @Num And Password = @Password
If @cnt > 0 --넘겨준 번호와 암호가 맞는 데이터가 있다면
Update Reply
Set
Name = @Name, Email=@Email,
Title = @Title, ModifyIP = @ModifyIP,
ModifyDate = Getdate(), Content = @Content,
Encoding = @Encoding, Homepage = @Homepage
Where Num = @Num And Password = @Password
Else
Return -1 --암호가 틀리면 -1을 반환하자
Go
--해당 글을 지우는 저장 프로시저 : DeleteReply
Create Procedure dbo.DeleteReply
@Password VarChar(20),
@Num Int
As
Declare @cnt Int
--암호와 번호가 맞으면 1을 반환
Select @cnt = COUNT(*) From Reply
Where Num = @Num And Password = @Password
If @cnt > 0
Delete Reply Where Num = @Num And Password = @Password
Else
Return -1 --삭제가 되지 않으면 -1반환
Go
--검색 저장 프로시저 : 정적 쿼리문
Create Procedure dbo.SearchReply
@SearchField VarChar(25),
@SearchQuery VarChar(25)
As
Set @SearchQuery = '%' + @SearchQuery + '%'
Select * From Reply
Where
(
Case @SearchField
When 'Name' Then Name
When 'Content' Then Content
When 'Title' Then Title
Else
@SearchQuery
End
)
Like @SearchQuery Order By Num Desc
Go
--글을 답변하는 저장 프로시저 : ReplyReply
Create Procedure dbo.ReplyReply
@Name VarChar(25),
@Email VarChar(100),
@Title VarChar(150),
@PostIP VarChar(15),
@Content Text,
@Password VarChar(20),
@Encoding VarChar(10),
@Homepage VarChar(100),
@ParentNumber Int --부모글의 Number 필드값 받기 -> Ref, Step, RefOrder
As
Declare @ParentRef Int
Declare @ParentStep Int
Declare @ParentRefOrder Int
Select @ParentRef = Ref, @ParentStep = Step,
@ParentRefOrder = RefOrder From Reply
Where Num = @ParentNumber
Begin Tran
--비집고 들어갈 자리 만들기
Update Reply
Set RefOrder = RefOrder + 1
Where
Ref = @ParentRef
And
RefOrder > @ParentRefOrder
-- 부모글보다 Step을 1증가, 보여지는 순서도 1증가
Insert Reply
(
Name, Email, Title, PostIP, Content, Password,
Encoding, Homepage, Ref, Step, RefOrder
)
Values
(
@Name, @Email, @Title, @PostIP, @Content, @Password,
@Encoding, @Homepage, @ParentRef, @ParentStep + 1, @ParentRefOrder + 1
)
Commit Tran
Go |