Home Forums SQL Server 2005 T-SQL (SS2K5) Dynamic SQL Giving 'Name not a valid identifier error' RE: Dynamic SQL Giving 'Name not a valid identifier error'

  • Try using sp_executesql and see if that fixes it.

    Here is a sample that should work.

    DECLARE @SQL NVARCHAR(MAX)

    DECLARE @TBL TABLE (i int)

    SET @SQL = 'SELECT TOP 10 message_id FROM sys.messages'

    INSERT INTO @TBL

    EXEC sp_executesql @SQL

    SELECT * FROM @TBL