locked
How to insert data into table Dynamically SQL RRS feed

  • Question

  • User-617859429 posted

    Hi I am trying to insert data dynamically using SQL. its not working.

    Code is

    DECLARE @Query varchar (MAX)

    SET @Query = 'INSERT INTO [dbo].' + @TableName + '([ID], A,B)

      SELECT [ID] , ColumnA, ColumB FROM #temp'

    EXEC (@Query)

    Friday, July 15, 2016 3:43 PM

All replies

  • User-359936451 posted

    What is your table design? And what is the error?

    Friday, July 15, 2016 5:41 PM
  • User-1404113929 posted

    hi,

    can you share error message.

    thanks,

    murali

    Saturday, July 16, 2016 8:02 AM
  • User-2057865890 posted

    Hi muhammadazeemazam,

    Please provide more details. Before you call EXECUTE with a character string, validate the character string. You could use 

    print @Query

    to check the result.

    Best Regards,

    Chris

    Monday, July 18, 2016 9:21 AM