insert,update등을 위해 답변개수 맥스값에 해당하게 글을 올려야 한다는 단점이 있음
사이트 성격에 따라 사용가치가 많음..
목록 Select시 top N방식으로 가져오는 부분도 볼만함(대용량 Select) 단점은 정렬같은 기능을
자유자제로 넣기가 힘들다는 점.
바로 아래는 일반적인 Board테이블 설계 부분인데 개인적으로는 BoardCode 와 같은 게시판 구분을 두는 부분 잘못됐다고 생각함.
왜? 게시글을 나타내는 테이블이 Board여야 되고 하나의 게시글은 여러 범주에 속하지 특정
게시판에만 속하는 경우는 거의 없음.
Create TABLE [dbo].[Board] (
[Seq] [bigint] IDENTITY (1, 1) NOT NULL ,
[BoardCode] [int] NOT NULL ,
[Thread] [int] NOT NULL ,
[Depth] [int] NOT NULL ,
[Writer] [varchar] (20) NOT NULL ,
[PWD] [varchar] (20) NOT NULL ,
[Email] [varchar] (100),
[Title] [varchar] (100) NOT NULL ,
[Mode] [smallint],
[IP] [varchar] (15) ,
[Readcount] [int] ,
[Replycount] [int] ,
[Transdate] [smalldatetime] NOT NULL ,
[Content] [text]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
drop table Board
delete Board where seq in (22)
--Select * From Board Order By Thread Desc
declare @i int
set @i = 0
while @i < 70000
begin
set @i = @i + 1
exec Board_ISUD 1,'A',19,-1,'강희기','1111','watch1234@msn.com','1-2-2.',1,'192.168.0.21','동해물과 백두산이 마르고 닳도록 하느님이 보우하사 우리나라만세 무궁화 삼천리....'
end
--Board_ISUD '1','D',19
--Board_ISUD '1','U',5086,0,',',','4첫번째글입.',0,'192.168.0.21','내용있'지4'
ALTER Procedure Board_ISUD
@BoardCode Int, --게시판 코드(고객센터 게시판 1)
@Type Char(1), --타입(R:조회,A:입력,U:수정,D:삭제)
@Seq Int, --글번호(답글 시엔 Key:부모 @Seq 외엔 현재 @Seq)
@Depth Int , --답변위치(부모Depth) 입력 1000일경우 새글
@Writer Varchar(20)=', --글쓴이
@PWD Varchar(20)=', --비밀번호
@Email Varchar(300)=', --메일주소
@Title Varchar(100)=', --글제목
@Mode Int =', --글형식(0:text, 1:html,.... )
@IP Varchar (15)=', --(문자열) 아이피
@Content Text =' --내용
/*신규작성글의 Depth = 0 */
AS
SET NOCOUNT ON
Declare @xSql Varchar(1000)
Declare @MinReplayThread Int,@ParentThread Int, @MaxReplyCount Int, @calcRight Int,@DeleteMinThread Int
Set @MaxReplyCount = 20 -- 최대 답변수는 19개 까지 가능(게시판의성격에따라 조절)
Set @calcRight =1
Set @ParentThread = 0
SELECT @ParentThread=Thread FROM Board WHERE Seq = @Seq --부모글이 있는 답변입력의 경우 @Thread는 부모의 Seq
--현재 글의 쓰레드 에서 오른쪽 두자리를 잘라서 비교(Thread에 새글은 MaxReplay단위로 등록되고 답글은 -1씩 줄어듬)
IF (@ParentThread % @MaxReplyCount ) = 0
Set @MinReplayThread = @ParentThread - @MaxReplyCount
ELSE
Set @MinReplayThread = @ParentThread - (@ParentThread % @MaxReplyCount )
IF @Type = 'A' --입력
IF @Depth <> -1 --부모에 종속된 글 일경우(답변글)
BEGIN
Set @Depth = @Depth + 1 --부모Depth + 1 , 새글등록 시 -1
Update Board Set Thread = Thread - 1
Where Thread < @ParentThread And Thread > @MinReplayThread --부모의 답변글들 모두 1씩 감소
-- 부모 쓰레드보다 하나 작은 값으로 인서트
INSERT INTO Board(BoardCode,Thread, Depth, Writer, PWD, Email, Title, Mode, IP,Transdate, Content)
VALUES (@BoardCode,@ParentThread-1,@Depth,@Writer,@PWD,@Email,@Title, @Mode, @IP,getdate(), @Content)
END
ELSE
BEGIN
SELECT @ParentThread = IsNull(Max(Thread),0) + @MaxReplyCount From Board --새글일 경우 : 최고값 + @MaxReplyCount
IF @ParentThread = 0
Set @ParentThread = @MaxReplyCount
INSERT INTO Board (BoardCode,Thread,Depth, Writer, PWD, Email, Title, Mode, IP,Transdate, Content)
VALUES (@BoardCode, @ParentThread , 0 , @Writer,@PWD,@Email,@Title,@Mode,@IP,getdate(),@Content)
END
ELSE IF @Type='S'
BEGIN --상세조회
Update Board Set Readcount = Readcount + 1 Where Seq = @Seq
Select @ParentThread=Thread From Board Where Seq = @Seq
Select Seq,BoardCode,Thread,Depth,Writer,PWD,Email,Title,Mode,IP,Readcount,Replycount,Transdate,Content
From Board Where Seq = @Seq
END
ELSE IF @Type = 'U'
BEGIN --수정
Update Board Set Title = @Title, IP = @IP, Mode=@Mode, Content = @Content
Where Seq=@Seq And Writer = @Writer And PWD =@PWD
END
ELSE IF @Type = 'D'
BEGIN --삭제
-- 하위 답변글들의 유무를 체크하여 같이 지운다.(현재Seq의 Thread보다 작고 @MinReplayThread보다 큰 녀석들 중 Depth
-- 지워야할 답글의 최소 Thread를 찾는다.
Select Top 1 @DeleteMinThread =Thread From Board
Where Thread < @ParentThread And Depth <= @Depth Order By Depth Desc
Delete From Board Where Thread <= @ParentThread And Thread > @MinReplayThread
And Case When @DeleteMinThread Is Null Then 1 Else Thread End > Case When @DeleteMinThread Is Null Then 2 Else @DeleteMinThread End
END
select Max(Thread) from board
select top 1000 * from board
select count(0) from board
BoardPagingSelect 2000,20,0,0
--게시판 조회
--d
ALTER Procedure BoardPagingSelect
@PAGE INT, --페이지 번호
@PAGE_SIZE INT, --한 페이지의 레코드 수
@TOTAL_RECORDS INT OUTPUT, --리턴될 총 레코드 수
@REMAINDER INT OUTPUT --리턴될 페이징 후 나머지 레코드 수
AS
SET NOCOUNT ON
DECLARE @TOTAL_PAGE INT, @SQL VARCHAR(300) , @ASC_COUNT INT,@PagingMaxThread INT,@RowCount Int
Set @TOTAL_RECORDS = 0
Set @REMAINDER = 0
Set @PagingMaxThread = 0
SELECT @TOTAL_RECORDS = Count(0) FROM Board -- 뒤에 조건이 붙겠죠
SET @RowCount = @TOTAL_RECORDS - ((@PAGE -1 ) * @PAGE_SIZE )
SET ROWCOUNT @RowCount
Select @PagingMaxThread = Thread From Board
-- Where 뒤에 조건이 붙겠죠
SET ROWCOUNT @PAGE_SIZE
SELECT Seq,BoardCode,Thread,Depth,Writer,PWD,Email,Convert(varchar,Depth) + Title Title,Mode,IP,Readcount,Replycount,Transdate,Content
FROM board WHERE Thread <= @PagingMaxThread Order By Thread Desc
SET ROWCOUNT 0
--전체 레코드를 페이지의 수로 나눔.
SET @TOTAL_PAGE = CEILING (@TOTAL_RECORDS / @PAGE_SIZE)
--나머지 구함
SET @REMAINDER = @TOTAL_RECORDS % @PAGE_SIZE
IF @REMAINDER > 0
BEGIN
SET @ASC_COUNT = (@TOTAL_RECORDS - (@PAGE-1 ) * @PAGE_SIZE)
SET @TOTAL_PAGE = @TOTAL_PAGE + 1
IF @PAGE = @TOTAL_PAGE
BEGIN
SET @PAGE_SIZE = @REMAINDER
END
END
ELSE
BEGIN
SET @ASC_COUNT = (@TOTAL_RECORDS - (@PAGE-1) * @PAGE_SIZE)
END
IF @TOTAL_PAGE < @PAGE
BEGIN
Set @TOTAL_PAGE = 1
Set @PAGE = 1
END
[출처] [본문스크랩] 답변형 게시판 만들때 참조할 쿼리들|작성자 정신차렷
"쇼핑몰·홈페이지·오픈마켓
블로그·페이스북·이메일 등의 각종 마케팅 글쓰기, 각종 광고, 영업, 판매, 제안서, 전단지 반응율 3배×10배 이상 높이는 마법의 8단계 공식" |
☞자세히보기 |
|
|