以及幾個函數OPENROWSET(),OPENQUERY(),OPENDATASOURCE()的使用
--類別:總結
--charry0110(曉風殘月)
--*遠程庫
select * from openrowset(''SQLOLEDB'',''遠程服務器名'';''用戶名'';''密碼'';''庫名.dbo.表名'')
insert into openrowset(''SQLOLEDB'',''BERRY'';''sa'';''sa'',tt.dbo.ALaw_BestbigPunish)(OneName) select ''c''--添加
select * from openrowset(''SQLOLEDB'',''BERRY'';''sa'';''sa'',tt.dbo.ALaw_FilePunish)--選擇
delete from openrowset(''SQLOLEDB'',''BERRY'';''sa'';''sa'',tt.dbo.ALaw_BestbigPunish)--刪除
update openrowset(''SQLOLEDB'',''BERRY'';''sa'';''sa'',tt.dbo.ALaw_BestbigPunish)--更新
set OneName=''b''
--注意''192.168.18.252'';''sa'';''abc''中間是";",而不是","。
select a.* from OPENQUERY(''SQLOLEDB'',''
SELECT a.* FROM OPENQUERY (''oradb'',''192.168.0.111'';''sa'';''sa'',庫名.dbo.表名) a
SELECT * FROM OPENDATASOURCE(''SQLOLEDB'',''Data Source=192.168.0.111;User ID=sa;PassWord=sa'').tt.dbo.ALaw_BestbigPunish
INSERT INTO OPENDATASOURCE(''SQLOLEDB'',''Data Source=192.168.0.111;User ID=sa;PassWord=sa'').tt.dbo.ALaw_FilePunish(CaseName) select Num from aa.dbo.ydy
select * from OPENROWSET(''SQLOLEDB'',''192.168.0.111'';''sa'';''sa'', ''select * from tt.dbo.ALaw_FilePunish'')
insert into OPENROWSET(''SQLOLEDB'',''192.168.0.111'';''sa'';''sa'', ''select CaseName from tt.dbo.ALaw_FilePunish'') SELECT Num FROM aa.dbo.ydy
--第二個參數webconfig連接字符串可以使用Delphi、visual studio等開發工具中的ADO控件自動生成相應的連接字符串
--*Excel
select a.* from OpenRowSet(''Microsoft.Jet.OLEDB.4.0'',''Excel 8.0;DataBase=D:\test.xls'',sheet1$) a
select a.* into ydy from OpenRowSet(''Microsoft.Jet.OLEDB.4.0'',''Excel 8.0;DataBase=D:\test.xls'',sheet1$) a
SELECT a.* FROM OpenDataSource(''Microsoft.Jet.OLEDB.4.0'',''Data Source= "D:\test.xls";Extended propertIEs=Excel 8.0'')...sheet1$ a
select * from ydy
--注意sheet1$是否正確
--*Access
SELECT * FROM OPENROWSET(''Microsoft.Jet.OLEDB.4.0'',''D:\db.mdb'';''admin'';'''',''SELECT * FROM vote'')
''Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\db.mdb;Persist Security Info=False'')...vote
--*txt
select * from OPENROWSET(''MICROSOFT.JET.OLEDB.4.0'',''Text;DATABASE=D:\'',[sopendata#txt])
select * from OPENDATASOURCE(''MICROSOFT.JET.OLEDB.4.0'',''Text;DATABASE=D:\'')...[sopendata#txt]
delete from OPENROWSET(''MICROSOFT.JET.OLEDB.4.0'',''Text;DATABASE=D:\'', [sopendata#txt])
/*ID,Name,Num
1,Jim,3
2,Kate,4
3,Tom,5*/
--在這裡文件名中的.要使用#代替,在向文本導出時,不僅文本文件要存在,而且第一行必須和要導出表的字段一至
--非SQL Server數據庫在OPENDATASOURCE(...)後面引用數據庫中的表時使用"...”,
insert into OPENROWSET(''MICROSOFT.JET.OLEDB.4.0'',''Text;DATABASE=D:\'',[sopendata#txt])
select * from t1
--*sql不支持導入Word
--解決一:文本文件,可惜這樣是要求這些數據排版得有規律。
--解決二:一般是先拷貝進Excel,然後再導入sqlserver的
----bcp
--假設要從硬盤文件c:\DT.''txt''中導入文件,則如下代碼實現:
EXEC master..xp_cmdshell ''bcp "aa.dbo.ydy" in D:\dbo.txt -c -charry -sa -sa''
--假設導出文件到c:\DT.''txt'',則如下代碼實現:
EXEC master..xp_cmdshell ''bcp "aa.dbo.ydy" out c:\DT.txt -c -Sservername -
EXEC master..xp_cmdshell ''bcp "Select * from dbname..tablename"
queryout c:\DT.txt -c -Sservername -