database.sarang.net
UserID
Passwd
Database
DBMS
MySQL
PostgreSQL
Firebird
Oracle
Informix
Sybase
ㆍMS-SQL
DB2
Cache
CUBRID
LDAP
ALTIBASE
Tibero
DB 문서들
스터디
Community
공지사항
자유게시판
구인|구직
DSN 갤러리
도움주신분들
Admin
운영게시판
최근게시물
MS-SQL Q&A 2143 게시물 읽기
No. 2143
셀프조인을 이용한 카테고리 구현 ?
작성자
석이
작성일
2005-07-24 23:39ⓒ
2005-07-24 23:57ⓜ
조회수
4,719

create table cate
(
parent int
,cateName varchar(100)
,cate int
)

create table product
(
idx int identity
,cate int
,productName varchar(100)
)

insert into cate (cateName , cate) values ('자동차',1)
insert into cate (cateName , cate) values ('배',2)
insert into cate (cateName , cate) values ('기차',3)
select * from cate

insert into cate (parent, cateName, cate) values(1,'자동차엔진',4)
insert into cate (parent, cateName, cate) values(2,'배엔진',5)
insert into cate (parent, cateName, cate) values(3,'기차엔진',6)
insert into cate (parent, cateName, cate) values(1,'자동차프레임',7)
insert into cate (parent, cateName, cate) values(7,'자동차프레임_종류1',8)
insert into cate (parent, cateName, cate) values(7,'자동차프레임_종류2',9)
insert into cate (parent, cateName, cate) values(7,'자동차프레임_종류3',10)
insert into cate (parent, cateName, cate) values(10,'자동차프레임_종류3_분류1',11)


-- ---------------------------------------------------------------------------------
declare @cate int
set @cate = 7

select '상위' as CWhere, b.cateName ,b.cate, a.cateName, a.cate from
cate a inner join cate b
on a.cate = b.parent
where b.cate = @cate

union all

select '하위' as CWhere, a.cateName ,a.cate, b.cateName, b.cate from
cate a inner join cate b
on a.cate = b.parent
where a.cate = @cate


CWhere cateName cate cateName cate
------ ---------------------- ----------- --------------------------- -----------
상위 자동차프레임 7 자동차 1
하위 자동차프레임 7 자동차프레임_종류1 8
하위 자동차프레임 7 자동차프레임_종류2 9
하위 자동차프레임 7 자동차프레임_종류3 10

 

이렇게 하면 어플리케이션에서 꾸밀수 있을까요?


이 글에 대한 댓글이 총 5건 있습니다.

하나의 테이블로 무한카테고리 구현 하신분 어떻게 구현하는지 답글좀 주시면 감사하겠습니다.

정말 null 나올때까지 외부 어플에서 무한루프 돌려요?

 

석이님이 2005-07-25 14:41에 작성한 댓글입니다. Edit

--파라미터로 받은 코드를 기준으로 계층구조를 뽑아서

--테이블에 저장하는함수입니다. 적당히 고쳐 보세요. 

--sec_up_code 상위부서코드, sec_code 부서코드

 

CREATE FUNCTION HWF_TBL_SEC_CASCADE_FROM_CODE  (@current varchar(32))
RETURNS @rtn_table table(sec_code varchar(32), lvl int)
AS
Begin

-- SET NOCOUNT ON
 DECLARE @lvl int;
DECLARE @line varchar(32);
DECLARE @strRow varchar(1000);
DECLARE @tmp_table table(sec_code varchar(32), lvl int);

SET @strRow = ''
 
--CREATE TABLE #stack (item varchar(32), lvl int) --Create a tempory stack.
--CREATE TABLE @tmp_table (item varchar(32), lvl int) 
-- INSERT INTO #stack VALUES (@current, 1) --Insert current node to the stack.
 INSERT INTO @tmp_table VALUES (@current, 1) 
 SELECT @lvl = 1    
 WHILE @lvl > 0     --From the top level going down.
 BEGIN
 --    IF EXISTS (SELECT * FROM #stack WHERE lvl = @lvl)
    IF EXISTS (SELECT * FROM  @tmp_table WHERE lvl = @lvl)
         BEGIN
             SELECT @current = sec_code          --Find the first node that matches current node's name.
 --            FROM #stack
                       FROM @tmp_table
             WHERE lvl = @lvl
--PRINT 1
 --            SELECT @line = space(@lvl - 1) + @current --@lvl - 1 s spaces before the node name.
   SELECT @line =  @current
 --            PRINT @line     --Print it.
  SET @strRow = @strRow + ', ' + @line
   INSERT INTO @rtn_table VALUES (@line, @lvl - 1) 

 --            DELETE FROM #stack
                        DELETE FROM @tmp_table
             WHERE lvl = @lvl
                 AND sec_code = @current --Remove the current node from the stack.

        --    INSERT #stack  --Insert the childnodes of the current node into the stack.
   INSERT  @tmp_table
                  SELECT sec_code, @lvl + 1
                 FROM hw_section
                 WHERE sec_up_code = @current
             IF @@ROWCOUNT > 0  --If the previous statement added one or more nodes, go down for its first child.
                        SELECT @lvl = @lvl + 1 --If no nodes are added, check its brother nodes.
  END
     ELSE
  SELECT @lvl = @lvl - 1  --Back to the level immediately above.
END
return
end

 

--함수 테스트용
--EXEC HWF_TBL_SEC_CASCADE_FROM_CODE  '23021000'

woojoo216님이 2005-07-25 21:01에 작성한 댓글입니다. Edit

--위에서 참고햇던 원본입니다.아마 msdn에서 제공하는 샘플이엇던듯..

CREATE TABLE hierarchy
        (parent VARCHAR(20) NOT NULL,
         child VARCHAR(20),
CONSTRAINT UIX_parentchild
         UNIQUE NONCLUSTERED (parent,child)
)
CREATE CLUSTERED INDEX CIX_parent
 ON hierarchy(parent)
GO
INSERT hierarchy VALUES('World','Europe')
INSERT hierarchy VALUES('World','North America')
INSERT hierarchy VALUES('Europe','France')
INSERT hierarchy VALUES('France','Paris')
INSERT hierarchy VALUES('North America','United States')
INSERT hierarchy VALUES('North America','Canada')
INSERT hierarchy VALUES('United States','New York')
INSERT hierarchy VALUES('United States','Washington')
INSERT hierarchy VALUES('New York','New York City')
INSERT hierarchy VALUES('Washington','Redmond')
GO

CREATE PROCEDURE expand (@current char(20)) as --This is a non-recursive preorder traversal.
 SET NOCOUNT ON
 DECLARE @lvl int, @line char(20)

 CREATE TABLE #stack (item char(20), lvl int) --Create a tempory stack.
 INSERT INTO #stack VALUES (@current, 1) --Insert current node to the stack.
 SELECT @lvl = 1    
 WHILE @lvl > 0     --From the top level going down.
 BEGIN
     IF EXISTS (SELECT * FROM #stack WHERE lvl = @lvl)
         BEGIN
             SELECT @current = item --Find the first node that matches current node's name.
             FROM #stack
             WHERE lvl = @lvl

             SELECT @line = space(@lvl - 1) + @current --@lvl - 1 s spaces before the node name.
             PRINT @line     --Print it.

             DELETE FROM #stack
             WHERE lvl = @lvl
                 AND item = @current --Remove the current node from the stack.

             INSERT #stack  --Insert the childnodes of the current node into the stack.
                 SELECT child, @lvl + 1
                 FROM hierarchy
                 WHERE parent = @current

             IF @@ROWCOUNT > 0  --If the previous statement added one or more nodes, go down for its first child.
                        SELECT @lvl = @lvl + 1 --If no nodes are added, check its brother nodes.
   END
     ELSE
       SELECT @lvl = @lvl - 1  --Back to the level immediately above.
        
END       --While

GO
EXEC expand 'World'

woojoo님이 2005-07-25 21:06에 작성한 댓글입니다. Edit

계층형 게시판의 로직을 쓰면 간단히 되지 않을까요?

무한, 유한 다 가능할텐데... ^^

이경환(babocom)님이 2005-07-26 09:22에 작성한 댓글입니다.

아~ 넘 어려버~

커넥티드바이는 왜 안되는기야...ㅠ.ㅠ

 

석이님이 2005-07-30 11:34에 작성한 댓글입니다. Edit
[Top]
No.
제목
작성자
작성일
조회
2146하드코딩으로 하면 타는 index가 변수로 대체시 타지 않습니다. [11]
박은희
2005-07-26
3179
2145attach 발생한 에러 입니다. [2]
attacher
2005-07-25
3857
2144currval과 같은 기능은 없나요? [1]
lee
2005-07-25
2636
2143셀프조인을 이용한 카테고리 구현 ? [5]
석이
2005-07-24
4719
2142mssql에서 mysql에서처럼 show create table 과 같은역할을 하는 구문은 [14]
bright
2005-07-23
8229
2141특수문자를 포함한 검색 질문 입니다. [4]
석이
2005-07-23
5919
2140날짜 컬럼에 NonCulsterindex를 만들어주면.. [2]
SQL초보
2005-07-22
2663
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2024 DSN, All rights reserved.
작업시간: 0.016초, 이곳 서비스는
	PostgreSQL v16.2로 자료를 관리합니다