I am trying to make a scalar function in SQL Server. I have coined up the select statement as follows:
SET @Statement1 = 'SELECT 1 FROM ' + @p_TableName +
' WHERE RegionTypeID = 1 AND RegionID = ' +
CONVERT (nvarchar (50), @p_RegionID)
Here @p_TableName is the name of the table (nvarchar (500)) and @p_RegionID is a uniqueidentifier.
I am now executing the above statement as follows:
EXEC @ReturnValue1 = @Statement1
WHERE @ReturnValue1 is int.
But when I call this function from elsewhere I get this error:
The name 'SELECT 1 FROM [PS].[Availability] WHERE RegionTypeID = 1 AND RegionID = AF4C182C-F751-41AD-AA6A-20A50A7A38C8' is not a valid identifier.
I need to know how I can call a dynamic sql select statement from within a scalar function.