Question

If I have a function like this

dbo.GetTable(@myInteger) 

can I select columns by name from the resulting table, and assign them to variables in a subsequent function in SQL Server 2005?

Or is it more efficient in the subsequent function to create a temporary table and populate it with the results of

dbo.GetTable(@myInteger) 

This way you only call the above function once instead of going something like

DECLARE @IntegerOne Integer = dbo.GetTable(@myInteger).Column1
DECLARE @IntegerTwo Integer = dbo.GetTable(@myInteger).Column2
DECLARE @IntegerThree Integer = dbo.GetTable(@myInteger).Column3

etc

Was it helpful?

Solution

If it is a table valued function you can treat it just like a table and you can do something like ....

DECLARE @IntegerOne Integer; 
DECLARE @IntegerTwo Integer;
DECLARE @IntegerThree Integer;

SELECT TOP 1                    --<-- To make sure only one row is returned
        @IntegerOne   = Column1
       ,@IntegerTwo   = Column2
       ,@IntegerThree = Column3
FROM dbo.GetTable(@myInteger)
--WHERE Clause if required  

OTHER TIPS

Not sure if I got your question correct, for table function this will work:

select
    @IntegerOne = Column1,
    @IntegerTwo = Column2,
    @IntegerThree = Column3
from dbo.GetTable(@myInteger)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top