create proc List_Cafe(@ct_text nvarchar(20) ,@text nvarchar(50),@pagen int,@page int, @mID varchar(20))
-- List_Cafe 프로시저를 만들어줌
-- @ct_text, @text, @pagen, @page변수 선언
as
begin
declare @table table --table 자료형으로 @table 변수 선언
(
sn int, -- sn이라는 int형 컬럼 선언
cnt int, --cnt라는 int형 컬럼 선언
name nvarchar(50), --name이라는 nvarchar컬럼 선언
make_date char(10), --make_date라느 char컬럼 선언
cate_name nvarchar(20), --cate_name이라는 nvarchar컬럼 선언
join_cnt int
)
-- @table변수에 아래 select된 내용을 넣어줌
insert into @table
--카페 목록 출력(카페 번호 , 카페에 가입된 회원수 , 카페명 , 커패생성일 , 카테고리명)
select sn, num, name, make_date, cate_name, isnull(mj.fk_member, 0) as fk_member from
(
select sn as sn,
(
-- 카페에 가입된 회원수 출력
select count(*) as num
from dbo.Member_join mj
where fk_cafe = sn
) as num, name as name, convert(char(10), make_date, 120) as make_date, cate_name as cate_name
from dbo.Cafe ca
inner join dbo.Category cg
on ca.fk_cate = cg.cate_id
) as tbl
left outer join
(
select fk_cafe as fk_cafe, id as id, fk_member as fk_member
from member_join as mj
inner join member as mb
on mj.fk_member = mb.sn
group by mj.fk_cafe, mb.id, mj.fk_member
having id = @mID
) as mj
on tbl.sn = mj.fk_cafe
--검색시 카테고리가 null이거나 아무것도 선택하지 않을 시 모든 카페목록 출력
if(@ct_text is null or @ct_text = '')
begin
select top(@pagen)* from @table
where sn not in
(select top ((@page -1) * @pagen) sn from @table order by sn desc)
order by sn desc
end
else if(@ct_text = 'All')-- 카테고리랑 상관없이 카페명으로만 검색
begin
select top(@pagen) * from @table
where name like '%' + @text + '%' and sn not in
(select top ((@page -1) * @pagen) sn from @table order by sn desc)
order by sn desc
end
else--카테고리와 카페명으로 검색
begin
select top(@pagen) * from @table
where name like '%' + @text + '%' and cate_name = @ct_text and sn not in
(select top ((@page -1) * @pagen) sn from @table order by sn desc)
order by sn desc
end
end
go
댓글 영역