Tuesday, October 9, 2012

UNION two SELECTs instead of providing a table name inside of a JOIN in MSSQL

This...

INNER JOIN Foo F ON F.BarId = B.BarId

 
 

...could be replaced with...

INNER JOIN (SELECT * FROM Foo UNION SELECT * FROM FooStagingGrounds) F ON F.BarId = B.BarId

 
 

...assuming Foo and FooStagingGrounds have the same columns. In such a scenario, all of the calls to F littered throughout the stored procedure should work against the union of the two tables without needing any further doctoring.

No comments:

Post a Comment