SQL中读取Excel 以及 bpc语言


1    --开启导入功能
2     exec sp_configure 'show advanced options',1
3     reconfigure
4     exec sp_configure 'Ad Hoc Distributed Queries',1
5     reconfigure
6     --允许在进程中使用ACE.OLEDB.12
7     EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'AllowInProcess', 1
8     --允许动态参数
9     EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'DynamicParameters', 1
1 -- 如果不存在表 就是用 SELECT * INTO  表名 ...
2 -- 读取Excel数据,注意Excel必须事先关闭
3 SELECT *into Temp FROM OpenDataSource( 'Microsoft.ACE.OLEDB.12.0','Data Source="C:\Users\amanda\Desktop\新建 Microsoft Excel 工作表.xlsx";User ID=Admin;Password=;Extended properties=Excel 12.0')...[Sheet1$]  

   --如果数据库中有表  就是用 INSERT INTO 表名 ...
   INSERT INTO Temp1 SELECT * FROM OpenDataSource( 'Microsoft.ACE.OLEDB.12.0','Data Source="C:\Users\amandage\Desktop\新建 Microsoft Excel 工作表.xlsx";User ID=Admin;Password=;Extended properties=Excel 12.0')...[Sheet1$]

1 --读取access文件
2 select * from OpenRowSet('Microsoft.ACE.OLEDB.12.0',';database=C:\Users\amanda\Desktop\奔驰-全国报价1204.mdb','select * from content')
 1 --SQL写到Excel 
 2 --1.表必须存在
 3 --2.Excel表中需要有列名存在
 4 INSERT INTO  
 5 OPENROWSET('Microsoft.ACE.OLEDB.12.0','Excel 12.0;HDR=Yes;DATABASE=C:\Users\amanda\Desktop\新建 Microsoft Excel 工作表.xlsx',
 6             'SELECT V1 FROM [Sheet1$]')  
 7 SELECT [周次] FROM [dbo].[Table_1]
 8 
 9  --全部写入
10 INSERT INTO 
11 OPENROWSET('Microsoft.Ace.OleDb.12.0','Excel 12.0;DATABASE=C:\Users\amanda\Desktop\新建 Microsoft Excel 工作表.xlsx',
12             'SELECT * FROM [Sheet1$] ') 
13 SELECT * FROM [dbo].[Table_1] 
--使用bpc写出数据
--1.不要有换行, 不要有odb等
--2.写到远程Ip下
EXEC master..xp_cmdshell 
  'bcp "Select * from SG..[Table_1] " queryout C:\11.xls -c -S AMANDA-PC1 -T'  --”-T表示安全输出不需要账号密码“
GO

---写到本地excel 
--1.必须是xls格式
--2.等等
Exec master..xp_cmdshell 'bcp "Select * from SG..[Table_1]" queryout "C:\test2.xls" -c -T'

--写出到远程以及填写自己的服务器和密码账号
EXEC master..xp_cmdshell 
  'bcp "Select Distinct URL_SHPrice_Series from YMCUUUBI..QKDSKFDL_Spider" queryout \\10.16.**.**\SpiderURL\BasicInfo_SHPrice.txt -c -S 10.16.**.** -U 服务器登录名 -P 密码'  --黑色的是需要自己填写的。
GO
1 //使用完成后,关闭Ad Hoc Distributed Queries:
2 exec sp_configure 'Ad Hoc Distributed Queries',0
3 reconfigure
4 exec sp_configure 'show advanced options',0
5 reconfigure
原文地址:https://www.cnblogs.com/goodge/p/5137492.html