--유틸함수---
// 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
// 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
댓글 0
번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|
6 | 데이타에 한글로 공백이 있는 경우 값을 가져오지 못하고 오류가 발생하는 경우 처리 | 구퍼 | 2009.03.23 | 5871 |
5 | mssql update예제(select ~ update) | 구퍼 | 2008.10.31 | 6318 |
4 | Update ~ Select 구문 예제 | 구퍼 | 2008.10.31 | 4699 |
3 | 분산트랜젝션 가능여부 확인 방법 | 하늘과컴 | 2008.05.29 | 5121 |
» | 순환구조에서 임시테이블에 하위 조직을 엮어서 넣는 프로시져 | 하늘과컴 | 2007.10.01 | 5311 |
1 | ORACLE -> MSSQL 시 LPAD 처리 | 운영자 | 2003.10.08 | 4268 |