Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Working with subqueries in functions

Author  Topic 

MagicCity77
Starting Member

19 Posts

Posted - 2009-11-23 : 17:13:15
Hello All, I would like to know why I am getting the following msg in my SQL statement and how to resolve it

Here is the code:
RETURNS real 

AS
BEGIN
RETURN (SELECT dbo.EDCDet.EDID
, dbo.EDCDet.Shot
, Sum(dbo.EDCDet.HoleNumber) AS 'HoleSum'

FROM dbo.EDCDet

GROUP BY dbo.EDCDetails.EDCDetailsID, dbo.EDCDet.Shot)

END


Here is the Error msg:

Msg 116, Level 16, State 1, Procedure fx_TotalHole, Line 25
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.


I currently have this set up as a scalar function. A colleague recommended that I take this approach rather than making this a storedprocedure but would not elaborate as to why and I hate being in the dark about things.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-11-23 : 18:20:36
You'd have to use a table valued function since your query returns more than one column and more than one row. When you use a scalar function, it can only return one value.

And yes a stored procedure should probably be used instead because of this.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

MagicCity77
Starting Member

19 Posts

Posted - 2009-11-24 : 09:09:18
Thanks, I felt like this person was trying to lead me down the wrong path. I will not be taking much advice from them.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-11-24 : 11:12:15
You're welcome. Some colleagues think they are smarter than you but aren't.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page
   

- Advertisement -