0

I am using the following select statement in a stored procedure to get data from a table, with a function that returns a JSON for one of the columns. What am I doing wrong in my query?

The function GetRecordComments requires two parameters, with the first one being the RecordID of each row. When I use this query though, I get the following error. How can I call the function passing the recordID as parameter for each row?

Query:

SELECT 
    RercordID, Name, Region, 
    dbo.GetRecordComments((SELECT RecordID 
                           FROM RecordList  
                           WHERE RecordID = a.RecordID), 1),
    [Address],
    dbo.GetRecordComments((SELECT RecordID 
                           FROM RecordList  
                           WHERE RecordID = a.RecordID), 2)
FROM 
    RecordList AS a

Error:

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

2
  • 4
    Why not .., Region, dbo.GetRecordComments(RecordID, 1), [Address], ..? Commented Mar 22, 2020 at 3:42
  • Thank you. This works perfectly! Commented Apr 16, 2020 at 17:11

1 Answer 1

1

To me, it is very strange that RecordId would be repeated in a table called RecordList. But clearly it is. Happily the subqueries are not necessary:

SELECT rl.RecordID, rl.Name, rl.Region, 
       dbo.GetRecordComments(rl.RecordID, 1),
       rl.Address,
       dbo.GetRecordComments(rl.RecordID, 2)
FROM RecordList rl;

You should know that such user-defined functions are generally performance killers. My guess is that you have a comments table. An explicit JOIN is usually much preferable.

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

Comments

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.