Search code examples
sqlreporting-servicesdynamics-crm

How to write if exist statement that would run a different select depending if it exists or not


I am trying to convert a sql if exists statement into a SSRS valid format to run a report on CRM.

CRM report doesn't accept the report on upload if I have a if exists method, I'm having troubles figuring out what I can use in its place.

IF EXISTS(select * from dbo.FC where dbo.FC.ContactID in (select dbo.AV.so_contactid from dbo.AV))
begin
    select [STATEMENT 1]
    from dbo.AV CRMAF_so_AV join
         dbo.FC c 
        on CRMAF_so_AV.so_contactid = c.ContactID;
end
else
begin
    select [STATEMENT 2]
    from dbo.AV CRMAF_so_AV join
         dbo.FA c
         on CRMAF_so_AV.so_contactid = c.AccountID;

end;

I want to be able to either run the select [STATEMENT 1] if the condition is true else I want to run select [STATEMENT 2]


Solution

  • I have managed to get this to work by doing a LEFT JOIN instead of a JOIN.

    select [STATEMENT 1 + 2 all columns needed]
    from dbo.AV CRMAF_so_AV 
    left join dbo.FC c on CRMAF_so_AV.so_contactid = c.ContactID;
    left join dbo.FA a on CRMAF_so_AV.so_contactid = a.AccountID;
    

    This now runs if its an account or a contact.