반응형

SQL서버 2008 SSMS 로그인할 때 암호저장을 체크 해 놓아도 저장이 안되는 경우가 있다.

보안상 좋을지는 몰라도 매번 입력하기가 여간 성가시지 않다.

이때는 다음 파일을 찾아서 삭제 하면 된다.

단 기존에 저장되어 있던 목록이 모두 삭제 되는 문제는 있지만 어차피 자주 로그인 하는 경우라면

한번만 다시 입력하면 되므로, 매번 암호입력을 해야 되는 수고를 덜 수 있다면 그정도는 감수 할 만.

Windows 7 의 경우 : C:\Users\사용자\AppData\Roaming\Microsoft\Microsoft SQL Server\100\Tools\Shell\SqlStudio.bin

Windows XP의 경우 : C:\Documents and Settings\사용자\Application Data\Microsoft\Microsoft SQL Server\100\Tools\Shell\SqlStudio.bin

해당 파일을 삭제 한 다음 반드시 SSMS 를 재시작 해야만 효과를 볼 수 있다. 
반응형
반응형

조직구조나 BOM 과 같이 계층구조로 된 데이터에 대하여 쉽게 쿼리를 구현할 수 있도록 해주는 것이 CTE,
그중에서도 재귀적 CTE 이다.


create table test1

(mycode varchar(10), uppercode varchar(10))

insert into test1

(mycode, uppercode)

select 1, 2

union all

select 2, 3

union all

select 3, 4

union all

select 4, null

insert into test1

(mycode, uppercode)

select 10, 20

union all

select 20, 30

union all

select 30, 4

insert into test1

(mycode, uppercode)

select 5, 2

go

with cteTest(uppercode, mycode, levelno, groupcode)

as

(

       select uppercode, mycode, 0, mycode from test1 where uppercode = 4

       union all

       select a.uppercode, a.mycode, b.levelno+1, b.groupcode  from test1 a join cteTest b on a.uppercode = b.mycode

)

select * from cteTest

order by groupcode, levelno

go



with cteTest(uppercode, mycode, levelno, groupcode)

as

(

       select uppercode, mycode, 0, mycode from test1 where uppercode = 4

       union all

       select a.uppercode, a.mycode, b.levelno+1, b.groupcode  from test1 a join cteTest b on a.uppercode = b.mycode

)

select

       a.groupcode as rootcode,

       a.mycode as leafcode

from cteTest a

where

       a.levelno = (select MAX(levelno) from cteTest b

                                 where a.groupcode = b.groupcode)




마지막 쿼리는 계층구조의 최하위 레벨과 최상위코드(상위코드가 NULL) + 1인 레벨을 연결한 예이다.
(조직구조의 최하위 조직이 속한 최상위 조직을 알고자 하는 경우)

반응형
반응형

SQL서버 에서 제공되는 ROLLUP을 사용하면 그룹간의 소계 및 전체 합계를 쉽게 구할 수 있다.

create table test1
(id int,
 dept_code int,
 qty int)

insert into test1
 (id, dept_code, qty)
 select 4, 200, 1450
 union all
 select 6, 300, 1230
 union all
 select 3, 100, 3000
 union all
 select 1, 100, 1000
 union all
 select 5, 200, 2540
 union all
 select 2, 100, 2000
select
    case when dept_code is null and id is null then '합계'
         when id is null then '소계'
         else CAST(id as varchar(10)) 
    end as id,
    dept_code, SUM(qty) as qty
from 
    test1
group by 
  rollup (dept_code, id)


ROLLUP을 사용하지 않고 기존 ANSI Query를 사용하면 다음과 같이 할 수 있다.

select
       case b.gb when 1 then CAST(a.id as varchar(10))
                 when 2 then '소계'
                 when 3 then '합계'
       end as id,
       case when b.gb in (1,2) then a.dept_code end as dept_code,
       SUM(qty) as qty
from  
       test1 a
join
       (select 1 as gb union all select 2 union all select 3) b on 1=1
group by
       b.gb,
       case b.gb when 1 then CAST(a.id as varchar(10))
                 when 2 then '소계'
                 when 3 then '합계'
       end,
       case when b.gb in (1,2) then a.dept_code end
order by
       case when case when b.gb in (1,2) then a.dept_code end is null then 999
            else case when b.gb in (1,2) then a.dept_code end end, gb


결과는 동일하다.

반응형
반응형

SQL서버에서 Oracle을 연결된 서버로 구성한 후 OpenQuery로 조회를 하면

메시지 9803, 수준 16, 상태 1, 줄 1
"numeric" 유형에 대한 데이터가 잘못되었습니다.


라는 오류가 발생 할 때가 있다.

원인은 Oracle 테이블에서 Type이 int 나 decimal인 컬럼을 사용하게 되면 발생 한다.

연결된 서버 구성시 공급자를 'Oracle Providier for OLE DB'로 지정 했을 때 이런 현상이 있다.

(공급자를 Microsoft OLE DB Provider for Oracle로 했을 때는 어떤 지는 모르겠다.

참고로 연결된 서버로 Oracle 을 추가한 후 공급자 속성에서 반드시 Inprocess허용을 해줘야 된다.)


오류를 해결 하기 위해서는 두가지 정도의 방법이 있는 것 같다.

첫번째는 OpenQuery 파라미터로 넘기는 쿼리에서 int 나 decimal 타입의 컬럼은 모두 to_number() 또는 to_char()로 형변환을

해서 SELECT를 하는 방법이고, 두번째는 컬럼 타입을 NUMBER 또는 FLOAT 로 변경 하는 방법이다.

단 첫번째 방법을 사용하면 SELECT 인 경우에는 문제가 없으나 해당 컬럼에 대하여 INSERT, UPDATE 할때

오류가 발생 한다.


연결된 서버 "hr"의 OLE DB 공급자 "OraOLEDB.Oracle"이(가) 메시지 "ROW-00008: Cannot update data in a read-only column"을(를) 반환했습니다.
메시지 7343, 수준 16, 상태 4, 줄 1
연결된 서버 "hr"의 OLE DB 공급자 "OraOLEDB.Oracle"이(가) 테이블 "[OraOLEDB.Oracle]"을(를) UPDATE할 수 없습니다.
반응형

+ Recent posts