现在有 表
a     1     100 
a     2     200 
b     1     100 
b     2     200 
b     3     300 
通过
declare @t table(a varchar(20),id1 int,id2 int)   
insert   into   @t   
select   'a',1,100   
union   all   
select   'a',2,200   
union     all   
select   'b',1,100   
union     all   
select     'b',2,200   
union     all   
select   'b',3,300   SELECT   a,   
    STUFF((SELECT   ','+cast(id1   as   varchar)+','+   cast(id2   as   varchar) AS [text)]           
           FROM   @t   AS   G2       
           WHERE   G2.a   =   G1.a   
           ORDER   BY   [id1]   
           FOR   XML   PATH('')),1,1,'')   AS   string   
FROM   @t   AS   G1   
GROUP   BY   a;
可以得到临时表@t
a   1,100,2,200
b   1,100,2,200,3,300但是现在我想在存储过程中调用这个临时表@t得到@t相同结果,怎么样实现?
因为我要在C#中调用存储过程的结果。
希望大家帮个忙

解决方案 »

  1.   

    改用 存储函数实现 可否 ,函数返回临时表变量即可了
    SET QUOTED_IDENTIFIER OFF 
    GO
    SET ANSI_NULLS OFF 
    GOALTER                                                           function CalcOneBureauLineExe(@subBureauID int,@datebgn char(12),@dateend char(12))
    returns @result table
            (
            button_id char(12),
            pointid int ,
            dev_id int ,
            bureau_id int ,
            line_id int ,
            lname varchar(50),
            linebelong int ,
            clicktime char(10),
            lstart varchar(50),
            lend varchar(50) 
            )
    AS
    begin         insert into @result         select d.button_id, tp.id, min(d.id) as id,d.bureau_id,l.id,min(l.name),l.belongto,max(nd.time) as time ,l.pt_begin,l.pt_end
                    from dev d 
                    inner join linedev ld 
                            on d.id=ld.devid and d.bureau_id=ld.bureauid                inner join line l
                            on l.id=ld.lineid and l.del_tag=0 and l.bureau_id=ld.bureauid
                    inner join toppoint tp
                           on tp.infoid=d.id and tp.bureauid=d.bureau_id and d.del_tag=0                left join nudity_data nd
                          on nd.button_id=d.button_id and nd.time>@datebgn and nd.time<@dateend         where d.bureau_id=@subbureauId 
             group by d.bureau_id,d.button_id,l.belongto,l.id,tp.id,l.pt_begin,l.pt_end         order by l.belongto,l.id         return
    end;
    GO
    SET QUOTED_IDENTIFIER OFF 
    GO
    SET ANSI_NULLS ON 
    GO改过即可以