메뉴 건너뛰기

tnt_db

--유틸함수---
        // Resultset >> Vector
        public static void fillVector(Vector vector, ResultSet rs) throws Exception {
                while (rs.next()) {
                        Parameters params = new Parameters();
                        ResultSetMetaData rsmd = rs.getMetaData();
                        for (int i = 0; i < rsmd.getColumnCount(); i++) {
                                params.setParameter(rsmd.getColumnName(i + 1).toLowerCase(),
                                                rs.getObject(rsmd.getColumnName(i + 1)));
                        }
                        vector.addElement(params);
                }
        }



-------사용하는 방법------------

public Vector getSubOrg(String org_cd) throws Exception {
        Vector vector = new Vector();

    String sql = "";
try{
                   sql = "  EXEC sp_sub_org ? ";
                                            
        logger.debug("sql:" + sql);
        
//      스테이트먼트 객체에 쿼리를 설정
        pstmt = conn.prepareStatement(sql);
        
                pstmt.setString(1, org_cd);

                rs = pstmt.executeQuery();

                Convert.fillVector(vector, rs);
                        
    } catch(Exception e){
        e.printStackTrace();
        throw e;
    } finally{
        if(pstmt != null){
            try{
                logger.info("close DB");
            rs.close();
                pstmt.close();
                pstmt = null;
            } catch(Exception e1){}
          }
        }
                
        
        return vector;
}//select_org









----------------프로시져-----------------

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO

/***********************************************************
************************************************************/

        ALTER   PROCEDURE [dbo].[sp_sub_org]
                @ORG_CD        NVARCHAR(20)                 /* 조직코드 */
        AS

DECLARE @XLEVEL AS INT
        DECLARE @XPATH AS NVARCHAR(500)

        

        IF EXISTS (SELECT * FROM SYSOBJECTS WHERE XTYPE = 'u' AND NAME LIKE '%#MAKE_TREE_TEMP%')
        DROP TABLE #MAKE_TREE_TEMP

        CREATE TABLE #MAKE_TREE_TEMP
                (
                        CHILD_YN VARCHAR(1),
                        ORG_LEVEL NVARCHAR(20),
                        ORG_NAME NVARCHAR(100),
                        ORG_CD NVARCHAR(20),
                        UP_ORG_CD NVARCHAR(20),
                        XLEVEL INT,
                        XPATH NVARCHAR(500)
                )
        
        SET @XLEVEL = 0
        SET @XPATH = '.'

        IF @ORG_CD IS NULL OR @ORG_CD = ''
                SET @ORG_CD = ( SELECT MIN(ORG_CD) FROM VW_ORG
                                       WHERE 1=1
               --@YEAR BETWEEN LEFT(ORG_START_YMD, 4) AND LEFT(ORG_END_YMD, 4)
                                           )

        INSERT INTO #MAKE_TREE_TEMP
        SELECT
                CHILD_YN,
                ORG_LEVEL,
                ORG_NAME,
                ORG_CD,
                UP_ORG_CD,
                @XLEVEL,
                '.' + CAST(ORG_CD AS NVARCHAR(100)) + '.'
        FROM VW_ORG WHERE ORG_CD = @ORG_CD
    --AND @YEAR BETWEEN LEFT(ORG_START_YMD, 4) AND LEFT(ORG_END_YMD, 4)


        WHILE @@ROWCOUNT > 0
        BEGIN
                SET @XLEVEL = @XLEVEL + 1


                INSERT INTO #MAKE_TREE_TEMP
                SELECT
                        A.CHILD_YN,
                        A.ORG_LEVEL,
                        A.ORG_NAME,
                        A.ORG_CD,
                        A.UP_ORG_CD,
                        @XLEVEL,
                        B.XPATH + CAST (A.ORG_CD AS NVARCHAR(20)) + '.'
                FROM
                        (SELECT * FROM VW_ORG
                                WHERE         ORG_CD <> UP_ORG_CD
                                AND  USE_YN = 'Y'
                                --AND @YEAR BETWEEN LEFT(ORG_START_YMD, 4) AND LEFT(ORG_END_YMD, 4)  
                        )  AS A
                         JOIN #MAKE_TREE_TEMP AS B
                ON
                        A.UP_ORG_CD = B.ORG_CD AND B.XLEVEL = @XLEVEL - 1

                --PRINT CAST(@XLEVEL AS NVARCHAR(10)) + '<<<<<<'
        END
        

        select         child_yn, org_level, org_name,
                        replicate('->', xlevel) + org_name as org_name_path,
                        xlevel,  xpath, org_cd, up_org_cd
                from #make_tree_temp
                order by xpath        


SET ANSI_NULLS OFF

위로