Apply function to the result of a select

  • Suppose I have a function that performs some mathematical operation on the input parameter x and returns a value y:

    select dbo.MyMathFunc(10)

    Is there a way to apply the function to the results of a select such as:

    select dbo.MyMathFunc(select MyInt from MyTable)

    Maybe I just can't come up with the correct syntax.

  • nano2nd (4/8/2010)


    Suppose I have a function that performs some mathematical operation on the input parameter x and returns a value y:

    select dbo.MyMathFunc(10)

    Is there a way to apply the function to the results of a select such as:

    select dbo.MyMathFunc(select MyInt from MyTable)

    Maybe I just can't come up with the correct syntax.

    This?

    declare @MyVariable as int;

    select @MyVariable = MyInt from dbo.MyTable; -- where this returns one row or the last value from multiple rows

    select dbo.MyMathFunc(@MyVariable);

  • Thanks, it worked as you expected. It applies the function to the first row returned from the select. How do I apply it iteratively to each of the rows in the table?

  • nano2nd (4/8/2010)


    Thanks, it worked as you expected. It applies the function to the first row returned from the select. How do I apply it iteratively to each of the rows in the table?

    That is what you asked for initially. Try this:

    select dbo.MyMathFunc(MyInt) from dbo.MyTable;

  • Thanks, that did it. I really appreciate the help.

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

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