zoukankan      html  css  js  c++  java
  • 我写的存储过程

    --存储过程:获得最新10条研报信息 
    ---2011-11-11 修改 lp,未能取到最新的研报 
    IF exists (select * from SysObjects where name='sp_reportlist' and type='p')
       drop procedure sp_reportlist
    go
     
    CREATE PROCEDURE sp_reportlist
    @stockcode nvarchar(20)
    as
    if(@stockcode!='')
    begin
     
    SELECT top 10 T1.ReportID,T1.StockCode,T2.ReportTitle,T3.SourceID,T3.CompanyName,T3.CompanyShortName,  
    CASE  
    WHEN T1.Grade=0 THEN '不评级' WHEN T1.Grade=1 THEN '买入'  WHEN T1.Grade=2 THEN '增持' WHEN T1.Grade=3 THEN '减持'  
    WHEN T1.Grade=4 THEN '卖出' WHEN T1.Grade=5 THEN '中性' END AS Grade, 
     CASE 
    WHEN T1.GradeUpdate=0 THEN '--' WHEN T1.GradeUpdate=1 THEN '维持' WHEN T1.GradeUpdate=2 THEN '调低' 
    WHEN T1.GradeUpdate=3 THEN '调高'  END AS GradeUpdate,
    T1.TargetPrice,T2.ReportDate  FROM    StockReview T1,Report T2,Company T3  where (T1.stockcode=@stockcode or T1.stockname=@stockcode)  and T1.ReportID = T2.ReportID   
    and T2.CompanyID = T3.SourceID    order by T2.ReportDate desc
    end
    else   --取全部数据的前10条
    begin
    SELECT top 10 T1.ReportID,T1.StockCode,T2.ReportTitle,T3.SourceID,T3.CompanyName,T3.CompanyShortName,  
    CASE  
    WHEN T1.Grade=0 THEN '不评级' WHEN T1.Grade=1 THEN '买入'  WHEN T1.Grade=2 THEN '增持' WHEN T1.Grade=3 THEN '减持'  
    WHEN T1.Grade=4 THEN '卖出' WHEN T1.Grade=5 THEN '中性' END AS Grade, 
     CASE 
    WHEN T1.GradeUpdate=0 THEN '--' WHEN T1.GradeUpdate=1 THEN '维持' WHEN T1.GradeUpdate=2 THEN '调低' 
    WHEN T1.GradeUpdate=3 THEN '调高'  END AS GradeUpdate,
    T1.TargetPrice,T2.ReportDate  FROM    StockReview T1,Report T2,Company T3  where T1.ReportID = T2.ReportID   
    and T2.CompanyID = T3.SourceID    order by T2.ReportDate desc
    end
    GO
     
    ----存储过程:通过stockcode获得所有研报信息
    -- 如果stockcode为空字符串,则返回前所有数据
    IF exists (select * from SysObjects where name='sp_allreportlist' and type='p')
       drop procedure sp_allreportlist
    go
    CREATE PROCEDURE sp_allreportlist
    @stockcode nvarchar(20)
    as
      if(@stockcode!='')
    begin
    SELECT   T1.ReportID,T1.StockCode,T2.ReportTitle,T3.SourceID,T3.CompanyName,T3.CompanyShortName,  
    CASE  
    WHEN T1.Grade=0 THEN '不评级' WHEN T1.Grade=1 THEN '买入'  WHEN T1.Grade=2 THEN '增持' WHEN T1.Grade=3 THEN '减持'  
    WHEN T1.Grade=4 THEN '卖出' WHEN T1.Grade=5 THEN '中性' END AS Grade, 
     CASE 
    WHEN T1.GradeUpdate=0 THEN '--' WHEN T1.GradeUpdate=1 THEN '维持' WHEN T1.GradeUpdate=2 THEN '调低' 
    WHEN T1.GradeUpdate=3 THEN '调高'   END AS GradeUpdate,
    T1.TargetPrice,T2.ReportDate  FROM (select   ReportID,StockCode,Grade,GradeUpdate,TargetPrice from  StockReview where stockcode=@stockcode or stockname=@stockcode) T1 left join Report T2 on  T1.ReportID = T2.ReportID  left join Company T3 
    on T2.CompanyID = T3.SourceID      order by T2.ReportDate desc
    end
    else
    begin
     SELECT   T1.ReportID,T1.StockCode,T2.ReportTitle,T3.SourceID,T3.CompanyName,T3.CompanyShortName, 
     CASE   WHEN T1.Grade=0 THEN '不评级'   WHEN T1.Grade=1 THEN '买入'  WHEN T1.Grade=2 THEN '增持'  
    WHEN T1.Grade=3 THEN '减持'   WHEN T1.Grade=4 THEN '卖出'  WHEN T1.Grade=5 THEN '中性' END AS Grade,  
     CASE  WHEN T1.GradeUpdate=0 THEN '--'   WHEN T1.GradeUpdate=1 THEN '维持'  WHEN T1.GradeUpdate=2 THEN '调低'  
     WHEN T1.GradeUpdate=3 THEN '调高'   END AS GradeUpdate,  T1.TargetPrice,T2.ReportDate 
    FROM StockReview T1 , Report T2,Company T3  
    where  T1.ReportID = T2.ReportID and T2.CompanyID = T3.SourceID     
    order by T2.Reportdate desc 
     
    end
     GO
     
     
    ---存储过程:通过stockcode获得所有研报信息的条数
    IF exists (select * from SysObjects where name='sp_allreportcount' and type='p')
       drop procedure sp_allreportcount
    go
    CREATE PROCEDURE sp_allreportcount
    @stockcode nvarchar(20)
    as
      if(@stockcode!='')
    begin
    SELECT   count(T1.ReportID)  FROM (select   ReportID,StockCode,Grade,GradeUpdate,TargetPrice from  StockReview where stockcode=@stockcode or stockname=@stockcode) T1 left join Report T2 on  T1.ReportID = T2.ReportID  left join Company T3 
    on T2.CompanyID = T3.SourceID      
    end
    else
    begin
     SELECT count( T1.ReportID)   FROM StockReview T1 , Report T2,Company T3  
    where  T1.ReportID = T2.ReportID and T2.CompanyID = T3.SourceID     
    end
     GO
     
    exec sp_allreportcount '000001'
     
     
    exec  sp_allreportlist ''  --执行存储过程
     
     
     
    exec  sp_reportpagelist '000001' ,10,10
    ---存储过程:分页,获得第@endrecord-@num+1条到第@endrecord条记录的研报信息
    IF exists (select * from SysObjects where name='sp_reportpagelist' and type='p')
       drop procedure sp_reportpagelist
    go
     
    CREATE PROCEDURE sp_reportpagelist
    @stockcode nvarchar(20),
    @num int,   --条数
    @endrecord int   --从0到该页最后一条记录的总记录数
    as
    if(@stockcode!='')  --如果@stockcode不为空
    begin
    select * from ( select top (@num) * from (
    SELECT    top (@endrecord)  T1.ReportID,T1.StockCode,T2.ReportTitle,T3.SourceID,T3.CompanyName,T3.CompanyShortName,  
    CASE  
    WHEN T1.Grade=0 THEN '不评级' WHEN T1.Grade=1 THEN '买入'  WHEN T1.Grade=2 THEN '增持' WHEN T1.Grade=3 THEN '减持'  
    WHEN T1.Grade=4 THEN '卖出' WHEN T1.Grade=5 THEN '中性' END AS Grade, 
     CASE 
    WHEN T1.GradeUpdate=0 THEN '--' WHEN T1.GradeUpdate=1 THEN '维持' WHEN T1.GradeUpdate=2 THEN '调低' 
    WHEN T1.GradeUpdate=3 THEN '调高'   END AS GradeUpdate,
    T1.TargetPrice,T2.ReportDate  FROM 
    (select  ReportID,StockCode,Grade,GradeUpdate,TargetPrice from  StockReview where stockcode=@stockcode or stockname=@stockcode) T1 
    left join Report T2 on  T1.ReportID = T2.ReportID  left join Company T3 
    on T2.CompanyID = T3.SourceID      order by T2.ReportDate desc
    ) TT  order by TT.ReportDate) TTT order by TTT.ReportDate desc 
    end
    else  --获得所有的研报数据
    begin
    select * from ( select top (@num) * from (
    SELECT    top (@endrecord)  T1.ReportID,T1.StockCode,T2.ReportTitle,T3.SourceID,T3.CompanyName,T3.CompanyShortName,  
    CASE  
    WHEN T1.Grade=0 THEN '不评级' WHEN T1.Grade=1 THEN '买入'  WHEN T1.Grade=2 THEN '增持' WHEN T1.Grade=3 THEN '减持'  
    WHEN T1.Grade=4 THEN '卖出' WHEN T1.Grade=5 THEN '中性' END AS Grade, 
     CASE 
    WHEN T1.GradeUpdate=0 THEN '--' WHEN T1.GradeUpdate=1 THEN '维持' WHEN T1.GradeUpdate=2 THEN '调低' 
    WHEN T1.GradeUpdate=3 THEN '调高'   END AS GradeUpdate,
    T1.TargetPrice,T2.ReportDate  FROM 
    (select  ReportID,StockCode,Grade,GradeUpdate,TargetPrice from  StockReview) T1 
    left join Report T2 on  T1.ReportID = T2.ReportID  left join Company T3 
    on T2.CompanyID = T3.SourceID      order by T2.ReportDate desc
    ) TT  order by TT.ReportDate) TTT order by TTT.ReportDate desc 
     
    end
     GO

    作者:沐雪
    文章均系作者原创或翻译,如有错误不妥之处,欢迎各位批评指正。本文版权归作者和博客园共有,如需转载恳请注明。
    如果您觉得阅读这篇博客让你有所收获,请点击右下方【推荐】
    找一找教程网-随时随地学软件编程 http://www.zyiz.net/

  • 相关阅读:
    Petapoco 数据库操作类
    .net Cookie操作公共方法 CookieHelper
    .net 服务端缓存 Cache/CacheHelper
    Base64加密解密方法
    对多字段进行去重 ( Linq 方式 )
    BootStrap之 提示工具(Tooltip)插件
    bootstrap之弹出框
    Jquery 强大的表单验证操作
    强大的数据库工具 dbForge Studio ForMySql
    My操作小技巧
  • 原文地址:https://www.cnblogs.com/puzi0315/p/2644845.html
Copyright © 2011-2022 走看看