x
login about faq Site discussion (meta-askssc)

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?

more ▼

asked Nov 10 '09 at 10:05 AM in Default

Felipe gravatar image

Felipe
262 21 23 23

(comments are locked)
10|1200 characters needed characters left

3 answers: sort voted first

If you were on 2005+ you could use cross apply

Select id_table, fn.* from table cross apply dbo.fn_function(id_table)
more ▼

answered Nov 10 '09 at 10:47 AM

Matt Whitfield gravatar image

Matt Whitfield ♦♦
29.2k 56 63 87

(comments are locked)
10|1200 characters needed characters left

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
more ▼

answered Nov 10 '09 at 10:57 AM

Håkan Winther gravatar image

Håkan Winther
15k 29 35 46

(comments are locked)
10|1200 characters needed characters left

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.

more ▼

answered Nov 10 '09 at 10:23 AM

Kev Riley gravatar image

Kev Riley ♦♦
46.1k 39 43 69

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments



Facebook logo Follow Ask SSC on Facebook
Find Ask SSC on Google+
linkedin logo Find us on LinkedIn

Topics:

x455
x49
x27

asked: Nov 10 '09 at 10:05 AM

Seen: 998 times

Last Updated: Nov 11 '09 at 08:51 AM

Copyright © 2002-2012 Simple Talk Publishing. All Rights Reserved. If you have any queries, please contact the site administrators.
Ask SQL Server Central is a community service provided by Red Gate.