I have three tables
tbl1 --which is the main table
col1 col2
1 aa
2 bb
3 cc
4 dd
tbl2
col1 col2 (foreign key to tbl1 (col1))
1 2
2 2
3 1
4 3
tbl3
col1 col2 (foreign key to tbl1 (col1))
1 4
2 3
3 3
4 1
I want to write a procedure which have two parameters @par1 and @par2 and selects data from tbl1 one on the following condition
if both @par1 and @par2 are null then all records in tbl1 are selected
or
if @par1 is null and @par2 is not null, all records in tbl1 which exists in tbl2 selected
or
if @par2 is null and @par1 is not null, all records in tbl1 which exists in tbl3 selected
how can I do this in a more efficient single query, my query takes more than a minute for only 2290 records
Thanks,
tbl1 --which is the main table
col1 col2
1 aa
2 bb
3 cc
4 dd
tbl2
col1 col2 (foreign key to tbl1 (col1))
1 2
2 2
3 1
4 3
tbl3
col1 col2 (foreign key to tbl1 (col1))
1 4
2 3
3 3
4 1
I want to write a procedure which have two parameters @par1 and @par2 and selects data from tbl1 one on the following condition
if both @par1 and @par2 are null then all records in tbl1 are selected
or
if @par1 is null and @par2 is not null, all records in tbl1 which exists in tbl2 selected
or
if @par2 is null and @par1 is not null, all records in tbl1 which exists in tbl3 selected
how can I do this in a more efficient single query, my query takes more than a minute for only 2290 records
Thanks,