在执行存储过程时将数值转换为数据类型的算术溢出错误

时间:2013-08-05 06:22:23

标签: sql-server tsql

ALTER procedure [dbo].[performance]
    @startdate nvarchar(100),
    @enddate nvarchar(100)
as begin
set NOCOUNT on;
select l.LocName,
       v.Vtype,
       SUM(DATEDIFF(MI,t.Paydate,t.DelDate)) as TotalDiff,
       [dbo].[testfunction](CONVERT(decimal(10,1), AVG(
              CONVERT(NUMERIC(18,2), DATEDIFF(SS,t.Paydate,t.DelDate) ) )))  as Average
from
    Transaction_tbl t
        left join
    VType_tbl v
        on t.vtid=v.vtid
        left join
    Location_tbl l
        on t.Locid=l.Locid
where
    t.Locid in(select t1.Locid  from Transaction_tbl t1) and
    dtime between '' + @startdate +'' and ''+@enddate+'' and
    Status =5
group by v.Vtype,l.LocName,l.Locid order by l.Locid
end

我也有一个这样的功能:

ALTER FUNCTION [dbo].[testfunction] (@dec NUMERIC(18, 2)) RETURNS Varchar(50) 
AS
BEGIN
     DECLARE
     @hour integer,
          @Mns integer,
          @second decimal(18,3)

     DECLARE @Average  Varchar(50) 
     select @hour=CONVERT(int,@dec/60/60)
     SELECT @Mns = convert(int, (@dec / 60) - (@hour * 60 ));
     select @second=@dec % 60;

     SELECT @Average = 
            convert(varchar(9), convert(int, @hour)) + ':' +
        right('00' + convert(varchar(2), convert(int, @Mns)), 2) + ':' +
        right('00' + CONVERT(decimal(10,0), convert(varchar(6), @second)), 6)
     RETURN @Average     

END

我正在通过这样的日期:2013-01-01和2013-05-01 执行此操作时出现错误:将数值转换为数据类型varchar的算术溢出错误。

1 个答案:

答案 0 :(得分:0)

试试这个 -

ALTER PROCEDURE [dbo].[performance] 

    @startdate NVARCHAR(100), --<-- try to use date datatype - DATE, DATETIME, ...
    @enddate NVARCHAR(100)

AS
BEGIN
    SET NOCOUNT ON;

    SELECT
          l.LocName
        , v.Vtype
        , SUM(DATEDIFF(MI, t.Paydate, t.DelDate)) AS TotalDiff
        , [dbo].[testfunction](
            CONVERT(DECIMAL(10, 1), AVG(CONVERT(NUMERIC(18, 2), DATEDIFF(SS, t.Paydate, t.DelDate))))
        ) AS Average
    FROM dbo.Transaction_tbl t
    LEFT JOIN dbo.VType_tbl v ON t.vtid = v.vtid
    LEFT JOIN dbo.Location_tbl l ON t.Locid = l.Locid
    WHERE dtime BETWEEN 
                  CAST(@startdate AS DATETIME) 
                AND 
                  CAST(@enddate AS DATETIME) --<-- possible problem
        AND [status] = 5
        --AND t.Locid IN (SELECT t1.Locid FROM Transaction_tbl t1) --<-- unnessesary
    GROUP BY
          v.Vtype
        , l.LocName
        , l.Locid
    ORDER BY l.Locid

END

<强>更新

ALTER FUNCTION [dbo].[testfunction] 
(
    @dec NUMERIC(18, 2)
) 
RETURNS Varchar(50) 
AS BEGIN

     DECLARE
          @hour BIGINT
        , @Mns BIGINT
        , @second DECIMAL(18,3)

    SELECT
          @hour = CONVERT(BIGINT, @dec / 60 / 60)
        , @Mns = CONVERT(BIGINT, (@dec / 60) - (@hour * 60))
        , @second = @dec % 60

    RETURN 
        CONVERT(VARCHAR(50), CONVERT(BIGINT, @hour)) + ':' + --<-- VARCHAR(9) => VARCHAR(50)
        RIGHT('00' + CONVERT(VARCHAR(2), CONVERT(BIGINT, @Mns)), 2) + ':' +
        RIGHT('00' + CONVERT(DECIMAL(2, 0), CONVERT(VARCHAR(6), @second)), 6)     

END