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

来源:互联网 发布:大乐透组合软件 编辑:程序博客网 时间:2024/05/18 02:26

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

原创粉丝点击