QUERY EXECUTION based on IF condition
HI,
I am trying to display records from more than one table depending on the condition How many tables the user chooses to display. I have several tables like contact_info, daily_nav,daily_returns and so on.
I have a check box for each table. IF user checks more than one table then the query should display records from all the table selected by the user. I am not getting how to write such query so I have started by writting the query with two fixe dtable which is as follows
CREATE PROCEDURE dbo.SearchDynamicData
(@v_Table1 int = NULL, @v_Table2 int = NULL, @v_FundName nvarchar(200))
AS
SET NOCOUNT OFF
IF @v_Table1 IS NOT NULL
BEGIN
SELECT * FROM contact_info ,Fund_hrd WHERE
contact_info.icdi_no LIKE Fund_hrd.icdi_no
AND Fund_hrd.fund_name LIKE @v_FundName
END
SET NOCOUNT ON
GO
Above code is for when table it works fine but how do I combine it with the second table say daily_nav
IF @v_Table2 IS NOT NULL
BEGIN
SELECT * FROM daily_nav ,Fund_hrd WHERE
daily_nav.icdi_no LIKE Fund_hrd.icdi_no
AND Fund_hrd.fund_name LIKE @v_FundName
END
I don't know how to combine these statements. I tried using JOIN clause but it didnt work. Can anyone help me out with this?
Thanks
durgesh
__________________
durgesh
|