I have a question addressed to sql guru.
There are two tables with almost the same structure.
Based on the parameter passed to the stored procedure, I need to collect data from a particular table.
How to do it in the best way?
Please do not suggest combining these tables into one, which is not suitable.
I did the following (MS SQL Server 2008):
Select *
FROM
String s
JOIN (
SELECT id
,TypeCode
,ProdNo
FROM Table1
WHERE @param = 1 AND TypeCode= 'INV'
UNION
SELECT id
,TypeCode
,ProdNo
FROM Table2
WHERE @param = 2 AND TypeCode= 'INV'
) m ON m.Id = s.Id
WHERE s.Id = 256
but when I looked at the execution plan, I was surprised because it received data from both tables in parallel threads and only after that filtered the @param value.
I thought that filtering would be done in the first step and the data collected from a separate table.
Is there a way to make a selection from only one table without splitting the query into two queries and using the IF statement?
thank