views:

29

answers:

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
+6  A: 

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
DyingCactus
+1: The `RETURN` statement needs to be within the BEGIN/END block.
OMG Ponies
+1: There should be no bare SELECT within a function on SQL Server, and the return value needs to be set.
Mike Burton
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
@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.
DyingCactus
A: 

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
AdaTheDev
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?
jeff