4

Consider the following sql query:

SELECT TaskId,FromItemId, IHV_FROM.NodePath as FromPath
FROM VIEW_TASKS with (nolock) 
left Join fn_ITEM_HIERARCHY(FromItemId) IHV_FROM ON FromItemId = IHV_FROM.ItemId
WHERE ...

fn_ITEM_HIERARCHY is a function which takes one input parameter. I would like to use FromItemId from VIEW_TASKS as input parameter to the function. When doing it like above, i get the error (Microsoft SQL Server 2008) "Invalid column name".

The SELECT statement returns multiple values, so it is not possible to assign the value to a variable in a separate select statement. Any ideas?

0

4 Answers 4

2

You probably need to use OUTER APPLY in this case:

SELECT TaskId,FromItemId, IHV_FROM.NodePath as FromPath
FROM VIEW_TASKS with (nolock) 
OUTER APPLY fn_ITEM_HIERARCHY(FromItemId) IHV_FROM
WHERE ...
;

If the function always returns rows or if you only want results for values where the function does return rows, use CROSS APPLY instead.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes. I actually did. OUTER APPLY was the correct approach for me.
1

Use Cross apply as shown here

http://blog.sqlauthority.com/2008/01/03/sql-server-2005-last-ran-query-recently-ran-query/

Comments

0

use alias

SELECT t.TaskId, t.FromItemId, IHV_FROM.NodePath as FromPath
FROM VIEW_TASKS t with (nolock) 
left Join fn_ITEM_HIERARCHY(FromItemId) IHV_FROM ON t.FromItemId = IHV_FROM.ItemId

1 Comment

Sadly this oes not work. The intellisence dots the FromItemId within the paranthesis. (and the result is still invalid column name)
0

use OUTER APPLY instead of left join

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.