Function within a function

  • i am calling the dbo.mycustomertest with some code and its giving me a string output instead i need it to execute the next function which is inside the function

    -----------------------------------------------------------------------------

    create function dbo.mycustomertest (@code varchar(10))

    returns table

    as

    return

    select (case when @code= 'brm' then 'select * from

    dbo.employee ()'

    when @code='crm' then 'select * from dbo.myorder()'

    else 'unknown'

    end) as col1

    --------------------------------------------------

    select * from dbo.mycustomertest ('brm')

    Answer : select * from

    dbo.employee ()

    I want the output of the function- select * from dbo.employee()

    ----------------------------------------------------

    create function dbo.employee()

    returns table

    as

    return

    select * from employees

    ---------------------------------------------------

    create function dbo.myorders()

    returns table

    as

    return

    select * from orders

  • Change function definition to:

    create function dbo.mycustomertest (@code varchar(10))

    returns table

    as

    declare @sqlCmd nvarchar(4000)

    select @sqlCmd = (case when @code= 'brm' then 'select * from

    dbo.employee ()'

    when @code='crm' then 'select * from dbo.myorder()'

    else 'unknown'

    end)

    exec (@sqlCmd)

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

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