Scalar Function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Yogesh Sharma
    New Member
    • Mar 2008
    • 40

    Scalar Function

    FIRST EXAMPLE--CREATE FUNCTION dbo.myFunction( )
    RETURNS INT
    AS
    BEGIN
    DECLARE @myInt INT
    SET @myInt = 1
    RETURN @myInt
    END

    select dbo.myFunction( ) as 'Simple Number'

    This Function is not returning any value,Error is coming
    Cannot find either column "dbo" or the user-defined function or aggregate "dbo.myFunction ", or the name is ambiguous.

    SECOND EXAMPLE--
    create function dbo.add1(@num1 Int,@num2 Int)
    Returns INT
    as
    Begin
    Return(@num1+@n um2)
    End

    select dbo.add1(10,20) as result

    Above function is working fine,but dbo is optional IF I write the above Function without dbo
    create function add1(@num1 Int,@num2 Int)
    Returns INT
    as
    Begin
    Return(@num1+@n um2)
    End

    select add1(10,20) as result
    Then the Function is Contructed successfully , But when I call the function usimg Select statement
    Error is there--
    'add1' is not a recognized built-in function name.

    Plz reply whether its necessary to write owner name to prevent errors.
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    Originally posted by Yogesh Sharma
    FIRST EXAMPLE--CREATE FUNCTION dbo.myFunction( )
    RETURNS INT
    AS
    BEGIN
    DECLARE @myInt INT
    SET @myInt = 1
    RETURN @myInt
    END

    select dbo.myFunction( ) as 'Simple Number'

    This Function is not returning any value,Error is coming
    Cannot find either column "dbo" or the user-defined function or aggregate "dbo.myFunction ", or the name is ambiguous.

    SECOND EXAMPLE--
    create function dbo.add1(@num1 Int,@num2 Int)
    Returns INT
    as
    Begin
    Return(@num1+@n um2)
    End

    select dbo.add1(10,20) as result

    Above function is working fine,but dbo is optional IF I write the above Function without dbo
    create function add1(@num1 Int,@num2 Int)
    Returns INT
    as
    Begin
    Return(@num1+@n um2)
    End

    select add1(10,20) as result
    Then the Function is Contructed successfully , But when I call the function usimg Select statement
    Error is there--
    'add1' is not a recognized built-in function name.

    Plz reply whether its necessary to write owner name to prevent errors.

    During creation, by default, you will own the function. Since sql server can handle multiple objects with the same name as long as it's owned by different owners, you have to specify the who owns the function you are calling. So during creation, owner is optional. In function call, it is required.

    -- CK

    Comment

    • Yogesh Sharma
      New Member
      • Mar 2008
      • 40

      #3
      Thx Ck,
      dbo is d schems name.Its necessary to have dbo,cz we have the functions with the same name in different schemas.Me right or Wrong?

      Can u plz tell me how to create Schema in SQL.
      select dbo.myFunction( ) as 'Simple Number'

      Comment

      • ck9663
        Recognized Expert Specialist
        • Jun 2007
        • 2878

        #4
        I would assume you're referring to functions, not schema.

        Read the full syntax here

        --CK

        Comment

        Working...