CREATE TABLE dbo.Notice
(
ID INT IDENTITY(1, 1) NOT NULL PRIMARY KEY -- ID
,[Name] NVARCHAR(25) NOT NULL -- 작성자명
,MailAddress NVARCHAR(100) NULL -- 메일 주소
,Title NVARCHAR(150) NOT NULL -- 제목
,WriteDate DATETIME DEFAULT GETDATE() NOT NULL -- 작성일
,WriteIP NVARCHAR(15) NULL -- 작성 IP
,Content NTEXT NOT NULL -- 내용
,[Password] NVARCHAR(20) NULL -- 패스워드
,ReadCount INT DEFAULT 0 -- 조회 수
,[Encoding] NVARCHAR(10) NOT NULL -- 인코딩(HTML/TEXT)
,Homepage NVARCHAR(100) NULL -- 홈페이지
,UpdateDate DATETIME NULL -- 수정일
,UpdateIP NVARCHAR(15) NULL -- 수정 IP
,[FileName] NVARCHAR(255) NULL -- 파일명
,FileSize INT DEFAULT 0 -- 파일 크기
,DownloadCount INT DEFAULT 0 -- 다운로드 수
,ReferenceID INT NOT NULL -- 참조 ID
,ReplyLevel INT DEFAULT 0 -- 답변 레벨
,ReplyOrder INT DEFAULT 0 -- 답변 순서
,ReplyCount INT DEFAULT 0 -- 답변 수
,ParentID INT DEFAULT 0 -- 부모 ID
,CommentCount INT DEFAULT 0 -- 댓글 수
,Category NVARCHAR(50) Default('FREE') Null -- 카테고리
)
GO
CREATE TABLE dbo.NoticeComment
(
ID INT IDENTITY(1, 1) NOT NULL PRIMARY KEY -- ID
,NoticeID INT NOT NULL -- 게시판 ID
,[Name] NVARCHAR(25) NOT NULL -- 작성자명
,Comment NVARCHAR(4000) NOT NULL -- 내용
,WriteDate SMALLDATETIME DEFAULT(GETDATE()) -- 작성일
,[Password] NVARCHAR(20) NOT NULL -- 패스워드
)
GO
CREATE PROCEDURE dbo.WriteNotice
@Name NVARCHAR(25)
,@MailAddress NVARCHAR(100)
,@Title NVARCHAR(150)
,@WriteIP NVARCHAR(15)
,@Content NTEXT
,@Password NVARCHAR(20)
,@Encoding NVARCHAR(10)
,@Homepage NVARCHAR(100)
,@FileName NVARCHAR(255)
,@FileSize INT
AS
DECLARE @MaximumReferenceID INT;
SELECT @MaximumReferenceID = MAX(ReferenceID) FROM dbo.Notice;
IF @MaximumReferenceID IS NULL
BEGIN
SET @MaximumReferenceID = 1;
END
ELSE
BEGIN
SET @MaximumReferenceID = @MaximumReferenceID + 1;
END
INSERT INTO dbo.Notice
(
[Name]
,MailAddress
,Title
,WriteIP
,Content
,[Password]
,[Encoding]
,Homepage
,[ReferenceID]
,[FileName]
,[FileSize]
)
Values
(
@Name
,@MailAddress
,@Title
,@WriteIP
,@Content
,@Password
,@Encoding
,@Homepage
,@MaximumReferenceID
,@FileName
,@FileSize
);
GO
CREATE PROCEDURE dbo.ListNotice
@Page INT
AS
WITH OrderedNoticeBoard
AS
(
SELECT
ID
,[Name]
,MailAddress
,Title
,WriteDate
,ReadCount
,ReferenceID
,ReplyLevel
,ReplyOrder
,ReplyCount
,ParentID
,CommentCount
,[FileName]
,FileSize
,DownloadCount
,ROW_NUMBER() OVER (ORDER BY ReferenceID DESC, ReplyOrder ASC) AS 'RowNumber'
FROM dbo.Notice
)
SELECT *
FROM OrderedNoticeBoard
WHERE RowNumber BETWEEN @Page * 10 + 1 AND (@Page + 1) * 10;
GO
CREATE PROCEDURE dbo.ViewNotice
@ID Int
As
UPDATE dbo.Notice
SET ReadCount = ReadCount + 1
WHERE ID = @ID;
SELECT *
FROM dbo.Notice
WHERE ID = @ID;
GO
CREATE PROCEDURE dbo.ReplyNotice
@Name NVARCHAR(25)
,@MailAddress NVARCHAR(100)
,@Title NVARCHAR(150)
,@WriteIP NVARCHAR(15)
,@Content NTEXT
,@Password NVARCHAR(20)
,@Encoding NVARCHAR(10)
,@Homepage NVARCHAR(100)
,@ParentID INT
,@FileName NVARCHAR(255)
,@FileSize INT
AS
DECLARE @MaximumReplyOrder INT;
DECLARE @MaximumReplyCount INT;
DECLARE @ParentReferenceID INT;
DECLARE @ParentReplyLevel INT;
DECLARE @ParentReplyOrder INT;
UPDATE dbo.Notice
Set ReplyCount = ReplyCount + 1
WHERE ID = @ParentID;
SELECT
@MaximumReplyOrder = ReplyOrder
,@MaximumReplyCount = ReplyCount
FROM dbo.Notice
WHERE ParentID = @ParentID
AND ReplyOrder = (SELECT MAX(ReplyOrder) FROM dbo.Notice WHERE ParentID = @ParentID);
IF @MaximumReplyOrder IS NULL
BEGIN
SELECT @MaximumReplyOrder = ReplyOrder
FROM dbo.Notice
WHERE ID = @ParentID;
SET @MaximumReplyCount = 0;
END
SELECT
@ParentReferenceID = ReferenceID
,@ParentReplyLevel = ReplyLevel
FROM dbo.Notice
WHERE ID = @ParentID;
UPDATE dbo.Notice
SET ReplyOrder = ReplyOrder + 1
WHERE ReferenceID = @ParentReferenceID And ReplyOrder > (@MaximumReplyOrder + @MaximumReplyCount);
INSERT INTO dbo.Notice
(
[Name]
,MailAddress
,Title
,WriteIP
,Content
,[Password]
,[Encoding]
,Homepage
,ReferenceID
,ReplyLevel
,ReplyOrder
,ParentID
,[FileName]
,FileSize
)
VALUES
(
@Name
,@MailAddress
,@Title
,@WriteIP
,@Content
,@Password
,@Encoding
,@Homepage
,@ParentReferenceID
,@ParentReplyLevel + 1
,@MaximumReplyOrder + @MaximumReplyCount + 1
,@ParentID
,@FileName
,@FileSize
);
GO
CREATE PROCEDURE dbo.GetNoticeCount
As
Select Count(*) From dbo.Notice;
GO
CREATE PROCEDURE dbo.SearchNoticeCount
@SearchField NVARCHAR(25)
,@SearchQuery NVARCHAR(25)
AS
SET @SearchQuery = '%' + @SearchQuery + '%';
SELECT COUNT(*)
FROM dbo.Notice
WHERE
(
CASE @SearchField
WHEN 'Name' THEN [Name]
WHEN 'Title' THEN Title
WHEN 'Content' THEN Content
ELSE @SearchQuery
END
)
LIKE @SearchQuery;
GO
CREATE PROCEDURE dbo.DeleteNotice
@ID INT
,@Password NVARCHAR(30)
AS
DECLARE @Count INT;
SELECT @Count = COUNT(*)
FROM dbo.Notice
WHERE ID = @ID
AND [Password] = @Password;
IF @Count = 0
BEGIN
Return 0;
END
DECLARE @ReplyCount INT;
DECLARE @ReplyOrder INT;
DECLARE @ReferenceID INT;
DECLARE @ParentID INT;
SELECT
@ReplyCount = ReplyCount
,@ReplyOrder = ReplyOrder
,@ReferenceID = ReferenceID
,@ParentID = ParentID
FROM dbo.Notice
WHERE ID = @ID;
IF @ReplyCount = 0
BEGIN
IF @ReplyOrder > 0
BEGIN
UPDATE dbo.Notice
SET ReplyOrder = ReplyOrder - 1
WHERE ReferenceID = @ReferenceID
AND ReplyOrder > @ReplyOrder;
UPDATE Notice
SET ReplyCount = ReplyCount - 1
WHERE ID = @ParentID;
END
DELETE FROM dbo.Notice
WHERE ID = @ID;
DELETE FROM dbo.Notice
WHERE ID = @ParentID
AND UpdateIP = N'((DELETED))'
AND ReplyCount = 0;
END
ELSE
BEGIN
UPDATE dbo.Notice
SET
[Name] = N'(Unknown)',
MailAddress = '',
[Password] = '',
Title = N'(삭제된 글입니다.)',
Content = N'(삭제된 글입니다. 현재 답변이 포함되어 있기 때문에 내용만 삭제되었습니다.)',
UpdateIP = N'((DELETED))',
[FileName] = '',
FileSize = 0,
CommentCount = 0
WHERE ID = @ID;
END
GO
CREATE PROCEDURE dbo.UpdateNotice
@Name NVARCHAR(25)
,@MailAddress NVARCHAR(100)
,@Title NVARCHAR(150)
,@UpdateIP NVARCHAR(15)
,@Content NTEXT
,@Password NVARCHAR(30)
,@Encoding NVARCHAR(10)
,@Homepage NVARCHAR(100)
,@FileName NVARCHAR(255)
,@FileSize INT
,@ID INT
AS
DECLARE @Count INT
SELECT @Count = Count(*)
FROM dbo.Notice
WHERE ID = @ID
AND [Password] = @Password;
IF @Count > 0
BEGIN
UPDATE dbo.Notice
SET
[Name] = @Name
,MailAddress = @MailAddress
,Title = @Title
,UpdateIP = @UpdateIP
,UpdateDate = GETDATE()
,Content = @Content
,[Encoding] = @Encoding
,Homepage = @Homepage
,[FileName] = @FileName
,FileSize = @FileSize
Where ID = @ID;
SELECT '1';
END
ELSE
SELECT '0';
GO
CREATE PROCEDURE dbo.SearchNotice
@Page INT
,@SearchField NVARCHAR(25)
,@SearchQuery NVARCHAR(25)
AS
WITH OrderedNoticeBoard
AS
(
SELECT
ID
,[Name]
,MailAddress
,Title
,WriteDate
,ReadCount
,ReferenceID
,ReplyLevel
,ReplyOrder
,ReplyCount
,ParentID
,CommentCount
,[FileName]
,FileSize
,DownloadCount
,ROW_NUMBER() OVER (ORDER BY ReferenceID DESC, ReplyOrder ASC) AS 'RowNumber'
FROM dbo.Notice
WHERE
(
CASE @SearchField
WHEN 'Name' THEN [Name]
WHEN 'Title' THEN Title
WHEN 'Content' THEN Content
ELSE @SearchQuery
END
)
LIKE '%' + @SearchQuery + '%'
)
SELECT
ID
,[Name]
,MailAddress
,Title
,WriteDate
,ReadCount
,ReferenceID
,ReplyLevel
,ReplyOrder
,ReplyCount
,ParentID
,CommentCount
,[FileName]
,FileSize
,DownloadCount
,RowNumber
FROM OrderedNoticeBoard
WHERE RowNumber BETWEEN @Page * 10 + 1 AND (@Page + 1) * 10
ORDER BY ID DESC;
GO