MS SQL根据条件查询结果导出excel,Access

use gs_gw630
declare @sql varchar(8000)
declare @sql1 varchar(8000)
declare @sql2 varchar(8000)
declare @DateBigin int
declare @DateEnd int
declare @pageCode int
declare @AddrID varchar(1000)
declare @tableName varchar(1000)
declare @pdname varchar(1000)
set @AddrID='Addr002'
set @DateBigin=1241161274
set @DateEnd=1244098881
set @PageCode=1
set @PageCode=@PageCode*65535
set @tableName='qqqq'
set @pdname='E'
create table #table
(AddrId varchar(100))

;With RootNodeCTE(Id,ParentId) As (
Select AddrId,AddrParent From gs_AddressInfo Where AddrParent = @AddrID
Union All
Select gs_AddressInfo.AddrId,gs_AddressInfo.AddrParent From RootNodeCTE Inner Join gs_AddressInfo On RootNodeCTE.Id = gs_AddressInfo.AddrParent )

insert into #table  select ParentId from RootNodeCTE

 set @sql1=
'
create table '+@tableName+'(AddrName varchar(50),FixtureNumber smallint ,DoNode tinyint,DoTmInfo float ,DoHmInfo float ,DoDateTime varchar(100) )insert into '+@tableName+'(AddrName ,FixtureNumber ,DoNode ,DoTmInfo  ,DoHmInfo  ,DoDateTime  )  select  top 65535 AddrName,FixtureNumber  ,DoNode ,DoTmInfo  ,DoHmInfo  ,CONVERT(varchar, DATEADD(mi,DoDateTime / 60,''1970-01-01 00:00:00''), 120 ) as DoDateTime FROM   V_AddressData where DoDateTime not in (select top ('+cast(@PageCode as varchar(100))+') DoDateTime from V_AddressData  where (DoDateTime between   '+cast(@DateBigin as varchar(100))+' and '+cast(@DateEnd as varchar(100))+') and AddrId not in (select AddrId  from #table) order by DoDateTime)and (DoDateTime between '+str(@DateBigin)+' and '+str(@DateEnd)+') order by DoDateTime'

set @sql='bcp "select * from gs_gw630..'+@tableName+'" queryout c:/'+@tableName+'.xls -c -S"192.168.1.152" -U"sa" -P"sa"'
EXEC gs_gw630..xp_cmdshell @sql
set @sql2='
insert into openrowset(''microsoft.jet.oledb.4.0'', ''C:/aa.mdb'';''admin'';'''','+@tableName+')  select * from gs_gw630..'+@tableName+''
print @sql1
exec (@sql1)
if(@pdname='E')
 begin
print @sql
exec (@sql)
 end
else
 begin
print @sql2
exec (@sql2)
 end

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值