You cannot use Temporary tables inside a function in sql server
Instead you can use table variable in sql server
The concept over here is user defined function will not allow to perform any DDL(Data Definition Language) operation.
If you use Temp Table, You can create index in it, but in Table variable you cannot perform any table level operation like alter table etc.
That's why temp table is restricted in Sql Server User-Defined function.
Please mark this as Answer, if this helps
<< Database Administrator >>
Jesus saves! The rest of us better make backups...