Why are you trying to retrieve data from a table that does not exist?
The main building block of the database is the table. Not knowing what is in your schema, basically trying to use SQL as a dynamic language, but it is not.
I would review your design; without knowing more about the tables in your database and its intention to use others to do this to help in this regard. Add more information to your question.
EDIT I read BOL, and the recommended way to handle errors when the object does not exist is as follows:
You can use TRY ... CATCH to handle errors that occur during compilation or recompile at the level of execution of the code that generates errors into a separate batch in the TRY block. For example, you do this by placing code in a stored procedure or executing dynamic Transact-SQL using sp_executesql. This allows TRY ... CATCH to catch the error at a higher level of execution than the error.
I checked this using the following procedure
CREATE PROCEDURE [dbo].[BrokenProcedure] AS BEGIN SET NOCOUNT ON; SELECT * FROM MissingTable END GO
Then called in the TRY..CATCH block:
BEGIN TRY PRINT 'Error Number before: ' + CAST(@@Error AS VARCHAR) EXECUTE [dbo].[BrokenProcedure] PRINT ' Should not see this' END TRY BEGIN CATCH PRINT 'Error Number in catch: ' + CAST(@@Error AS VARCHAR) END CATCH
The result of the following output:
Error number to: 0
Catch Error Number: 208
This is not an ideal solution, since you have to create procedures (or use dynamic SQL) for all access to the table, but this method is recommended by MS.
Tony
source share