Sql-server – ignore table does not exist error

sql-server-2008

I'm creating a script to automaticly copy from multiple access databases into sql server databases.

My script currently creates a new sql database and creates all of the tables. It then trys to insert the data from an access database (using linked servers) table into the newly created sql database table.

The problem is not all of the databases have all of the tables. In these cases it is ok for the sql server table to remain empty. However, the insert throws an error because the table does not exist in the linked server.

Is there any way to tell sql server to ignore the table not found error so that it will continue on to the next insert statment?

Best Answer

Does TRY/CATCH properly handle this?

BEGIN TRY
    INSERT dbo.localtable SELECT ... FROM linkedserver...table;
END TRY
BEGIN CATCH
    PRINT 'this table was skipped';
END CATCH