Trouble using Dynamic SQL to add a column to a linked server

  • I have a situation that I need to add a field to a table over linked server. The specifications of this is dynamic and it is being done in TQL / Stored procedures and this can not change. My code is generating the statement just fine and if I copy paste it to a new SSMS window and execute it WORKS.. The problem is I need to dynamically generate the statement (I am doing that just fine, I THINK). THEN I need to execute the statement IN THE SPROC, this part is not working.

    Here is the code:

    SET @AlterSQL = @DestinationServerName + '.[' + @DestinationDBName +'].' + @DestinationSchemaName + '.sp_executesql N'' ALTER TABLE '

    + @DestinationTableName + ' ADD ' + @TempColumn + ' int' + CHAR(39)

    The above Creates this when I expose it via a PRINT statement:

    addb15.[FSParallel].dbo.sp_executesql N' ALTER TABLE Node ADD ImportIdentity int'

    After I create the statement I use:

    EXEC @AlterSQL

    And this returns the following error:

    Msg 2812, Level 16, State 62, Procedure ETLDynamicImport, Line 244

    Could not find stored procedure 'FSParallel.dbo.sp_executesql N' ALTER TABLE Node ADD ImportIdentity int''.

    Can someone PLEASE advise on this? I am on a project Deadline and have googled this to no end and just can not get it working.

    <hr noshade size=1 width=250 color=#BBC8E5> Regards,Jeffery Williams http://www.linkedin.com/in/jwilliamsoh

  • Duplicate post.

    Replies here please: http://www.sqlservercentral.com/Forums/FindPost1679271.aspx

    -- Gianluca Sartori

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply