2

I Have a table-valued function which return 2 columns and 1 row. I would like to use those values in the select statement. Kindly help me in fetching the value from table.

Edit (From OP's comments)

something like

select a,b (select c1 from MyTableFunction(Param1, Param2)) as c, 
           (select c2 from MyTableFunction(Param1, Param2)) as d 
from main_table
1
  • 1
    Post what you have got already. Also, what platform are you using? Commented Feb 13, 2014 at 14:49

2 Answers 2

2

If its Sql Server, this is as simple as:

SELECT Col1, Col2 
FROM Schema.MyTableFunction(Param1, Param2);

Edit

If the parameters passed to the Function are constants / independent of the other joined tables, you can use a CROSS JOIN to it, alias it and use the result columns.

SELECT a,b, X.Col1 as c, X.Col2 as d 
FROM main_table mt
 CROSS JOIN MyTableFunction(123, 'SomeConstant') AS X;

However, if you need to pass in columns from a table into a table valued function during a join, you need to use CROSS APPLY

SELECT a,b, X.Col1 as c, X.Col2 as d 
FROM main_table mt
 CROSS APPLY MyTableFunction(mt.Col1, mt.Col2) AS X;
Sign up to request clarification or add additional context in comments.

3 Comments

i need to use in another select Query, something like select a,b (select c1 from MyTableFunction(Param1, Param2)) as c, (select c2 from MyTableFunction(Param1, Param2)) as d from main_table
i need to use it in another select Query, something like select a,b (select c1 from MyTableFunction(Param1, Param2)) as c, (select c2 from MyTableFunction(Param1, Param2)) as d from main_table
@Rajakrishnan: Still we don't know your database platform.
0

In SQL Server just put the function name in the from. So:

select column1
,      column2
from   dbo.udFunctionName('xyz')

In Oracle use:

select column1
,      column2
from   table(functionName('xyz'))

1 Comment

I SQL Query . i need to use it in another select Query, something like select a,b (select c1 from MyTableFunction(Param1, Param2)) as c, (select c2 from MyTableFunction(Param1, Param2)) as d from main_table

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.