有人帮忙写了个存储过程,执行的结果是一张表。但是存储结果不能查询,想发满哪位高手帮忙该成视图或是函数之类的,能查询结果的类型(需要传参)。谢谢,如下:
CREATE proc SelfComIDContent
@comId int 
as 
select c.*,(c.selfReal/c.dutyShould)as selfRate, p.pName from 
(select a.selfID,a.idxNum,a.pID,a.idxID,a.perid,a.selfReason,a.selflevel,a.selfif,idxitem,idxss,dutycharge ,dutyShould as dutyShould,
(select [dbo].[f_selfComIDNow](a.idxNum,@comId))as selfReal 
from [dbo].[selfcheck] a inner join [dbo].[dutydis] b on a.dutyID=b.dutyID join indexs i on a.idxid=i.idxid  where a.pid  in (select pID from person where comID=@comID))c join person p on c.pID=p.pId 
where c.perID=(select top 1 perid from period order by perID desc)  order by idxid asc
GO

解决方案 »

  1.   

    --函数
    CREATE function fn_SelfComIDContent(@comId int)
    returns table
    as
    return(
    select c.*,(c.selfReal/c.dutyShould)as selfRate, p.pName from
    (select a.selfID,a.idxNum,a.pID,a.idxID,a.perid,a.selfReason,a.selflevel,a.selfif,idxitem,idxss,dutycharge ,dutyShould as dutyShould,
    (select [dbo].[f_selfComIDNow](a.idxNum,@comId))as selfReal
    from [dbo].[selfcheck] a inner join [dbo].[dutydis] b on a.dutyID=b.dutyID join indexs i on a.idxid=i.idxid where a.pid in (select pID from person where comID=@comID))c join person p on c.pID=p.pId
    where c.perID=(select top 1 perid from period order by perID desc) order by idxid asc)
    GO
      

  2.   

    --更正
    CREATE function fn_SelfComIDContent(@comId int)
    returns table
    as
    return(
    select c.*,(c.selfReal/c.dutyShould)as selfRate, p.pName from
    (select a.selfID,a.idxNum,a.pID,a.idxID,a.perid,a.selfReason,a.selflevel,a.selfif,idxitem,idxss,dutycharge ,dutyShould as dutyShould,
    (select [dbo].[f_selfComIDNow](a.idxNum,@comId))as selfReal
    from [dbo].[selfcheck] a inner join [dbo].[dutydis] b on a.dutyID=b.dutyID join indexs i on a.idxid=i.idxid where a.pid in (select pID from person where comID=@comID))c join person p on c.pID=p.pId
    where c.perID=(select top 1 perid from period order by perID desc))
    -- order by idxid asc)
    -- 这里不能用order by了
    GO--调用
    declare @i int
    set @i = 1
    select * from dbo.fn_SelfComIDContent(@i)
      

  3.   

    Limpire(昨夜小楼):还有个问题,我那个存储过程中的f_selfComIDNow也是个函数,执行的时候报错-除非同时指定了 TOP,否则 ORDER BY 子句在视图、内嵌函数、派生表和子查询中无效。
    ??
      

  4.   

    --调用的时候,你想怎么排序就怎么排序
    declare @i int
    set @i = 1
    select * from dbo.fn_SelfComIDContent(@i) order by idxid asc
      

  5.   

    呵呵,页head:回复 | 推荐 | 收藏 | 专题 | 公告 | 管理  | 关闭窗口管理就行了。