Click here to Skip to main content
15,889,656 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have:

ALTER Procedure [dbo].[GetService]
(
@intCountryID int,
@intRegionID int
)

as
Begin
SELECT 
				ECT.ContactID,
				TEC.Country AS [Country Tag],
				TER.Region AS [Region Tag], 
				EBR.BrandCOE AS [BrandCOE Tag],
				ECT.DisplayContact,
				ECT.Contact,
				ECT.Address1,
				ECT.Address2,
				ECT.City,
				EC.Country,
				ER.Region,
				ECT.ContactState,
				ECT.PostalCode,
				ECT.Phone1,
				ECT.Fax,
				ECT.Email,
				ECT.URL

			FROM dbo.Emr_ContactBrandCOE ECBS		

				INNER JOIN dbo.Emr_Contact ECT					ON (ECT.ContactID = ECBS.ContactID)				  
				INNER JOIN dbo.Emr_BrandCOE EBR					ON (ECBS.BrandCOEID = EBR.BrandCOEID)	
				--Contacts Location
				LEFT JOIN dbo.Emr_Country EC					ON (EC.CountryID = ECT.CountryID)
				LEFT JOIN dbo.Emr_Region ER					ON	(ER.RegionID = EC.RegionID)
				--Tag Location
				LEFT JOIN dbo.Emr_Country TEC					ON (TEC.CountryID = ECBS.CountryID)
				LEFT JOIN dbo.Emr_Region TECER					ON	(TECER.RegionID = TEC.RegionID)

				LEFT JOIN dbo.Emr_Region TER					ON (TER.RegionID = ECBS.RegionID)			   
		   WHERE     
				 EBR.BusinessID = 3 
				AND (
						(ECT.CountryID = @intCountryID) OR
						(ECBS.CountryID = @intCountryID) OR
						(ECBS.RegionID = @intRegionID) 
					)

		   ORDER BY ECT.DisplayContact ASC 
end


as a stored procedure. I want to change the whole where clause depending on parameter.

if @intCountryID = 0:

SQL
WHERE
                 EBR.BusinessID = 3
                AND (
                        (ECBS.RegionID = @intRegionID)
                    )

           ORDER BY ECT.DisplayContact ASC


if @intCountryID is not equal to 0:

SQL
WHERE
                 EBR.BusinessID = 3
                AND (
                        (ECT.CountryID = @intCountryID) OR
                        (ECBS.CountryID = @intCountryID) OR
                        (ECBS.RegionID = @intRegionID)
                    )

           ORDER BY ECT.DisplayContact ASC



is there a way to implement this?
Posted
Comments
PIEBALDconsult 28-Jul-14 9:00am    
Write two procedures?

SQL
WHERE EBR.BusinessID = 3 
AND 1 = CASE WHEN @intCountryID = 0 
         THEN CASE WHEN ECBS.RegionID = @intRegionID THEN 1 ELSE 0 END
         ELSE 
            CASE WHEN ECT.CountryID = @intCountryID OR ECBS.RegionID = @intRegionID OR ECBS.RegionID = @intRegionID THEN 1 ELSE 0 END
         END
ORDER BY ECT.DisplayContact ASC

Happy Coding!
:)
 
Share this answer
 
Comments
Maciej Los 30-Jul-14 1:56am    
+5
Aarti Meswania 2-Aug-14 4:56am    
thank u :)
How about:
SQL
WHERE
    EBR.BusinessID = 3
AND
    (
        (@intCountryID != 0 AND @intCountryID IN (ECT.CountryID, ECBS.CountryID))
    OR
        ECBS.RegionID = @intRegionID
    )
 
Share this answer
 
above answers are correct.
if you want to put other solution then
you can store all the query string in to string/varchar/ text variable and use concatenation by putting if condition and put where clause as you required
then
execute your query by using exec keyword
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900