SQLServer and Access, Excel data transfer simple summary

The so-called data transmission, in fact, refers to the SQLServer access Access, data between Excel.

Why should we consider this problem?

Due to historical reasons, many customers before the data are stored in a text database, such as Acess, Excel, Foxpro. Now the system upgrade and database servers such as SQLServer, after ORACLE, often need to access text data in the database, it will have such a demand. Some time ago a business trip of the project, is facing such a problem: SQLServer and the exchange of data between VFP.

To complete the required title, it is a very simple thing in SQLServer.

Usually there are three possible ways: 1, DTS tool 2, BCP 3, distributed query

DTS do not need to say, because that is the graphical user interface, very easy to use.

Here is mainly about the latter two are, respectively, check, add, delete, change as a simple example:

The following nonsense did not say, directly manifested in the form of T-SQL.


A, SQLServer and Access

1, the data in the Access query methods:

select * from OpenRowSet('microsoft.jet.oledb.4.0',';database=c:\db2.mdb','select * from serv_user')

or

select * from OpenDataSource('Microsoft.Jet.OLEDB.4.0','Data Source="c:\DB2.mdb";User ID=Admin;Password=')...serv_user

2, writing data from Access to SQLServer:

insert into OpenRowSet('microsoft.jet.oledb.4.0',';database=c:\db2.mdb','select * from Accee表')
select * from SQLServer表
或用BCP

master..xp_cmdshell'bcp "serv-htjs.dbo.serv_user" out  "c:\db3.mdb" -c -q -S"." -U"sa" -P"sa"'

The main difference between the above is: OpenRowSet mdb and tables need to exist, BCP will be generated when the mdb does not exist

3, writing data from Access to SQLServer: With the above basis, this is very simple

insert into SQLServer表 select * from
OpenRowSet('microsoft.jet.oledb.4.0',';database=c:\db2.mdb','select * from Accee表')

Or with BCP

master..xp_cmdshell'bcp "serv-htjs.dbo.serv_user" in  "c:\db3.mdb" -c -q -S"." -U"sa" -P"sa"'

4, delete the Access data:

delete from OpenRowSet('microsoft.jet.oledb.4.0',';database=c:\db2.mdb','select * from serv_user')
where lock=0

5, data modify Access:

update OpenRowSet('microsoft.jet.oledb.4.0',';database=c:\db2.mdb','select * from serv_user')
set lock=1

SQLServer and Access generally so much.


Two, SQLServer and Excel


1, the query to Excel

select * from OpenRowSet('microsoft.jet.oledb.4.0','Excel 8.0;HDR=yes;database=c:\book1.xls;','select * from [Sheet1$]') where c like '%f%'

select * from
OPENROWSET('MICROSOFT.JET.OLEDB.4.0'
,'Excel 5.0;HDR=YES;IMEX=2;DATABASE=c:\book1.xls',[sheet1$])

1) may be the first line of field XLS as viewed when hdr = yes, as the first one in hdr = no, then an error is reported when WHERE
2) [] $ dollars and must be, or may not recognize this account M $

2, modify Execl

update OpenRowSet('microsoft.jet.oledb.4.0','Excel 8.0;hdr=yes;database=c:\book1.xls;','select * from [Sheet1$]')
set a='erquan' where c like '%f%'

3, import and export


insert into OpenRowSet('microsoft.jet.oledb.4.0','Excel 8.0;hdr=yes;database=c:\book1.xls;','select * from [Sheet2$]')(id,name)
select id,name from serv_user

Or BCP

master..xp_cmdshell'bcp "serv-htjs.dbo.serv_user" out  "c:\book2.xls" -c -q -S"." -U"sa" -P"sa"'

Import from Excel to SQLServer:

select * into serv_user_bak
from OpenRowSet('microsoft.jet.oledb.4.0','Excel 8.0;HDR=yes;database=c:\book1.xls;','select * from [Sheet1$]')

If the table serv_user_bak does not exist, create

Detailed answers about the BCP and distributed queries, search on SQLServer own help it.
SQLServer and txt files, HTML files, VFP Data exchange files have become very easy. . . .

In fact, these elements are there to help, even just summed up what, to facilitate your reference, Oh ~ ~

cptrk.ashx?id=bf182e80-f5f6-4c2b-a6b7-fba0b4168ac1

Reproduced in: https: //www.cnblogs.com/Maxer/archive/2006/08/06/469279.html

Guess you like

Origin blog.csdn.net/weixin_33762130/article/details/93994798