I wrote a multi statement table valued function. But I encountered slowness in this function.
CREATE FUNCTION [dbo].[userFunc]
(
-- Input param
)
RETURNS
@Results TABLE
(
UserId BIGInt,
FirstName BIGINT,
LastName INT
)
AS
BEGIN
INSERT INTO @Results
SELECT UserId, FirstName, LastName
FROM MyTable
RETURN
END
When I investigate this issue i found the cause of the slowness. Issue is with below Insert query.
INSERT INTO @Results
SELECT UserId, FirstName, LastName
FROM MyTable
But when I remove INSERT INTO @Results, the query is fast and return the result. Any idea why?