question

Felipe avatar image
Felipe asked

Function - Returns Table - SQL 2000

Friends,

I have a Function to returns a table. I can execute a function like following:

SELECT * 
FROM dbo.FN_Function(1)

I Want to Know if i can call a function in other select. For example:

SELECT ID_Table,
  dbo.FN_Function(ID_Table)
FROM [Table]

I need to do this but I can't.

Someone can help me?

sql-server-2000functionsuser-defined-function
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Matt Whitfield avatar image
Matt Whitfield answered

If you were on 2005+ you could use cross apply

Select id_table, fn.* from table cross apply dbo.fn_function(id_table)
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Håkan Winther avatar image
Håkan Winther answered

You can't return a table in the SELECT clause, but you can include the function in the FROM clause. (SQL 2005 syntax)

SELECT * FROM myTable t 
CROSS APPLY dbo.FN_Function(t.ID_Table)

You can only use a scalar valued function in the select clause or a subselect that will return only one row and one column.

the statement below will work if the function only returns one row:

SELECT *, (SELECT MyCol FROM DBO.FN_Function(t.ID_Table)) AS myFnCol FROM myTable t
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kev Riley avatar image
Kev Riley answered

You can call a function in the SELECT if it is a scalar valued function, not a table-valued function. In other words if the function returns a single value, you can. if it returns a table, then treat it as a table.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.