浠ヤ笅鍐呭鏉ヨ嚜http://hi.baidu.com/suofang/blog/item/a58bdd5829d5e583800a1812.html錛堝叾涓殑鍥劇墖鍙埌榪欎釜鍦板潃鏌ョ湅錛? ![]() 琛ㄧ粨鏋勫浘
![]() 琛ㄦ暟鎹?/div>
select * from tb_cus_area_cde --瀛愬彇鐖? select * from tb_cus_area_cde a CONNECT BY PRIOR a.c_snr_area=a.c_area_cde START WITH a.c_area_cde='1040101' --鐖跺彇瀛? select * from tb_cus_area_cde a CONNECT BY PRIOR a.c_area_cde=a.c_snr_area START WITH a.c_snr_area is null *************************鑷繁鎬葷粨 榪樻槸鐪婥SDN鍚э紝璧涜開鎬繪槸涓嶅ソ鐢?a target="_blank">http://writeblog.csdn.net/PostList.aspx********************************* ORACLE鏌ヨ鏍戝瀷鍏崇郴錛?/font>connect by prior start with錛?/font>
鏈夊涓嬭〃緇撴瀯錛?/font> create table TVideoSort ( FsysId NVARCHAR2(32 char) not null, FSortName NVARCHAR2(32 char), FSortTopId NVARCHAR2(32 char), FSortAddress number, FSortIsList char(1), constraint PK_TVIDEOSORT primary key (FsysId) ); 鏁版嵁濡備笅錛?/font> --瀛愭煡鐖訛紙閫氳繃瀛愯妭鐐瑰悜鏍硅妭鐐硅拷鏈?/font>.錛?/font> 鏌ヨ銆旂壒涓嬭竟銆曠殑鐖惰妭鐐?/font> select FsysId,FSortName,FSortTopId,FSortAddress,FSortIsList from TVideoSort start with FsysId='3703E5CD81E48D6BE040007F01001254'--榪欎釜鏄壒涓嬭竟鐨?/font>fsysid connect by prior FSortTopId=FsysId order by FsortAddress 緇撴灉 鈥曗曠埗鏌ュ瓙錛堥氳繃鏍硅妭鐐歸亶鍘嗗瓙鑺傜偣.錛?/font> 鏌ヨ銆旂壒涓嬭竟銆曠殑瀛愯妭鐐癸細緇撴灉 select FsysId,FSortName,FSortTopId,FSortAddress,FSortIsList from TVideoSort start with FsysId='3703E5CD81E48D6BE040007F01001254' --榪欎釜鏄壒涓嬭竟鐨?/font>fsysid connect by prior FsysId=FSortTopId order by FsortAddress 鏌ヨ銆旂壒闀跨粌涔犮曠殑瀛愯妭鐐癸細緇撴灉 select FsysId,FSortName,FSortTopId,FSortAddress,FSortIsList from TVideoSort start with FsysId='36AE4D68EC7D364CE040007F01000CA5' connect by prior FsysId=FSortTopId order by FsortAddress 鐗瑰埆娉ㄦ剰錛屼袱涓煡璇㈠彧鏄?/font>FsysId=FSortTopId涓嶅悓鑰屽凡錛屽嵆瀛愭煡鐖跺垯鎶?/font>FSortTopId鍋氫負涓昏〃鏀懼墠闈紝鐖舵煡瀛愬垯鎶?/font>FsysId鏀懼墠闈綔涓昏〃 |
姣斿錛屾垜鏌ヨ鐨勬潯浠惰兘榪斿洖100鏉¤褰曪紝鑰屾垜鍙鍙栧緱鍏朵腑鐨勭50~絎?0鏉★紝 聽
聽 聽 聽 璇烽棶濡備綍澶勭悊? 闂鐐規暟錛?0銆佸洖澶嶆鏁幫細4Top
鏌ヨ絎琗欏?姣忛〉Y鏉¤褰?聽
聽 聽
聽 鏈鍩烘湰鐨勫鐞嗘柟娉? 聽
聽 聽
聽 濡傛灉琛ㄤ腑鏈変富閿?璁板綍涓嶉噸澶嶇殑瀛楁涔熷彲浠?,鍙互鐢ㄧ被浼間笅闈㈢殑鏂規硶,褰撶劧x,y瑕佹崲鎴愬叿浣撶殑鏁板瓧,涓嶈兘鐢ㄥ彉閲? 聽
聽 聽
聽 select 聽 top 聽 y 聽 * 聽 from 聽 琛?聽 where 聽 涓婚敭 聽 not 聽 in(select 聽 top 聽 (x-1)*y 聽 涓婚敭 聽 from 聽 琛? 聽
聽 聽
聽 聽
聽 聽
聽 濡傛灉琛ㄤ腑鏃犱富閿?鍙互鐢ㄤ復鏃惰〃,鍔犳爣璇嗗瓧孌佃В鍐?榪欓噷鐨剎,y鍙互鐢ㄥ彉閲? 聽
聽 聽
聽 select 聽 id=identity(int,1,1),* 聽 聽 into 聽 #tb 聽 from 聽 琛?聽
聽 select 聽 * 聽 from 聽 #tb 聽 where 聽 id 聽 between 聽 (x-1)*y 聽 and 聽 x*y-1 聽
聽 聽
聽 Top
/*--鐢ㄥ瓨鍌ㄨ繃紼嬪疄鐜扮殑鍒嗛〉紼嬪簭 聽
聽 聽
聽 鏄劇ず鎸囧畾琛ㄣ佽鍥俱佹煡璇㈢粨鏋滅殑絎琗欏?聽
聽 瀵逛簬琛ㄤ腑涓婚敭鎴栨爣璇嗗垪鐨勬儏鍐?鐩存帴浠庡師琛ㄥ彇鏁版煡璇紝鍏跺畠鎯呭喌浣跨敤涓存椂琛ㄧ殑鏂規硶 聽
聽 濡傛灉瑙嗗浘鎴栨煡璇㈢粨鏋滀腑鏈変富閿?涓嶆帹鑽愭鏂規硶 聽
聽 濡傛灉浣跨敤鏌ヨ璇彞,鑰屼笖鏌ヨ璇彞浣跨敤浜唎rder 聽 by,鍒欐煡璇㈣鍙ュ繀欏誨寘鍚玹op 聽 璇彞 聽
聽 聽
聽 --閭瑰緩 聽 2003.09--*/ 聽
聽 聽
聽 /*--璋冪敤紺轟緥 聽
聽 exec 聽 p_show 聽 '鍦板尯璧勬枡' 聽
聽 聽
聽 exec 聽 p_show 聽 'select 聽 top 聽 100 聽 percent 聽 * 聽 from 聽 鍦板尯璧勬枡 聽 order 聽 by 聽 鍦板尯鍚嶇О',5,3,'鍦板尯緙栧彿,鍦板尯鍚嶇О,鍔╄鐮? 聽
聽 --*/ 聽
聽 聽
聽 if 聽 exists 聽 (select 聽 * 聽 from 聽 dbo.sysobjects 聽 where 聽 id 聽 = 聽 object_id(N'[dbo].[p_show]') 聽 and 聽 OBJECTPROPERTY(id, 聽 N'IsProcedure') 聽 = 聽 1) 聽
聽 drop 聽 procedure 聽 [dbo].[p_show] 聽
聽 GO 聽
聽 聽
聽 CREATE 聽 Proc 聽 p_show 聽
聽 @QueryStr 聽 nvarchar(4000), --琛ㄥ悕銆佽鍥懼悕銆佹煡璇㈣鍙?聽
聽 @PageSize 聽 int=10, --姣忛〉鐨勫ぇ灝?琛屾暟) 聽
聽 @PageCurrent 聽 int=1, --瑕佹樉紺虹殑欏?聽
聽 @FdShow 聽 nvarchar 聽 (4000)='', --瑕佹樉紺虹殑瀛楁鍒楄〃,濡傛灉鏌ヨ緇撴灉鏈夋爣璇嗗瓧孌?闇瑕佹寚瀹氭鍊?涓斾笉鍖呭惈鏍囪瘑瀛楁 聽
聽 @FdOrder 聽 nvarchar 聽 (1000)='' --鎺掑簭瀛楁鍒楄〃 聽
聽 as 聽
聽 declare 聽 @FdName 聽 nvarchar(250) --琛ㄤ腑鐨勪富閿垨琛ㄣ佷復鏃惰〃涓殑鏍囪瘑鍒楀悕 聽
聽 ,@Id1 聽 varchar(20),@Id2 聽 varchar(20) --寮濮嬪拰緇撴潫鐨勮褰曞彿 聽
聽 ,@Obj_ID 聽 int --瀵硅薄ID 聽
聽 --琛ㄤ腑鏈夊鍚堜富閿殑澶勭悊 聽
聽 declare 聽 @strfd 聽 nvarchar(2000) --澶嶅悎涓婚敭鍒楄〃 聽
聽 ,@strjoin 聽 nvarchar(4000) --榪炴帴瀛楁 聽
聽 ,@strwhere 聽 nvarchar(2000) --鏌ヨ鏉′歡 聽
聽 聽
聽 聽
聽 select 聽 @Obj_ID=object_id(@QueryStr) 聽
聽 ,@FdShow=case 聽 isnull(@FdShow,'') 聽 when 聽 '' 聽 then 聽 ' 聽 *' 聽 else 聽 ' 聽 '+@FdShow 聽 end 聽
聽 ,@FdOrder=case 聽 isnull(@FdOrder,'') 聽 when 聽 '' 聽 then 聽 '' 聽 else 聽 ' 聽 order 聽 by 聽 '+@FdOrder 聽 end 聽
聽 ,@QueryStr=case 聽 when 聽 @Obj_ID 聽 is 聽 not 聽 null 聽 then 聽 ' 聽 '+@QueryStr 聽 else 聽 ' 聽 ('+@QueryStr+') 聽 a' 聽 end 聽
聽 聽
聽 --濡傛灉鏄劇ず絎竴欏碉紝鍙互鐩存帴鐢╰op鏉ュ畬鎴?聽
聽 if 聽 @PageCurrent=1 聽
聽 begin 聽
聽 select 聽 @Id1=cast(@PageSize 聽 as 聽 varchar(20)) 聽
聽 exec('select 聽 top 聽 '+@Id1+@FdShow+' 聽 from 聽 '+@QueryStr+@FdOrder) 聽
聽 return 聽
聽 end 聽
聽 聽
聽 --濡傛灉鏄〃,鍒欐鏌ヨ〃涓槸鍚︽湁鏍囪瘑鏇存垨涓婚敭 聽
聽 if 聽 @Obj_ID 聽 is 聽 not 聽 null 聽 and 聽 objectproperty(@Obj_ID,'IsTable')=1 聽
聽 begin 聽
聽 select 聽 @Id1=cast(@PageSize 聽 as 聽 varchar(20)) 聽
聽 ,@Id2=cast((@PageCurrent-1)*@PageSize 聽 as 聽 varchar(20)) 聽
聽 聽
聽 select 聽 @FdName=name 聽 from 聽 syscolumns 聽 where 聽 id=@Obj_ID 聽 and 聽 status=0x80 聽
聽 if 聽 @@rowcount=0 --濡傛灉琛ㄤ腑鏃犳爣璇嗗垪,鍒欐鏌ヨ〃涓槸鍚︽湁涓婚敭 聽
聽 begin 聽
聽 if 聽 not 聽 exists(select 聽 1 聽 from 聽 sysobjects 聽 where 聽 parent_obj=@Obj_ID 聽 and 聽 xtype='PK') 聽
聽 goto 聽 lbusetemp --濡傛灉琛ㄤ腑鏃犱富閿?鍒欑敤涓存椂琛ㄥ鐞?聽
聽 聽
聽 select 聽 @FdName=name 聽 from 聽 syscolumns 聽 where 聽 id=@Obj_ID 聽 and 聽 colid 聽 in( 聽
聽 select 聽 colid 聽 from 聽 sysindexkeys 聽 where 聽 @Obj_ID=id 聽 and 聽 indid 聽 in( 聽
聽 select 聽 indid 聽 from 聽 sysindexes 聽 where 聽 @Obj_ID=id 聽 and 聽 name 聽 in( 聽
聽 select 聽 name 聽 from 聽 sysobjects 聽 where 聽 xtype='PK' 聽 and 聽 parent_obj=@Obj_ID 聽
聽 ))) 聽
聽 if 聽 @@rowcount>1 --媯鏌ヨ〃涓殑涓婚敭鏄惁涓哄鍚堜富閿?聽
聽 begin 聽
聽 select 聽 @strfd='',@strjoin='',@strwhere='' 聽
聽 select 聽 @strfd=@strfd+',['+name+']' 聽
聽 ,@strjoin=@strjoin+' 聽 and 聽 a.['+name+']=b.['+name+']' 聽
聽 ,@strwhere=@strwhere+' 聽 and 聽 b.['+name+'] 聽 is 聽 null' 聽
聽 from 聽 syscolumns 聽 where 聽 id=@Obj_ID 聽 and 聽 colid 聽 in( 聽
聽 select 聽 colid 聽 from 聽 sysindexkeys 聽 where 聽 @Obj_ID=id 聽 and 聽 indid 聽 in( 聽
聽 select 聽 indid 聽 from 聽 sysindexes 聽 where 聽 @Obj_ID=id 聽 and 聽 name 聽 in( 聽
聽 select 聽 name 聽 from 聽 sysobjects 聽 where 聽 xtype='PK' 聽 and 聽 parent_obj=@Obj_ID 聽
聽 ))) 聽
聽 select 聽 @strfd=substring(@strfd,2,2000) 聽
聽 ,@strjoin=substring(@strjoin,5,4000) 聽
聽 ,@strwhere=substring(@strwhere,5,4000) 聽
聽 goto 聽 lbusepk 聽
聽 end 聽
聽 end 聽
聽 end 聽
聽 else 聽
聽 goto 聽 lbusetemp 聽
聽 聽
聽 /*--浣跨敤鏍囪瘑鍒楁垨涓婚敭涓哄崟涓瀛楁鐨勫鐞嗘柟娉?-*/ 聽
聽 lbuseidentity: 聽
聽 exec('select 聽 top 聽 '+@Id1+@FdShow+' 聽 from 聽 '+@QueryStr 聽
聽 +' 聽 where 聽 '+@FdName+' 聽 not 聽 in(select 聽 top 聽 ' 聽
聽 +@Id2+' 聽 '+@FdName+' 聽 from 聽 '+@QueryStr+@FdOrder 聽
聽 +')'+@FdOrder 聽
聽 ) 聽
聽 return 聽
聽 聽
聽 /*--琛ㄤ腑鏈夊鍚堜富閿殑澶勭悊鏂規硶--*/ 聽
聽 lbusepk: 聽
聽 exec('select 聽 '+@FdShow+' 聽 from(select 聽 top 聽 '+@Id1+' 聽 a.* 聽 from 聽
聽 (select 聽 top 聽 100 聽 percent 聽 * 聽 from 聽 '+@QueryStr+@FdOrder+') 聽 a 聽
聽 left 聽 join 聽 (select 聽 top 聽 '+@Id2+' 聽 '+@strfd+' 聽 聽
聽 from 聽 '+@QueryStr+@FdOrder+') 聽 b 聽 on 聽 '+@strjoin+' 聽
聽 where 聽 '+@strwhere+') 聽 a' 聽
聽 ) 聽
聽 return 聽
聽 聽
聽 /*--鐢ㄤ復鏃惰〃澶勭悊鐨勬柟娉?-*/ 聽
聽 lbusetemp: 聽
聽 select 聽 @FdName='[ID_'+cast(newid() 聽 as 聽 varchar(40))+']' 聽
聽 ,@Id1=cast(@PageSize*(@PageCurrent-1) 聽 as 聽 varchar(20)) 聽
聽 ,@Id2=cast(@PageSize*@PageCurrent-1 聽 as 聽 varchar(20)) 聽
聽 聽
聽 exec('select 聽 '+@FdName+'=identity(int,0,1),'+@FdShow+' 聽
聽 into 聽 #tb 聽 from'+@QueryStr+@FdOrder+' 聽
聽 select 聽 '+@FdShow+' 聽 from 聽 #tb 聽 where 聽 '+@FdName+' 聽 between 聽 ' 聽
聽 +@Id1+' 聽 and 聽 '+@Id2 聽
聽 ) 聽
聽 聽
聽 GO 聽
聽 Top
鏀惰棌瀛樺偍榪囩▼鍏?a >Top
--琛ㄩ噷娌℃湁鑷瀛楁錛屾墠鍙互濡備笅 聽
聽 select 聽 identity(int,1,1) 聽 FID,* 聽 into 聽 #temp 聽 from 聽 tablename 聽
聽 go 聽
聽 select 聽 * 聽 from 聽 #temp 聽 where 聽 FID 聽 between 聽 50 聽 and 聽 60 聽
聽 go 聽
聽 drop 聽 table 聽 #temp