August 10, 2007 at 2:24 am
hi
i have a function and i want to pass the parameter from a select query
like this
select ID from dbo.functionName ( select name from table where id=1)
i get this error
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'SELECT'.
Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near ')'
so its not possible to do that?
August 10, 2007 at 2:40 am
As far as I know, that isn't allowed. Something like this should work:
DECLARE
@name SYSNAME
SELECT
@name = [name]
FROM
WHERE
[id] = 1
SELECT
[ID]
FROM
dbo.functionName(@name)
John
August 10, 2007 at 5:14 am
August 10, 2007 at 5:38 am
I think you can do this in SQL 2005 using cross join or cross apply and pass field value to function, but at the same I think your function should return table instead of scalar value. Check BOL for more detail if you've got 2005.
August 10, 2007 at 7:45 am
The only thing you can pass to a function are scalar values... can't pass a query.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 10, 2007 at 7:48 am
August 10, 2007 at 3:56 pm
you could keep it scalar by doing something like this:
select ID, dbo.functionName (name) from table where id=1
Lowell
August 10, 2007 at 6:20 pm
Now, there's some common sense thinking outside the box... thanks, Lowell.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 14, 2007 at 7:33 am
cross apply(oops only 2005) But I'll leave the post.
From BOL
The APPLY operator allows you to invoke a table-valued function for each row returned by an outer table expression of a query. The table-valued function acts as the right input and the outer table expression acts as the left input. The right input is evaluated for each row from the left input and the rows produced are combined for the final output. The list of columns produced by the APPLY operator is the set of columns in the left input followed by the list of columns returned by the right input.
There are two forms of APPLY: CROSS APPLY and OUTER APPLY. CROSS APPLY returns only rows from the outer table that produce a result set from the table-valued function. OUTER APPLY returns both rows that produce a result set, and rows that do not, with NULL values in the columns produced by the table-valued function.
As an example, consider the following tables, Employees
and Departments
:
August 14, 2007 at 7:36 am
This works if the function returns a scalar, but not if it returns a table.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply