Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

sql µ¼³ö£¬½â¾ösheet±í´óСÏÞÖÆ

CREATE proc sqlToMultiExcelFile
@sqlstr nvarchar(4000), --²éѯÓï¾ä,Èç¹û²éѯÓï¾äÖÐʹÓÃÁËorder by ,Çë¼ÓÉÏtop 100 percent
@primaryKey varchar(100),--·ÖÒ³Ö÷¼ü×Ö¶Î
@path nvarchar(1000), --Îļþ´æ·ÅĿ¼
@fname nvarchar(250), --ÎļþÃû
@sheetname varchar(250)='sheet1' --Òª´´½¨µÄ¹¤×÷±íÃû,ĬÈÏΪÎļþÃû
as
set nocount on
declare @err int,@src nvarchar(255),@out int,@desc nvarchar(255),@sheetCount int,@i int,@topCount int,@where varchar(1000),@from varchar(1000),@myWhere varchar(1000),@filename varchar(250)
declare @obj int,@constr nvarchar(1000),@sql varchar(8000),@fdlist varchar(8000),@totalCount int,@pageCount int
create table #tbMemory
(
recId int identity(1,1) primary key,
primaryKey varchar(50)
)
create table #pageTb(totalCount int)


set @from=substring(@sqlstr,charindex('from',@sqlstr)+5,len(@sqlstr)-charindex('from',@sqlstr)+1)


if charindex('where',@sqlstr)>0
set @where=substring(@sqlstr,charindex('where',@sqlstr)+6,len(@sqlstr)-charindex('where',@sqlstr)+1)
else
set @where=''


set @pageCount=65000
set @sql='select count(*) from ('+@sqlStr+') a'

insert into #pageTb execute (@sql)
select @totalCount=totalCount from #pageTb

insert into #tbMemory execute('select top '+@totalCount+' '+@primaryKey+' from '+@from)

--µÃ³öÒªµ¼³öµÄsheetÊýÁ¿
if @totalCount>@pageCount
set @sheetCount=@totalCount/@pageCount+1
else
set @sheetCount=1

--²ÎÊý¼ì²â
if isnull(@fname,'')='' set @fname='temp.xls'
if isnull(@sheetname,'')='' set @sheetname=replace(@fname,'.','#')

--¼ì²éÎļþÊÇ·ñÒѾ­´æÔÚ
if right(@path,1)<>'\' set @path=@path+'\'
create table #tb(a bit,b bit,c bit)



--´´½¨±íµÄSQL
declare @tbname sysname
set @tbname='##tmp_'+convert(varchar(38),newid())
set @sql='select top 1 * into ['+@tbname+'] from('+@sqlstr+') a'
--print @sql
exec(@sql)


select @sql='',@fdlist=''
select @fdlist=@fdlist+',['+a.name +']'
,@sql=@sql+',['+a.name+'] '
+ca


Ïà¹ØÎĵµ£º

PL/SQL³ÌÐòÉè¼Æ£¨ÓαêµÄʹÓÃ)

ΪÁË´¦Àí SQL Óï¾ä£¬ORACLE ±ØÐë·ÖÅäһƬ½ÐÉÏÏÂÎÄ( context area )µÄÇøÓòÀ´´¦ÀíËù±ØÐèµÄÐÅÏ¢£¬ÆäÖаüÀ¨Òª´¦ÀíµÄÐеÄÊýÄ¿£¬Ò»¸öÖ¸ÏòÓï¾ä±»·ÖÎöÒÔºóµÄ±íʾÐÎʽµÄÖ¸ÕëÒÔ¼°²éѯµÄ»î¶¯¼¯(active set)¡£
 ÓαêÊÇÒ»¸öÖ¸ÏòÉÏÏÂÎĵľä±ú( handle)»òÖ¸Õ롣ͨ¹ýÓα꣬PL/SQL¿ÉÒÔ¿ØÖÆÉÏÏÂÎÄÇøºÍ´¦ÀíÓï¾äʱÉÏÏÂÎÄÇø»á·¢ÉúЩʲôÊÂÇ ......

[ת]Éú³ÉÎÞ¼¶Ê÷(sqlº¯Êý)

--´¦ÀíʾÀý
--ʾÀýÊý¾Ý
create table tb(ID int,Name varchar(10),ParentID int)
insert tb select 1,'AAAA'    ,0
union all select 2,'BBBB'    ,0
union all select 3,'CCCC'    ,0
union all select 4,'AAAA-1'  ,1
union all select 5,'AAAA-2'  ,1
u ......

SQL SERVER¿ç·þÎñÆ÷²éѯ(SQL SERVER DBLINK)

--¿ç·þÎñÆ÷²éѯÈçÏ£º
SELECT a.*,b.stor_Name
from OPENROWSET('MSDASQL',
   'DRIVER={SQL Server};SERVER=tom;UID=sa;PWD=123',
   pubs.dbo.authors) AS a,stores b
ORDER BY a.au_lname, a.au_fname
--ÆäÖУ¬tomΪԶ³Ì·þÎñÆ÷Ãû£¬stores ÊDZ¾»úÊý¾Ý¿âpubsÖеıí
--ÐèҪעÒâµÄÊÇÈô¶þ¸ö±íÖÐµÄ ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ