I have following scalar function but the problem is that it doesn't return actual result but returns a query.
Could somebody guide me on it what I am missing here or making wrong?
I have formed the following function
ALTER function test(@QueId int, @Answer varchar(250)) RETURNS VARCHAR(500) AS
BEGIN
DECLARE @STR varchar(4000)
DECLARE @QRY AS VARCHAR(1000)
SELECT @QRY=
'SELECT @str = COALESCE(@str + '';'','''') + AnswerText '+
'FROM SurveyQuestionAnswerTypes WHERE AnswerType=(Select AnswerType From SurveyQuestions Where QuestionID=' +
CAST(@QueId AS VARCHAR(4)) + ')AND AnswerValue in (' + replace(@Answer,'^',',') +')'
--EXEC sp_executesql @QRY, '@STR VARCHAR(4000)', @STR
RETURN @QRY
END
Instead of returning a result it returns
SELECT @str = COALESCE(@str + ';','') + AnswerText
FROM SurveyQuestionAnswerTypes
WHERE AnswerType = (Select AnswerType
From SurveyQuestions
Where QuestionID=25)AND AnswerValue in (3,4,5,6)