I have a SQL view that calls a scalar function with a string parameter. The problem is that the string occasionally has special characters which causes the function to fail.
The view query looks like this:
SELECT TOP (100) PERCENT
Id, Name, StartDate, EndDate
,dbo.[fnGetRelatedInfo] (Name) as Information
FROM dbo.Session
The function looks like this:
ALTER FUNCTION [dbo].[fnGetRelatedInfo]( @Name varchar(50) )
RETURNS varchar(200)
AS
BEGIN
DECLARE @Result varchar(200)
SELECT @Result = ''
SELECT @Result = @Result + Info + CHAR(13)+CHAR(10)
FROM [SessionInfo]
WHERE SessionName = @Name
RETURN @Result
END
How do I escape the name value so it will work when passed to the function?