return UDF data to the user

  • Hi, I have recently discovered UDF’s.

    I understand that they cannot return data directly to the user.

    I am using the following UDF:

    CREATE FUNCTION dbo.udfGetClientIdPerActivity (@activityId INT)

    RETURNS INT

    AS

    BEGIN

    DECLARE @clientId INT

    SELECT @clientId= (SELECT clientId

    FROM tblActivity

    WHERE activityId = @activityId)

    RETURN ISNULL(@clientId, 0)

    END

    I currently call this UDF from a sProc and then return the result to the user. Is there a better way to achieve this?

    Cheers,

    yogi

  • This was removed by the editor as SPAM

  • You can use UDFs in the same places you can use built-in functions. For instance, I created:

    CREATE FUNCTION myTime(@theDate datetime)

    RETURNS varchar(30)

    AS

    BEGIN

    RETURN REPLACE(CONVERT(varchar,@theDate,106),' ','-')

    END

    If I then execute:

    select dbo.myTime(current_timestamp)

    in Query Analyzer, I get a single-column result set with one row, containing:

    05-Jun-2003

    So, you can return the results of your function to the user in the same way you can return the results of any existing SQL function to the user.

    RD Francis


    R David Francis

  • Hiya,

    thanks for the reply.

    It makes a joyous change to realise that I am on the right track for once 🙂

    Cheers,

    yogiberr

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

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