2

I cannot get this type of select query (pseudo-code) to work. The UDF returns a table with 8 columns in a single row for a given 'UID_VEHICLE'. It works perfectly when the 'UID_VEHICLE' is provided as a constant like 3308. But I need one row of these function-results for each vehicle for a given customer -- up to 100 rows to be returned.

SELECT 
    * 
FROM
    [dbo].[fnGetNextDOT_InspectionData](UID_VEHICLE)
WHERE  
    UID_VEHICLE IN (SELECT UID_VEHICLE 
                    FROM tVEHICLES 
                    WHERE UID_CUSTOMER = 88);

Your comments and solutions are welcome...thanks...John

1 Answer 1

2

When passing row values from a query into a TVF, you need to use CROSS APPLY or OUTER APPLY (starting with SQL Server 2005):

SELECT   * -- or dot.*, or whatever is desired
FROM     tVEHICLES veh
CROSS APPLY [dbo].[fnGetNextDOT_InspectionData](veh.UID_VEHICLE) dot
WHERE    veh.UID_CUSTOMER = 88;
Sign up to request clarification or add additional context in comments.

6 Comments

EXCELLENT!!! This is the answer, but with minor change..SELECT dot.*... Thanks you very much -- you are very smart!!!
@JohnD Yer welcome, and I updated my answer with that. I didn't include it at first since I didn't know what columns you needed and figured you would change the SELECT as needed.
@srutzky...dear sir...Although I marked this as the answer, and I thank you for your answer, I find upon further examination of the result-set, I set the CROSS to OUTER and found that NOT all vehicles for customer=88 are being returned. Any ideas?
@JohnD Why would you change the CROSS to OUTER? Off hand it is impossible to say without seeing some data and the logic if the InspectionData TVF.
...I changed to OUTER, because I need the data for each and every vehicle -- and the inspection data may be NULL in some cases. But that is OK, for the report will display default-values when the inspection fields are NULL. thx...John
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.