2

I have a TVF that takes a userID parameter.

I have another table that contains a list of these userIDs.

I want to pass all these userID values into the TVF.

I have tried this code...

    FROM [dbo].[UserData](u.UserID) data
    JOIN dbo.Users u ON u.DetailID = data.DetailID

but get the error...

The multi-part identifier "u.UserID" could not be bound.

What am I missing?

K09
  • 1,394
  • 13
  • 36
  • 59
  • 6
    FROM dbo.Users AS u CROSS APPLY dbo.UserData(u.UserID) AS data. You cannot reference u.UserID before joining to the table. – Mark Sinkinson Mar 31 '15 at 09:17

1 Answers1

2

The error you're getting is that you are using an alias that is defined after the table you're currently referencing. In this instance, you will need to change the order of your tables as mentioned by Mark in the comment.

You can use a CROSS APPLY which is more similar to an INNER JOIN, or you can use an OUTER APPLY which is more similar to an OUTER JOIN. Based on the syntax of your query above, my guess is that you are wanting a CROSS APPLY.

To echo Mark,

FROM dbo.Users AS u
CROSS APPLY dbo.UserData(u.UserID) AS data
John
  • 471
  • 2
  • 8