Answered Drop Table error

  • Sunday, April 15, 2012 8:06 PM
     
      Has Code

    Hi,

    The following one is producing error;  Please let me know how to drop the table which set in variable @A;

    DECLARE @A VARCHAR(40);
    SET @A='MyTable';
    IF OBJECT_ID(@A) IS NOT NULL
    DROP TABLE @A;

    Msg 102, Level 15, State 1, Line 4

    Incorrect syntax near '@A'.

All Replies

  • Sunday, April 15, 2012 8:17 PM
     
     Answered Has Code

    Thanks got it while google;

    DECLARE @A VARCHAR(40);
    SET @A='MyTable';
    IF OBJECT_ID(@A) IS NOT NULL
    Execute ('DROP TABLE '+@A);

    • Marked As Answer by Kashif Chotu Sunday, April 15, 2012 8:17 PM
    •