Not sure if this is possible and this might be a stupid question but I figured it couldn't hurt to ask. Is it possible to check if a table exists before Joining to it? for example like this Select * from dbo.Master t1 JOIN dbo.table2 t2 ON
t2.ID IF OBJECT_ID('dbo.t_TABLE3') IS NOT NULL BEGIN LEFT JOIN dbo.t_TABLE3' t3 ON
No. You'd have to check for it's existence as part of a logic branch in the code, and then run separate queries depending on whether it did or not. I'm intrigued? Why would you want to do such a thing? Whatever consumes the results wouldn't know how many/what columns it was going to get back.
There are potential pitfalls with writing conditional code like this - particularly if you try to port it to other versions of SQL Server. In order to protect yourself against code compilation / run-time errors, you'll need to do something like: DECLARE @SQLCommand varchar(max) SELECT @SQLCommand = 'SELECT * FROM dbo.master t1 JOIN dbo.Table2 t2 ON
t2.ID ' IF OBJECT_ID('dbo.t_Table3') IS Not NULL BEGIN SELECT @SQLCommand = @SQLCommand + 'LEFT JOIN dbo.t_Table3 t3 ON
t3.ID ' END EXEC (@SQLCommand) Something like that, anyway. Build up your query as a separate string based on what objects are available, then `EXEC` it. A pain, to program, maintain and keep performance optimised, and generally a clue that something, somewhere, has gone wrong with the design of the system.