該文章講述了利用以下query進(jìn)行SQL Server2000與Excel之間數(shù)據(jù)的導(dǎo)入導(dǎo)出. 利用以下的query進(jìn)行SQL Server2000與Excel之間數(shù)據(jù)的導(dǎo)入導(dǎo)出: insert into OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=D:\testing.xls;', 'SELECT * FROM [SheetName$]') select * from SQLServerTable 從Excel導(dǎo)入到新的SQL Server table, select * into SQLServerTable FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=D:\testing.xls;HDR=YES', 'SELECT * FROM [Sheet1$]') 從Excel導(dǎo)入到已存在的 SQL Server table, Insert into SQLServerTable Select * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=D:\testing.xls;HDR=YES', 'SELECT * FROM [SheetName$]') 另收集: 一,、SQLServer和Excel1,、向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)hdr=yes時可以把xls的第1行作為字段看待,如第1個中hdr=no的話,,where時就會報(bào)錯 2)[]和美圓$必須要,,否則M$可不認(rèn)這個賬 2、修改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,、導(dǎo)入導(dǎo)出 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 或BCP master..xp_cmdshell'bcp "serv-htjs.dbo.serv_user" out "c:\book2.xls" -c -q -S"." -U"sa" -P"sa"' 從Excel向SQLServer導(dǎo)入: select * into serv_user_bak from OpenRowSet('microsoft.jet.oledb.4.0','Excel 8.0;HDR=yes;database=c:\book1.xls;','select * from [Sheet1$]') 二,、SQLServer和Access1、查詢Access中數(shù)據(jù)的方法: select * from OpenRowSet('microsoft.jet.oledb.4.0',';database=c:\db2.mdb','select * from serv_user') 或 select * from OpenDataSource('Microsoft.Jet.OLEDB.4.0','Data Source="c:\DB2.mdb";User ID=Admin;Password=')...serv_user 2,、從SQLServer向Access寫數(shù)據(jù): 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"' 上面的區(qū)別主要是:OpenRowSet需要mdb和表存在,,BCP會在不存在的時候生成該mdb 3、從Access向SQLServer寫數(shù)據(jù):有了上面的基礎(chǔ),,這個就很簡單了 insert into SQLServer表 select * from OpenRowSet('microsoft.jet.oledb.4.0',';database=c:\db2.mdb','select * from Accee表') 或用BCP master..xp_cmdshell'bcp "serv-htjs.dbo.serv_user" in "c:\db3.mdb" -c -q -S"." -U"sa" -P"sa"' 4、刪除Access數(shù)據(jù): delete from OpenRowSet('microsoft.jet.oledb.4.0',';database=c:\db2.mdb','select * from serv_user') where lock=0 5,、修改Access數(shù)據(jù): update OpenRowSet('microsoft.jet.oledb.4.0',';database=c:\db2.mdb','select * from serv_user') set lock=1 SQLServer和Access大致就這么多,。 |
|