Hi,
you can use a session Key/Value instead of variable for the task using the function SESSION_CONTEXT
For full explanation of the limitations and the solution you can check this post:
https://ariely.info/Blog/tabid/83/EntryId/335/SQL-Server-CREATE-VIEW-which-use-a-Variable.aspx
----------
Ronen Ariely
Personal Site | Blog | Facebook | Linkedin