Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
dhinasql
Posting Yak Master
195 Posts |
Posted - 2014-01-01 : 03:01:19
|
Friends,I am new for SQL SPI have three table, Table 1 - MerchantMerchantID MerchantName Zip1 Merchant1 10012 Merchant2 10023 Merchant3 10034 Merchant4 1004Table 2 - RegionRegionID RegionName1 Region12 Region23 Region3Table 3 - OfferOfferID RegionID MerchantID1 1 32 1 23 1 14 2 25 2 4I have two input parameter for this SP, @MerchantName = NULL and @RegionName = NULLExpected Result TableIf input parameter @MerchantName = NULL and @RegionName = NULLMerchantID MerchantName Zip 1 Merchant1 10012 Merchant2 10023 Merchant3 10034 Merchant4 1004If we Search by RegionName="Region1" Ex: @MerchantName = NULL and @RegionName = 'Region1'MerchantID MerchantName Zip 1 Merchant1 10012 Merchant2 10023 Merchant3 1003If we Search by RegionName="Region2" Ex: @MerchantName = NULL and @RegionName = 'Region2'MerchantID MerchantName Zip 2 Merchant2 10024 Merchant4 1004If we search by MerchantName='Merchant1' Ex: @MerchantName = 'Merchant1' and @RegionName = ''MerchantID MerchantName Zip 1 Merchant1 1001Please help me to get the expected result for the above scenario |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2014-01-01 : 10:58:37
|
you need to use a logic like belowCREATE PROC GetMerchantDetails@MerchantName varchar(100)= NULL,@RegionName varchar(100) = NULLASSELECT *FROM Merchant mWHERE EXISTS(SELECT 1FROM Offer oINNER JOIN Region rON r.RegionID = o.RegionIDWHERE o.MerchantID = m.MerchantIDAND (r.RegionName = @RegionName OR @RegionName IS NULL))AND (MerchantName = @MerchantNameOR @MerchantName IS NULL) GO ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
dhinasql
Posting Yak Master
195 Posts |
Posted - 2014-01-02 : 12:04:03
|
Thank you so much Visakh, Its working great... |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2014-01-02 : 12:24:08
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
|
|
|