[转]【利用OpenDataSource、OPENROWSET进行分布式查询和数据的导入导出】

楼主
[转]【利用OpenDataSource、OPENROWSET进行分布式查询和数据的导入导出】
[P][B][SIZE=14px]利用OpenDataSource、OPENROWSET进行分布式查询和数据的导入导出[/SIZE][/B][/P][P][B][COLOR=rgb(255, 0, 0)][SIZE=12px][SIZE=12px]ps:此文转自他人资料[/COLOR][/SIZE][/SIZE][/B][/P][P][B][/B] [/P][P][B] [SIZE=14px]OpenDataSource:[/SIZE][/B][/P][P][COLOR=rgb(255, 0, 0)][SIZE=12px][B][SIZE=14px][COLOR=rgb(255, 0, 255)]一:[COLOR=rgb(255, 0, 255)]SQL SERVER 和ACESS的数据导入导出[/COLOR][/SIZE][/SIZE][/B][/COLOR][/COLOR][/P][P][B]1.在SQL SERVER里查询access数据:[/B]
-- ======================================================
SELECT *
 FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
 'Data Source="c:/DB.mdb";User ID=Admin;Password=')...表名[/P][P]select *  from opendatasource('Microsoft.Jet.OLEDB.4.0','Data Source="D:/db.mdb";User ID=Admin;Password=')...Student where name = '我'
select * from newTable
 -------------------------------------------------------------------------------------------------
[B]2.将access导入SQL server[/B]
 -- ======================================================
在SQL SERVER 里运行:
SELECT *
 INTO newtable
 FROM OPENDATASOURCE ('Microsoft.Jet.OLEDB.4.0',
       'Data Source="c:/DB.mdb";User ID=Admin;Password=' )...表名
-------------------------------------------------------------------------------------------------
[B]3.将SQL SERVER表里的数据插入到Access表中[/B]
-- ======================================================
在SQL SERVER 里运行:
insert into OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
   'Data Source=" c:/DB.mdb";User ID=Admin;Password=')...表名
(列名1,列名2)
 select 列名1,列名2  from  sql表[/P][P]实例:
insert into  OPENROWSET('Microsoft.Jet.OLEDB.4.0',
    'C:/db.mdb';'admin';'', Test)
 select id,name from Test
 INSERT INTO OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'c:/trade.mdb'; 'admin'; '', 表名)
SELECT *
 FROM sqltablename[/P][P]
 insert into  OpenDataSource('Microsoft.Jet.OLEDB.4.0','Data Source="D:/db.mdb";User ID=Admin;Password=')...Student(studentid,name)
 select staffid,name from staff[/P][P]insert into  OPENROWSET('Microsoft.Jet.OLEDB.4.0',
    'D:/db.mdb';'admin';'', Student)
 select accountid,name from tblbaccount[/P][P]select * from openrowset('Microsoft.Jet.OLEDB.4.0','D:/db.mdb';'admin';'',Student)
 -------------------------------------------------------------------------------------------------[/P][P] [/P][P] [/P][P][SIZE=14px][B][COLOR=rgb(255, 0, 255)]二、SQL SERVER 和EXCEL的数据导入导出[/SIZE][/B][/COLOR]
[B]1、在SQL SERVER里查询Excel数据:[/B]
-- ======================================================
SELECT *
 FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
 'Data Source="c:/book1.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...[Sheet1$]
下面是个查询的示例,它通过用于 Jet 的 OLE DB 提供程序查询 Excel 电子表格。
SELECT *
 FROM OpenDataSource ( 'Microsoft.Jet.OLEDB.4.0',
   'Data Source="c:/Finance/account.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...xactions[/P][P]SELECT *
 FROM OpenDataSource ( 'Microsoft.Jet.OLEDB.4.0',
   'Data Source="D:/财务统计科目.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...vib
 -------------------------------------------------------------------------------------------------[/P][P][B]2、将Excel的数据导入SQL server :[/B]
 -- ======================================================
 SELECT * into newtable
 FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
   'Data Source="c:/book1.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...[Sheet1$]
实例:
SELECT * into newtable
 FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
   'Data Source="c:/Finance/account.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...xactions
 -------------------------------------------------------------------------------------------------[/P][P][B]3、将SQL SERVER中查询到的数据导成一个Excel文件[/B]
-- ======================================================
T-SQL代码:
EXEC master..xp_cmdshell 'bcp 库名.dbo.表名out c:/Temp.xls -c -q -S"servername" -U"sa" -P""'
参数:S 是SQL服务器名;U是用户;P是密码[/P][P]EXEC master..xp_cmdshell 'bcp axzq.dbo.Staff out d:staff.xls -c -q -S"." -U"sa" -P"gazx"'[/P][P]说明:还可以导出文本文件等多种格式
 实例:EXEC master..xp_cmdshell 'bcp saletesttmp.dbo.CusAccount out c:/temp1.xls -c -q -S"pmserver" -U"sa" -P"sa"'
 EXEC master..xp_cmdshell 'bcp "SELECT au_fname, au_lname FROM pubs..authors ORDER BY au_lname" queryout C:/ authors.xls -c -Sservername -Usa -Ppassword'[/P][P]EXEC master..xp_cmdshell 'bcp "select name from axzq..staff order by name"  queryout d:staffName.xls -c -q -S"." -U"sa" -P"gazx"'[/P][P]------------------------------------------------------------------------------------------------[/P][P][B]4、在SQL SERVER里往Excel插入数据:
[/B]-- ======================================================
insert into OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
 'Data Source="c:/Temp.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...table1 (A1,A2,A3) values (1,2,3)[/P][P]T-SQL代码:
INSERT INTO  
 OPENDATASOURCE('Microsoft.JET.OLEDB.4.0',  
 'Extended Properties=Excel 8.0;Data source=C:/training/inventur.xls')...[Filiale1$]  
 (bestand, produkt) VALUES (20, 'Test')  
 -------------------------------------------------------------------------------------------------[/P][P] [/P][P][B][SIZE=14px]OPENROWSET:[/SIZE][/B][/P][P]举几个例子:[/P][P]--1.[/P][P][SIZE=10px][SIZE=10px]SELECT a.*
 FROM OPENROWSET('SQLNCLI', 'Server=Seattle1;Trusted_Connection=yes;',
      'SELECT GroupName, Name, DepartmentID
       FROM AdventureWorks.HumanResources.Department
       ORDER BY GroupName, Name') AS a;[/SIZE][/SIZE][/P][P] [/P][P][SIZE=10px][SIZE=10px]--2.[/SIZE][/SIZE][/P][P][SIZE=10px][SIZE=10px]SELECT CustomerID, CompanyName
    FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
       'C:/Program Files/Microsoft Office/OFFICE11/SAMPLES/Northwind.mdb';
       'admin';'',Customers)
[/SIZE][/SIZE][/P][P][SIZE=10px][SIZE=10px]--3.[/SIZE][/SIZE][/P][P][SIZE=10px][SIZE=10px]USE Northwind  
 GO
 SELECT c.*, o.*
 FROM Northwind.dbo.Customers AS c
    INNER JOIN OPENROWSET('Microsoft.Jet.OLEDB.4.0',
    'C:/Program Files/Microsoft Office/OFFICE11/SAMPLES/Northwind.mdb';'admin';'', Orders)    
    AS o
    ON c.CustomerID = o.CustomerID
[/SIZE][/SIZE][/P][P] [/P][P][SIZE=10px][SIZE=10px]--4[/SIZE][/SIZE][/P][P][SIZE=10px][SIZE=10px]USE AdventureWorks
 GO
 CREATE TABLE myTable(FileName nvarchar(60),
   FileType nvarchar(60), Document varbinary(max))
 GO[/SIZE][/SIZE][/P][P][SIZE=10px][SIZE=10px]INSERT INTO myTable(FileName, FileType, Document)
    SELECT 'Text1.txt' AS FileName,
       '.txt' AS FileType,
       * FROM OPENROWSET(BULK N'C:/Text1.txt', SINGLE_BLOB) AS Document[/SIZE][/SIZE][/P][P][SIZE=10px]
[SIZE=10px]--5[/SIZE][/SIZE][/P][P][SIZE=10px][SIZE=10px]SELECT a.* FROM OPENROWSET( BULK 'c:/test/values.txt',
    FORMATFILE = 'c:/test/values.fmt') AS a;[/SIZE][/SIZE] [/P][P] [/P][P]
[COLOR=rgb(255, 0, 0)]附上:[/COLOR]
[B]--开启xp_cmdshell[/B]
 --SQL Server blocked access to procedure 'xp_cmdshell'
 sp_configure 'show advanced options', 1
 go
 reconfigure
 go
 sp_configure 'xp_cmdshell', 1
 go
 reconfigure
 go[/P][P][B]--开启sp_OACreate[/B]
 --SQL Server blocked access to procedure 'sys.sp_OACreate'
 sp_configure 'show advanced options', 1;
 go
 reconfigure;
 go
 sp_configure 'ole automation procedures', 1;
 go
 reconfigure;
 go[/P][P]
 sp_configure 'Ad Hoc Distributed Queries',1;
 go
 reconfigure
 go[/P]

电脑版 Page created in 0.0781 seconds with 4 queries.