I have a dynamically compiled update query which really has a bunch of statements in it. Now the syntax seems correct as I am able to, in a new query window(SSMS) execute the same string with no errors returned and the changes are indeed made in the table. i.e
SELECT  @updatequerystring = 
' UPDATE  ##Details  
    SET  [FieldName] =  (Replace 
                            ( Replace 
                                ( Replace 
                                    ( Replace 
                                        ( Replace 
                                            ( Replace 
                                                ( Replace 
                                                    ( Replace 
                                                        ( Replace 
                                                            ( Replace 
                                                                ( FieldName, Char(92),''___reversesolidusChar___'') 
                                                            , Char(91),''___leftsquarebracketChar___'') 
                                                        , Char(59),''___Semicolon___'') 
                                                    , Char(58),''___ColonChar___'') 
                                                , Char(47),''___SolidusChar___'') 
                                            , Char(46),''___fullstopChar___'') 
                                        , Char(44),''___CommaChar___'') 
                                    , Char(39),''___apostropheChar___'') 
                                , Char(34),''___DoubleQuotesChar___'') 
                            , Char(32),''___SpaceChar___'') )  
;  UPDATE  ##Details  
    SET  [Response] =  (Replace 
                            ( Replace 
                                ( Replace 
                                    ( Replace 
                                        ( Replace 
                                            ( Replace 
                                                ( Replace 
                                                    ( Replace 
                                                        ( Replace 
                                                            ( Replace 
                                                                ( Response, Char(92),''___reversesolidusChar___'') 
                                                            , Char(91),''___leftsquarebracketChar___'') 
                                                        , Char(59),''___Semicolon___'') 
                                                    , Char(58),''___ColonChar___'') 
                                                , Char(47),''___SolidusChar___'') 
                                            , Char(46),''___fullstopChar___'') 
                                        , Char(44),''___CommaChar___'') 
                                    , Char(39),''___apostropheChar___'') 
                                , Char(34),''___DoubleQuotesChar___'') 
                            , Char(32),''___SpaceChar___'') )  ; '
EXEC sp_executesql @updatequerystring
However when I set the same string to execute, in exactly the same was above, only from within my query, the following error is thrown instead.
.Net SqlClient Data Provider: Msg 50000, Level 15, State 1, Procedure GenerateActivitiesQuestionResponseResultSet, Line 251 Incorrect syntax near ' UPDATE ##Details SET [FieldName] = (Replace ( Replace ( Replace ( Replace ( Replace ( Replace ( Replace ( Replac'.
Same error is thrown when I try to execute just one of the two update statements in the above. I would appreciate any help in figuring this out. Thank you in advance.