I have this SQL function which is wrapped by a stored procedure:
ALTER FUNCTION dbo.GetObjList
(
@filterUID int = NULL,
@filterSID varchar(32) = NULL
)
RETURNS TABLE
AS
RETURN
SELECT ROW_NUMBER() OVER (ORDER BY UID) AS [RowNumber], *
FROM ObjTable
WHERE
(COALESCE(@filterUID, @filterSID) IS NULL) OR
(
((@filterUID IS NOT NULL) AND (UID = @filterUID)) OR
((@filterSID IS NOT NULL) AND (SID = @filterSID))
)
Why would I receive such an error: "Conversion failed when converting the varchar value 'abc' to data type int." if I pass only @filterSID = 'abc'
as parameters (and DEFAULT for others) ?
I noticed that COALESCE is responsible for the error.
EDIT: Now that I got the reason of the error... and given the fact that I have lots of params actually... what would you guys recommend as solution?