2

Getting the following error when trying to create this sql function in SQL2k5. Any ideas here? It runs fine outside the function.

UPDATE THIS work NOW ,but I have to come to the realization that this needs to be in the form of a view since I need to do an inner join on the product_id so this current form will only work when I am passing the product ID. Any thoughts?

Msg 102, Level 15, State 1, Procedure getoptionlist, Line 13 Incorrect syntax near ')'.

CREATE FUNCTION dbo.getoptionlist 
(@ProductID as int)
RETURNs varchar(101) 
AS
BEGIN
declare @Return varchar(101)
 SELECT SUBSTRING(
(SELECT ','  + s.Name + '~0'
FROM vOptions_details s
where product_id=@ProductID
ORDER BY s.Name
FOR XML PATH('')),2,200000) 
)
end
return @return
1
  • this is why indenting properly and formatting your code can be helpful Commented Feb 10, 2010 at 18:43

2 Answers 2

6

A few problems:
- one too many parentheses
- return statement should be before "end"
- you need to set the @return variable

CREATE FUNCTION dbo.getoptionlist 
(@ProductID as int)
RETURNs varchar(101) 
AS
BEGIN
declare @Return varchar(101)
 SELECT @return = SUBSTRING(
(SELECT ','  + s.Name + '~0'
FROM vOptions_details s
where product_id=@ProductID
ORDER BY s.Name
FOR XML PATH('')),2,200000) 

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

4 Comments

+1: The RETURN statement needs to be within the BEGIN/END block.
+1: There should be no bare SELECT within a function on SQL Server, and the return value needs to be set.
I have to come to the realization that this needs to be in the form of a view since I need to do an inner join on the product_id so this current form will only work when I am passing the product ID. Any thoughts?
@jeff: I think that changes the question enough that you might want to ask it separately. If not, at least you need to edit the current question with a short description of the tables/columns involved and the sample data and results you would want. Thanks.
0

You have one too many end brackes and the RETURN statement needs to be inside the BEGIN..END block.

Change the last 3 lines from

)
end 
return @return

to:

return @return
end

1 Comment

Got this working, but I have to come to the realization that this needs to be in the form of a view since I need to do an inner join on the product_id so this current form will only work when I am passing the product ID. Any thoughts?

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.