create proc returnVoteResult
@qid varchar(50),
@qtype int,
@width int
as
begin
declare @otype int
declare @tmp int
if (@qtype=1 or @qtype=2)
begin
set @otype=1
end
else
begin
set @otype=2
endset @tmp=(select sum(selectcount) from FA_IBOX_Vote_Option where questionid=@qid and optiontype=@otype)select optiontitle,selectcount,(@width*(selectcount)/@tmp)as sumcnt from FA_IBOX_Vote_Option where questionid=@qid and optiontype=@otype ---这是我要返回的记录集end

解决方案 »

  1.   

    create function fnReturnCOteResult(@qid varchar(50),@qtype int,@width int)
    returns @Tabl table(optiontitle varchar(100),selectcount decimal(19,3),sumcnt decimal(18,3))
    as
    begin
    declare @otype int
    declare @tmp int if(@qtype=1 or @qtype=2)
    set @otype=1
    else
    set @otype=2 select @tmp=sum(selectcount) from FA_IBOX_Vote_Option where questionid=@qid and optiontype=@otype
    insert @Tabl select optiontitle,selectcount,@width*selectcount/@tmp from FA_IBOX_Vote_Option where questionid=@qid and optiontype=@otype 
    return
    end
      

  2.   

    create function f(
    @qid varchar(50),
    @qtype int,
    @width int
    )
    returns @re table(col1 varchar(100), col2 varchar(100), col3 varchar(100))
    as
    begin
    declare @otype int
    declare @tmp int
    if (@qtype=1 or @qtype=2)
    begin
    set @otype=1
    end
    else
    begin
    set @otype=2
    endset @tmp=(select sum(selectcount) from FA_IBOX_Vote_Option where questionid=@qid and optiontype=@otype)insert into @re select optiontitle, selectcount,(@width*(selectcount)/@tmp)as sumcnt
    from FA_IBOX_Vote_Option 
    where questionid=@qid and optiontype=@otype ---这是我要返回的记录集return 
    end
      

  3.   

    @re table(col1 varchar(100), col2 varchar(100), col3 varchar(100))
    col1, col2, col3 的数据类型应该与你查询出来的数据类型一致
      

  4.   

    谢谢楼上的几位,有个问题出来了,我查询的话用
    select fnReturnCOteResult('TPWT200310170952380323',1,500)
    执行会出错:'fnReturnCOteResult' 不是可以识别的 函数名。
    我又改函数为create function dbo.fnReturnCOteResult(....) 
    然后执行select dbo.fnReturnCOteResult('TPWT200310170952380323',1,500)
    也出错:对象名 'dbo.fnReturnCOteResult' 无效。怎么回事呢?如何解决啊?!
      

  5.   

    select dbo.fnReturnCOteResult('TPWT200310170952380323',1,500)--改一下:
    select * from dbo.fnReturnCOteResult('TPWT200310170952380323',1,500)--使用查表模式