Function incorrectly returns zero.

  • I have a function that is supposed to get the next maximum value (plus 1) from a table. Here is the T-SQL:

    ALTER FUNCTION [dbo].[udfDtsGetNewTraceID]

    (

    )

    RETURNS INT

    AS

    BEGIN

    DECLARE @intNewTraceID INT

    SELECT @intNewTraceID =ISNULL(MAX(intDTSTrace_ID), 0) + 1

    FROM tblDTSTrace

    RETURN @intNewTraceID

    END

    There are thousands of records in this table with actual values in the field intDTSTrace_ID. Yet, this function sometimes returns zero. Can anyone explain why?

    Thanks in advance,

    Ray

  • post the DDL with some sample data. I am sure you can get HELP from here.

    karthik

  • Just so I understand. . . run this and tell me if it gives you the result you'd expect.

    DECLARE @testtable1 TABLE(

    id INT IDENTITY(1, 1),

    intdtstrace_id INT PRIMARY KEY CLUSTERED)

    INSERT INTO @testtable1

    (intdtstrace_id)

    VALUES ('1')

    INSERT INTO @testtable1

    (intdtstrace_id)

    VALUES ('2')

    INSERT INTO @testtable1

    (intdtstrace_id)

    VALUES ('3')

    INSERT INTO @testtable1

    (intdtstrace_id)

    VALUES ('4')

    SELECT Isnull(MAX(intdtstrace_id), 0) + 1

    FROM @testtable1


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • ray 78019 (5/17/2010)


    I have a function that is supposed to get the next maximum value (plus 1) from a table. Here is the T-SQL:

    ALTER FUNCTION [dbo].[udfDtsGetNewTraceID]

    (

    )

    RETURNS INT

    AS

    BEGIN

    DECLARE @intNewTraceID INT

    SELECT @intNewTraceID =ISNULL(MAX(intDTSTrace_ID), 0) + 1

    FROM tblDTSTrace

    RETURN @intNewTraceID

    END

    There are thousands of records in this table with actual values in the field intDTSTrace_ID. Yet, this function sometimes returns zero. Can anyone explain why?

    Thanks in advance,

    Ray

    A return value zero doesn't seem possible, unless the max value is -1.

  • --EDIT-- The more I look at your code, the more it looks right. Just tested it on my server and got the expected answer. So I suspect, the error is in your data.

    --EDIT 2-- I've tried lots of ways to replicate the '0' result and only come up with the previous mentioned "if the largest value is -1".


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply