写了好半天的有史以来写过的最长的存储过程

本文分享了一个SQL存储过程的编写实例,并展示了如何将其简化为更高效的单条SQL语句,对比了两种方法的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

费尽心里写了好半天的有史以来写过的最长的存储过程,不是什么机密的东西,贴在这里,以资纪念,:)
 1None.gifCREATE PROCEDURE dbo.list_taoke    
 2None.gifAS
 3ExpandedBlockStart.gifContractedBlock.gif    /**//* SET NOCOUNT ON */ 
 4None.gif    delete from taokee_Temp
 5None.gif    
 6ExpandedBlockStart.gifContractedBlock.gif    declare @uid int;/**//*用户ID*/
 7ExpandedBlockStart.gifContractedBlock.gif    declare @exp_count decimal/**//*被淘的文章数量*/
 8ExpandedBlockStart.gifContractedBlock.gif    declare @persent decimal ;/**//*被淘率*/
 9ExpandedBlockStart.gifContractedBlock.gif    declare @info_count decimal;/**//*发布的文章数量*/
10None.gif    
11ExpandedBlockStart.gifContractedBlock.gif    /**//*以下循环*/
12None.gif    declare  cursor_Userid cursor  for
13None.gif        select author from info where (infolawid>0and (exp>=2group by author
14None.gif    open cursor_Userid
15None.gif    begin
16None.gif        FETCH NEXT FROM cursor_Userid
17None.gif        INTO @uid
18None.gif        WHILE @@FETCH_STATUS = 0
19None.gif        BEGIN
20None.gif            set @exp_count=select count(*from info where author=@uid and exp>2)
21None.gif            set @info_count = (select count(*from info where author=@uid )
22None.gif            set @persent = (@exp_count/@info_count)*100
23None.gif            insert into taokee_Temp(uid,exp_count,persent,info_count) values (@uid,@exp_count,@persent,@info_count)
24None.gif            FETCH NEXT FROM cursor_Userid
25None.gif            INTO @uid
26None.gif        END
27None.gif    end
28None.gif    close cursor_Userid
29None.gif    deallocate cursor_Userid    
30None.gif    RETURN
31None.gif
32None.gifGO
33None.gif


更新于: 2006年11月22日 下午
发现上面的存储过程可以被一句SQL取代,如下:
None.gifselect top 50 * from 
None.gif
None.gif
select  distinct author as uid ,info_count=(select count(0from info where info.author=t.author ),
None.gifexp_count
=(select count(0from info where info.author=t.author and exp>2)
None.gif 
from info t where infolawid>0  
None.gif
as x order by x.exp_count desc
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值