울어라휘파람새야

엑셀을 쿼리에서 다루기 본문

밥줄/MS-SQL

엑셀을 쿼리에서 다루기

꼬꼬리코 2009. 3. 2. 11:21

이런경우 linkedserver 나 opendatasource를 사용하여 쿼리할수있겠네요.
===========================================================================
먼저 db에다가 샘플을 만들고
--drop table t1
create table t1(id varchar(10), c1 varchar(10), c2 varchar(10))
insert into t1 values ('a1', 'a', 'a')
insert into t1 values ('a2', 'b', 'b')
insert into t1 values ('a3', 'c', 'c')
insert into t1 values ('a4', 'd', 'd')
go

c:\temp\test1.xls 에
c1 c2  id
-- --  ---
a a a1
b b a2
c b a3
d c a4
이런 샘플데이터를 먼저 만들어 두고.


OpenDataSource를 이용하는 방법
---------------------------------------------------------------------------
1. 그냥 바로 쿼리한다.

select x.*, convert(varchar(10), y.c1) c1, convert(varchar(10), y.c2) c2
from t1 x inner join OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
                'Data Source="c:\temp\test1.xls";Extended properties=Excel 5.0')...sheet1$  y
on x.id = y.id
where x.c1 <> y.c1
   or x.c2 <> y.c2

id         c1         c2         c1         c2        
---------- ---------- ---------- ---------- ----------
a3         c          c          c          b
a4         d          d          d          c

(2개 행 적용됨)

---------------------------------------------------------------------------
---------------------------------------------------------------------------

linkedserver를 이용하는 방법
---------------------------------------------------------------------------
1. linkedserver를 먼저 만들고
exec sp_addlinkedserver 'Excel1',
   'Jet 4.0',
   'Microsoft.Jet.OLEDB.4.0',
   'c:\temp\test1.xls',
   NULL,
   'Excel 5.0'
GO

2. excel에 들어 있는 자료확인해보고
select * from excel1...sheet1$

c1 c2  id
-- --  ---
a a a1
b b a2
c b a3
d c a4

(4개 행 적용됨)

3. 두테이블을 조인
select x.*, convert(varchar(10), y.c1) c1, convert(varchar(10), y.c2) c2
from t1 x inner join excel1...sheet1$ y
on x.id = y.id
where x.c1 <> y.c1
   or x.c2 <> y.c2

4. 필요없어진 linkedserver 삭제
exec sp_dropserver 'excel1'



---------------------------------------------------------------------------------------------------

여기부터는 제가 sqlserver2005에서 테스트 하면서 긁적긁적...


파일을 만들고 쿼리를 약간수정하여 아래와 같이 한 수 실행을 시키면,,,

declare @table Table(id varchar(10), c1 varchar(10), c2 varchar(10))
insert into @table values ('a1', 'a', 'a')
insert into @table values ('a2', 'b', 'b')
insert into @table values ('a3', 'c', 'c')
insert into @table values ('a4', 'd', 'd')


select x.*, convert(varchar(10), y.c1) c1, convert(varchar(10), y.c2) c2
from @table x inner join OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
                'Data Source="c:\temp\test1.xls";Extended properties=Excel 5.0')...sheet1$  y
on x.id = y.id
where x.c1 <> y.c1
   or x.c2 <> y.c2

구성 요소 'Ad Hoc Distributed Queries'이(가) SQL Server 보안 구성의 일부로 해제되었으므로 이 구성 요소의 문 'OpenRowset/OpenDatasource'에 대한 액세스가 차단되었습니다. 시스템 관리자는 sp_configure를 사용하여 'Ad Hoc Distributed Queries'의 사용을 활성화할 수 있습니다. 'Ad Hoc Distributed Queries' 활성화 방법은 SQL Server 온라인 설명서의 "노출 영역 구성"을 참조하십시오.
이런 아주 친절하고도 친절한 에러메시지를 출력하더군요. 출력된 내용대로 찾아보니깐...

sp_configure 'Ad Hoc Distributed Queries', 1;
GO
RECONFIGURE;
GO

이렇게 명령을 하라고 해서 해보면

구성 옵션 'Ad Hoc Distributed Queries'이(가) 없거나 고급 옵션입니다.

이런 오류가 나오구요.. 찾아보면 고급옵션관련은

sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO

이걸 먼저 해줘야 되더군요 ㅎㅎ


요런 결과가 쿼리됩니다.

id         c1         c2         c1         c2
---------- ---------- ---------- ---------- ----------
a3         c          c          c          b
a4         d          d          d          c


퍼옴: http://blog.naver.com/bellvirus1?Redirect=Log&logNo=80002863924

퍼옴: http://cafe.naver.com/smartnet.cafe?iframe_url=/ArticleRead.nhn%3Farticleid=296


---------------------------------------------------------------------------------------------------

위 문제 해결 결과

Microsoft OLE DB Provider for SQL Server (0x80040E14)
OLE DB 공급자 'Microsoft.Jet.OLEDB.4.0'에 대한 임의 액세스가 거부되었습니다. 연결된 서버를 통해 이 공급자에 액세스해야 합니다.

오류 발생 그래서 재검색해보니

OPENROWSET 쿼리를 사용하여 원격 데이터 엑세스 실패  글 검색

바로 앞 글에 내용 있음. (결국 해결은 못함)

Comments