3

我有这样的存储过程:

alter procedure [dbo].[Test]  @locid int as  begin
declare @Mns decimal , @dec decimal
select @dec= AVG( CONVERT(NUMERIC(18,2), DATEDIFF(MI,t.Paydate,t.DelDate) ))  FROM Transaction_tbl t WHERE Locid=@locid 
select HH=convert(decimal(10,1) ,@dec/60)  select @Mns=@dec%60
 select @dec=@dec/60   select mm=@Mns
  end

那是在两个记录中返回输出

HH
29.1

mm
6

我想在 1 行中获得输出。预期输出如下:

HH           mm
29.1          6  

我怎么能这样做?

4

3 回答 3

3

没有任何额外变量的解决方案 -

ALTER PROCEDURE [dbo].[test] 
@locid INT
AS BEGIN

     SELECT 
            mm = [dec] % 60
          , HH = CONVERT(DECIMAL(10, 1), [dec] / 60)
     FROM (
          SELECT [dec] = AVG(CONVERT(NUMERIC(18, 2), DATEDIFF(MI, t.Paydate, t.DelDate)))
          FROM dbo.Transaction_tbl t
          WHERE Locid = @locid
     ) t

END
于 2013-07-22T12:37:01.633 回答
2

也许在最后将 HH 和 mm 放在一个选择中:

    alter procedure [dbo].[Test]  @locid int as  begin
    declare @Mns decimal , @dec decimal
    select @dec= AVG( CONVERT(NUMERIC(18,2), DATEDIFF(MI,t.Paydate,t.DelDate) ))  FROM Transaction_tbl t WHERE Locid=@locid;
    -- select @Mns=@dec%60;
    -- select @dec=@dec/60;   
    select HH=convert(decimal(10,1) ,@dec/60), mm=@dec%60;
    end
于 2013-07-22T12:33:50.480 回答
1

试试这个:

Select
   @Mns As Mns,
   @HH As HH

您可以选择任何内容,只需用逗号分隔它们

Select
   'John'           As MyName,
   5*20             As MyCalc,
   MONTH(GetDate()) AS ThisMonth,
   @SomeVariable    As SomeVariable

输出

MyName    MyCalc    ThisMonth    SomeVariable
-----------------------------------------------
John      100       7            0
于 2013-07-22T12:37:27.063 回答