• Well this code is making a Dynamic Query based upon the parameter(s) value and if you want to check whats is making in the end. In the end of the code place the following :

    SELECT @QUERY

    this will give you the complete query copy the text and paste it in a t-sql and you will understand what its actually doing.