您的位置:首页 > 编程语言 > C语言/C++

SQL中读取Excel 以及 bpc语言

2016-01-17 17:30 489 查看

   --开启导入功能
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure
--允许在进程中使用ACE.OLEDB.12
EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'AllowInProcess', 1
--允许动态参数
EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'DynamicParameters', 1


-- 如果不存在表 就是用 SELECT * INTO  表名 ...
-- 读取Excel数据,注意Excel必须事先关闭
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$]

--读取access文件
select * from OpenRowSet('Microsoft.ACE.OLEDB.12.0',';database=C:\Users\amanda\Desktop\奔驰-全国报价1204.mdb','select * from content')


 --SQL写到Excel
2 --1.表必须存在
3 --2.Excel表中需要有列名存在
INSERT INTO
OPENROWSET('Microsoft.ACE.OLEDB.12.0','Excel 12.0;HDR=Yes;DATABASE=C:\Users\amanda\Desktop\新建 Microsoft Excel 工作表.xlsx',
'SELECT V1 FROM [Sheet1$]')
SELECT [周次] FROM [dbo].[Table_1]

--全部写入
INSERT INTO
OPENROWSET('Microsoft.Ace.OleDb.12.0','Excel 12.0;DATABASE=C:\Users\amanda\Desktop\新建 Microsoft Excel 工作表.xlsx',
'SELECT * FROM [Sheet1$] ')
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


//使用完成后,关闭Ad Hoc Distributed Queries:
exec sp_configure 'Ad Hoc Distributed Queries',0
reconfigure
exec sp_configure 'show advanced options',0
reconfigure
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: