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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Order by clause invalid in a function

Author  Topic 

pixelwiz
Starting Member

25 Posts

Posted - 2011-08-10 : 15:35:26
Hi,

I have a function that returns a string of records returned by a subquery. This has worked fine in the past, but all of a sudden the subquery started returning items not in the order they are being inserted. And it won't let me add an order by clause to the subquery either. If I add select top 100, it works, and this will never return more than like 20, but I don't know if it's a bad idea...


ALTER FUNCTION [dbo].[udf_mp_getShotsForTeamHole]
(
-- Add the parameters for the function here
@matchid int,
@sideid int,
@holeid int
)
RETURNS varchar(64)
AS
BEGIN
-- Declare the return variable here
DECLARE @shots varchar(64)

-- Add the T-SQL statements to compute the return value here
SELECT @shots = (COALESCE(@shots + ', ', '') + abbr)
FROM (
SELECT ms.hole_id, ms.shot_number, ISNULL(st.abbr,'U') abbr, st.description
FROM match_shots ms
LEFT OUTER JOIN shot_types st
ON st.id = ms.shot_type_id
WHERE ms.match_id = @matchid
AND ms.side_id = @sideid
AND ms.hole_id = @holeid
ORDER BY ms.shot_number
) shots_data

-- Return the result of the function
RETURN @shots
END

The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2011-08-10 : 16:15:49
I think you could use TOP 100 PERCENT.

But, i'd suggest you use the FOR XML method for contactenating a string if the order is important.
Go to Top of Page
   

- Advertisement -