I am trying to create a function as per my requirements.
But, when am creating or drop #tempTable, it is giving an error as:
invalid use of a side-effecting operator 'drop object' within a function
My understanding is that we can't have create, drop or insert operations on #temptable in a function.
Is that correct?
My SQL:
CREATE FUNCTION [dbo].[RT_ResultFunction]
(
Id VARCHAR(4000)
)
RETURNS @RT_ResultFunction TABLE
(
Id VARCHAR(20)
, Name varchar(20)
,Balance Int
)
AS
BEGIN
IF OBJECT_ID('tempdb..#tempTable') IS NOT NULL
DROP TABLE #tempTable
SELECT Id, COUNT(Balance)
INTO #tempTable
'Balance' FROM Table1
INSERT @RT_ResultFunction
SELECT T1.ID,T1,NAME,T2,Balance
FROM Table2 T1,
#tempTable T2
WHERE T1.ID = T2.ID
RETURN
END