• Running the code as is, and adding a tablename from a SQL Server 2005 database, gives the following error:

    Msg 102, Level 15, State 1, Line 108

    Incorrect syntax near 'end'.

    Any ideas what's wrong. The syntax looks fine. I'm using the SQL Server query editor and selecting the database from the combo box in the menu bar at the top of the editor.

    Thanks,

    Ed