An error is appear
I found the max length of the @ssql is 4000.
The issue is, eventhough you have set the @ssql variable as varchar(8000), it will take only 4000 characters. And the error you are getting is because of the dynamic query what you are constructing is having more than 4000 characters. Hence, I suggest you to use NVarchar(max) for @ssql variable. Hope this will solve the issue.