简体   繁体   中英

Join on table that does not exist

Is it possible to join tables, when one or more tables does not even exist?

Consider this use case: You are using some system that has a certain DB scheme out of the box, but allows you to create your own custom tables as well.

It is possible to run a query of some kind that includes custom tables, but will also run without errors for someone who does not have these custom tables set up?

Or what is the most elegant way to achieve this without having to maintain different versions of your queries?

edit: especially for Sybase ASE, but I am also interested in other dbms.

You could do something like this:

IF EXISTS (Select * from sysobjects where name = 'tblname')
Begin

Select *
from tbl

End
Else
Begin
--Do something else
End

Basically check the table exists and run the query if it does, if it doesn't then do something else.

In most RDBMS, the query execution looks like something like this:

  • Parser
    • Name resolution (this checks the referenced objects, binds aliases, etc)
    • Type derivation (Determines the final types in the resultset)
    • Aggregate bindinf (Determines if any aggregation is required)
    • Group binding (Binds the aggregation to the select list)
  • Algebrizer
  • Optimizer
  • Execution

Because of the steps of query execution, the RDBMS always checks the referenced objects (during the name resolution step), the actual query processing (which executes any user defined checks - eg an IF statement) are executed after that. (This stands for Data Manipulation, not for Data Definition).

To make it short: In most RDBMS you can not do this, but only with dynamic queries.

In my opinion, dynamic queries are sometimes helpful, but not an easy task maintain and debug them, so be careful, especially if the query generation is based on complex logic.

Please keep in mind, that most client applications are accept only a predefined resultset (this includes the columns and their types), so working with dynamic queries could be a problem in the client application too.

For Sybase, you can read more about query processing in it's online documentation: Performance and Tuning Series: Query Processing and Abstract Plans

SQL Server

Try to make use of TRY/CATCH, it can contain anything up to error severity 20.

In your case you are trying to catch an error severity 11 - "Indicates that the given object or entity does not exist." - which should be fine within a TRY/CATCH block.

More info and examples here: https://msdn.microsoft.com/en-us/library/ms175976.aspx

In my case, I use it exclusively to drop tables - can't be bothered to do an IF statement anymore, to check if the table exists.

BEGIN TRY DROP TABLE XXX END TRY BEGIN CATCH END CATCH

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM