Hi @kkran
As Erland says, the error messages are already obvious. You can improve your code based on error messages.
For the error that cannot access temporary tables from within a function, you can actually try an alternative method.
You can try using table variables.
You can refer to this post to try the methods provided inside.
https://stackoverflow.com/questions/9561626/cannot-access-temporary-tables-from-within-a-function
Best regards,
Percy Tang
If the answer is the right solution, please click "Accept Answer". If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.