반응형

Windows 2003에서 사용하던 SQL서버 2008 DB를 백업 한 후, Windows 2008 R2 상에 복원을 하였다.

그런데 Windows 2008 R2 서버 자체에서는 SQL서버로 접속이 아무 문제가 없는데

원격에서 SSMS(SQL Server Management Studio)로 접속을 하려고 했으나 되지를 않았다.

SQL 서버 네트워크 구성에 TCP/IP가 사용으로 되어 있는 지 확인 했는데 이 부분은 이상이 없었다.



이것 저것 삽질 하다가 결국 Windows 2008 R2 의 방화벽 문제라는 것을 알게 되었다. 보안강화의 목적이겠지만... SQL 서버가

설치 되면서 자동으로 해당 포트 정도는 열어 줄 수 있을 것 같은데... 아무튼 다음과 같이 수동으로 포트를 개방하면

원격에서 SQL서버로 접속 할 수 있다.

1. 서버 관리자에서 구성 > 고급 보안이 포함된 Windows 방화벽 > 인바운드 규칙에서 마우스 오른쪽 버튼 클릭 > 새 규칙 선택



2. 마법사에서 포트를 선택 하고 다음 으로.



3. SQL 서버의 기본 포트인 1433을 입력. 만일 네트워크 구성에서 기본 포트가 아닌 다른포트를 설정 했다면 해당 포트번호를 입력 한다.


4. 다음 화면에서 연결 허용을 선택하고 다음 으로.



5. 기본적으로 도메인, 개인, 공용 모두 선택되어 있는데 그냥 다음 으로.


6. 적당한 이름을 부여 하고 확인 하면 완료.

반응형
반응형

조직구조나 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


결과는 동일하다.

반응형
반응형
IP 대역을 스캐닝하여 살아있는 IP를 알려 주는 IP Scanner 입니다.


반응형

+ Recent posts