Inner Join only if satisfies If condition

 Clash Royale CLAN TAG#URR8PPP
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
3
down vote
favorite
I'm looking for code review, and optimizations.
I have two tables ItemCategory and ShopItemCategory
ItemCategory
--------------------------------------------------
| No | ItemCategoryId | CategoryName |
--------------------------------------------------
| 1 | 1233 | Cement |
--------------------------------------------------
| 2 | 1200 | Drinks | 
--------------------------------------------------
ShopItemCategory
--------------------------------------------------
| No | ItemCategoryId | ShopId |
--------------------------------------------------
| 1 | 1233 | 25 |
--------------------------------------------------
| 2 | 1200 | 32 | 
--------------------------------------------------
I have an sp (stored procedure) to get ItemCategory list for dropdown,
CREATE PROCEDURE [dbo].[spGetItemCategory]
AS
BEGIN
 SET NOCOUNT ON;
 SELECT ItemCategoryId ,CategoryName 
 FROM ItemCategory
End
This one works fine, and now I have new requirement. I need to send ShopId to the sp and return ItemCategory list that satisfies the condition, and if the receiving shopid equals 0 then I need to return all the ItemCategory.
My sp :
CREATE PROCEDURE [dbo].[spGetItemCategory]
( 
 @ShopID INT
)
AS
BEGIN
 SET NOCOUNT ON; 
 if @ShopID <> 0
 BEGIN
 SELECT ItemCategory.ItemCategoryId,CategoryName 
 FROM ItemCategory 
 inner join ShopItemCategory on 
 ShopItemCategory.ShopId = @ShopID AND 
 ShopItemCategory.ItemCategoryId = ItemCategory.ItemCategoryId
 END
 else
 BEGIN
 SELECT ItemCategoryId,CategoryName
 FROM aItemCategory
 END
 END
This works well in my code. But in the two conditions only the inner join part is different, So is there a way to reduce the code and improve performance?
I got other stored procedures with larger codes in if conditions, so this method will make it bigger and bigger. Is there any alternate way to do this? or to simplify this?
sql join stored-procedure
add a comment |Â
up vote
3
down vote
favorite
I'm looking for code review, and optimizations.
I have two tables ItemCategory and ShopItemCategory
ItemCategory
--------------------------------------------------
| No | ItemCategoryId | CategoryName |
--------------------------------------------------
| 1 | 1233 | Cement |
--------------------------------------------------
| 2 | 1200 | Drinks | 
--------------------------------------------------
ShopItemCategory
--------------------------------------------------
| No | ItemCategoryId | ShopId |
--------------------------------------------------
| 1 | 1233 | 25 |
--------------------------------------------------
| 2 | 1200 | 32 | 
--------------------------------------------------
I have an sp (stored procedure) to get ItemCategory list for dropdown,
CREATE PROCEDURE [dbo].[spGetItemCategory]
AS
BEGIN
 SET NOCOUNT ON;
 SELECT ItemCategoryId ,CategoryName 
 FROM ItemCategory
End
This one works fine, and now I have new requirement. I need to send ShopId to the sp and return ItemCategory list that satisfies the condition, and if the receiving shopid equals 0 then I need to return all the ItemCategory.
My sp :
CREATE PROCEDURE [dbo].[spGetItemCategory]
( 
 @ShopID INT
)
AS
BEGIN
 SET NOCOUNT ON; 
 if @ShopID <> 0
 BEGIN
 SELECT ItemCategory.ItemCategoryId,CategoryName 
 FROM ItemCategory 
 inner join ShopItemCategory on 
 ShopItemCategory.ShopId = @ShopID AND 
 ShopItemCategory.ItemCategoryId = ItemCategory.ItemCategoryId
 END
 else
 BEGIN
 SELECT ItemCategoryId,CategoryName
 FROM aItemCategory
 END
 END
This works well in my code. But in the two conditions only the inner join part is different, So is there a way to reduce the code and improve performance?
I got other stored procedures with larger codes in if conditions, so this method will make it bigger and bigger. Is there any alternate way to do this? or to simplify this?
sql join stored-procedure
 
 
 
 
 
 
 You can do it as 1 query by using a left outer join and adding "and (@Shopid = 0 or ShopID = @ShopID)" on your where statement. Whether this would be quicker, I doubt it
 â Paul
 Feb 6 at 16:29
 
 
 
 
 
 
 
 
 
 Sorry I did not understand what you said.
 â I R Shad
 Feb 7 at 6:29
 
 
 
 
 
 
 
 
 
 @Paul thank you for commenting, I tried that one, but its not working.
 â I R Shad
 Feb 7 at 6:48
 
 
 
 
 
 
 
 
 
 Your self-answer below is exactly what I just said.
 â Paul
 Feb 7 at 8:14
 
 
 
 
 
 
 
 
 
 you said using- left outer join, but in that way on the two condition I get full list of itemcategory
 â I R Shad
 Feb 7 at 8:33
 
 
 
 
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I'm looking for code review, and optimizations.
I have two tables ItemCategory and ShopItemCategory
ItemCategory
--------------------------------------------------
| No | ItemCategoryId | CategoryName |
--------------------------------------------------
| 1 | 1233 | Cement |
--------------------------------------------------
| 2 | 1200 | Drinks | 
--------------------------------------------------
ShopItemCategory
--------------------------------------------------
| No | ItemCategoryId | ShopId |
--------------------------------------------------
| 1 | 1233 | 25 |
--------------------------------------------------
| 2 | 1200 | 32 | 
--------------------------------------------------
I have an sp (stored procedure) to get ItemCategory list for dropdown,
CREATE PROCEDURE [dbo].[spGetItemCategory]
AS
BEGIN
 SET NOCOUNT ON;
 SELECT ItemCategoryId ,CategoryName 
 FROM ItemCategory
End
This one works fine, and now I have new requirement. I need to send ShopId to the sp and return ItemCategory list that satisfies the condition, and if the receiving shopid equals 0 then I need to return all the ItemCategory.
My sp :
CREATE PROCEDURE [dbo].[spGetItemCategory]
( 
 @ShopID INT
)
AS
BEGIN
 SET NOCOUNT ON; 
 if @ShopID <> 0
 BEGIN
 SELECT ItemCategory.ItemCategoryId,CategoryName 
 FROM ItemCategory 
 inner join ShopItemCategory on 
 ShopItemCategory.ShopId = @ShopID AND 
 ShopItemCategory.ItemCategoryId = ItemCategory.ItemCategoryId
 END
 else
 BEGIN
 SELECT ItemCategoryId,CategoryName
 FROM aItemCategory
 END
 END
This works well in my code. But in the two conditions only the inner join part is different, So is there a way to reduce the code and improve performance?
I got other stored procedures with larger codes in if conditions, so this method will make it bigger and bigger. Is there any alternate way to do this? or to simplify this?
sql join stored-procedure
I'm looking for code review, and optimizations.
I have two tables ItemCategory and ShopItemCategory
ItemCategory
--------------------------------------------------
| No | ItemCategoryId | CategoryName |
--------------------------------------------------
| 1 | 1233 | Cement |
--------------------------------------------------
| 2 | 1200 | Drinks | 
--------------------------------------------------
ShopItemCategory
--------------------------------------------------
| No | ItemCategoryId | ShopId |
--------------------------------------------------
| 1 | 1233 | 25 |
--------------------------------------------------
| 2 | 1200 | 32 | 
--------------------------------------------------
I have an sp (stored procedure) to get ItemCategory list for dropdown,
CREATE PROCEDURE [dbo].[spGetItemCategory]
AS
BEGIN
 SET NOCOUNT ON;
 SELECT ItemCategoryId ,CategoryName 
 FROM ItemCategory
End
This one works fine, and now I have new requirement. I need to send ShopId to the sp and return ItemCategory list that satisfies the condition, and if the receiving shopid equals 0 then I need to return all the ItemCategory.
My sp :
CREATE PROCEDURE [dbo].[spGetItemCategory]
( 
 @ShopID INT
)
AS
BEGIN
 SET NOCOUNT ON; 
 if @ShopID <> 0
 BEGIN
 SELECT ItemCategory.ItemCategoryId,CategoryName 
 FROM ItemCategory 
 inner join ShopItemCategory on 
 ShopItemCategory.ShopId = @ShopID AND 
 ShopItemCategory.ItemCategoryId = ItemCategory.ItemCategoryId
 END
 else
 BEGIN
 SELECT ItemCategoryId,CategoryName
 FROM aItemCategory
 END
 END
This works well in my code. But in the two conditions only the inner join part is different, So is there a way to reduce the code and improve performance?
I got other stored procedures with larger codes in if conditions, so this method will make it bigger and bigger. Is there any alternate way to do this? or to simplify this?
sql join stored-procedure
edited Feb 6 at 8:02
asked Feb 6 at 5:59
I R Shad
849
849
 
 
 
 
 
 
 You can do it as 1 query by using a left outer join and adding "and (@Shopid = 0 or ShopID = @ShopID)" on your where statement. Whether this would be quicker, I doubt it
 â Paul
 Feb 6 at 16:29
 
 
 
 
 
 
 
 
 
 Sorry I did not understand what you said.
 â I R Shad
 Feb 7 at 6:29
 
 
 
 
 
 
 
 
 
 @Paul thank you for commenting, I tried that one, but its not working.
 â I R Shad
 Feb 7 at 6:48
 
 
 
 
 
 
 
 
 
 Your self-answer below is exactly what I just said.
 â Paul
 Feb 7 at 8:14
 
 
 
 
 
 
 
 
 
 you said using- left outer join, but in that way on the two condition I get full list of itemcategory
 â I R Shad
 Feb 7 at 8:33
 
 
 
 
add a comment |Â
 
 
 
 
 
 
 You can do it as 1 query by using a left outer join and adding "and (@Shopid = 0 or ShopID = @ShopID)" on your where statement. Whether this would be quicker, I doubt it
 â Paul
 Feb 6 at 16:29
 
 
 
 
 
 
 
 
 
 Sorry I did not understand what you said.
 â I R Shad
 Feb 7 at 6:29
 
 
 
 
 
 
 
 
 
 @Paul thank you for commenting, I tried that one, but its not working.
 â I R Shad
 Feb 7 at 6:48
 
 
 
 
 
 
 
 
 
 Your self-answer below is exactly what I just said.
 â Paul
 Feb 7 at 8:14
 
 
 
 
 
 
 
 
 
 you said using- left outer join, but in that way on the two condition I get full list of itemcategory
 â I R Shad
 Feb 7 at 8:33
 
 
 
 
You can do it as 1 query by using a left outer join and adding "and (@Shopid = 0 or ShopID = @ShopID)" on your where statement. Whether this would be quicker, I doubt it
â Paul
Feb 6 at 16:29
You can do it as 1 query by using a left outer join and adding "and (@Shopid = 0 or ShopID = @ShopID)" on your where statement. Whether this would be quicker, I doubt it
â Paul
Feb 6 at 16:29
Sorry I did not understand what you said.
â I R Shad
Feb 7 at 6:29
Sorry I did not understand what you said.
â I R Shad
Feb 7 at 6:29
@Paul thank you for commenting, I tried that one, but its not working.
â I R Shad
Feb 7 at 6:48
@Paul thank you for commenting, I tried that one, but its not working.
â I R Shad
Feb 7 at 6:48
Your self-answer below is exactly what I just said.
â Paul
Feb 7 at 8:14
Your self-answer below is exactly what I just said.
â Paul
Feb 7 at 8:14
you said using
left outer join, but in that way on the two condition I get full list of itemcategoryâ I R Shad
Feb 7 at 8:33
you said using
left outer join, but in that way on the two condition I get full list of itemcategoryâ I R Shad
Feb 7 at 8:33
add a comment |Â
 1 Answer
 1
 
active
oldest
votes
up vote
1
down vote
I did something like this
CREATE PROCEDURE [dbo].[spGetItemCategory] (@ShopID INT)
AS
BEGIN
 SET NOCOUNT ON;
 SELECT ItemCategory.ItemCategoryID
 ,CategoryName
 FROM ItemCategory
 INNER JOIN ShopitemCategory ON (
 ShopitemCategory.shopid = @ShopID
 AND ShopitemCategory.itemcategoryid = ItemCategory.ItemCategoryID
 )
 OR (@ShopID = 0)
END
add a comment |Â
 1 Answer
 1
 
active
oldest
votes
 1 Answer
 1
 
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
I did something like this
CREATE PROCEDURE [dbo].[spGetItemCategory] (@ShopID INT)
AS
BEGIN
 SET NOCOUNT ON;
 SELECT ItemCategory.ItemCategoryID
 ,CategoryName
 FROM ItemCategory
 INNER JOIN ShopitemCategory ON (
 ShopitemCategory.shopid = @ShopID
 AND ShopitemCategory.itemcategoryid = ItemCategory.ItemCategoryID
 )
 OR (@ShopID = 0)
END
add a comment |Â
up vote
1
down vote
I did something like this
CREATE PROCEDURE [dbo].[spGetItemCategory] (@ShopID INT)
AS
BEGIN
 SET NOCOUNT ON;
 SELECT ItemCategory.ItemCategoryID
 ,CategoryName
 FROM ItemCategory
 INNER JOIN ShopitemCategory ON (
 ShopitemCategory.shopid = @ShopID
 AND ShopitemCategory.itemcategoryid = ItemCategory.ItemCategoryID
 )
 OR (@ShopID = 0)
END
add a comment |Â
up vote
1
down vote
up vote
1
down vote
I did something like this
CREATE PROCEDURE [dbo].[spGetItemCategory] (@ShopID INT)
AS
BEGIN
 SET NOCOUNT ON;
 SELECT ItemCategory.ItemCategoryID
 ,CategoryName
 FROM ItemCategory
 INNER JOIN ShopitemCategory ON (
 ShopitemCategory.shopid = @ShopID
 AND ShopitemCategory.itemcategoryid = ItemCategory.ItemCategoryID
 )
 OR (@ShopID = 0)
END
I did something like this
CREATE PROCEDURE [dbo].[spGetItemCategory] (@ShopID INT)
AS
BEGIN
 SET NOCOUNT ON;
 SELECT ItemCategory.ItemCategoryID
 ,CategoryName
 FROM ItemCategory
 INNER JOIN ShopitemCategory ON (
 ShopitemCategory.shopid = @ShopID
 AND ShopitemCategory.itemcategoryid = ItemCategory.ItemCategoryID
 )
 OR (@ShopID = 0)
END
answered Feb 7 at 7:34
I R Shad
849
849
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186884%2finner-join-only-if-satisfies-if-condition%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
You can do it as 1 query by using a left outer join and adding "and (@Shopid = 0 or ShopID = @ShopID)" on your where statement. Whether this would be quicker, I doubt it
â Paul
Feb 6 at 16:29
Sorry I did not understand what you said.
â I R Shad
Feb 7 at 6:29
@Paul thank you for commenting, I tried that one, but its not working.
â I R Shad
Feb 7 at 6:48
Your self-answer below is exactly what I just said.
â Paul
Feb 7 at 8:14
you said using
left outer join, but in that way on the two condition I get full list of itemcategoryâ I R Shad
Feb 7 at 8:33