x

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, 2009 at 10:05 AM in Default

Felipe gravatar image

Felipe
262 23 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, 2009 at 10:47 AM

Matt Whitfield gravatar image

Matt Whitfield ♦♦
29.5k 61 65 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, 2009 at 10:57 AM

Håkan Winther gravatar image

Håkan Winther
15.8k 35 37 48

(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, 2009 at 10:23 AM

Kev Riley gravatar image

Kev Riley ♦♦
54.1k 47 49 76

(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.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

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

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x476
x58
x27

asked: Nov 10, 2009 at 10:05 AM

Seen: 1407 times

Last Updated: Nov 11, 2009 at 08:51 AM