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

avatar image

Felipe
262 23 23 26

(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

avatar image

Matt Whitfield ♦♦
29.5k 62 66 88

(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

avatar image

Håkan Winther
16.5k 36 45 57

(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

avatar image

Kev Riley ♦♦
64k 48 61 81

(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

SQL Server Central

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

Topics:

x501
x68
x29

asked: Nov 10, 2009 at 10:05 AM

Seen: 1559 times

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

Copyright 2016 Redgate Software. Privacy Policy