Inner Join only if satisfies If condition

The name of the pictureThe name of the pictureThe name of the pictureClash 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?







share|improve this question





















  • 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

















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?







share|improve this question





















  • 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













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?







share|improve this question













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?









share|improve this question












share|improve this question




share|improve this question








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

















  • 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











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





share|improve this answer





















    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    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






























    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





    share|improve this answer

























      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





      share|improve this answer























        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





        share|improve this answer













        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






        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Feb 7 at 7:34









        I R Shad

        849




        849






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            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













































































            Popular posts from this blog

            Chat program with C++ and SFML

            Function to Return a JSON Like Objects Using VBA Collections and Arrays

            Will my employers contract hold up in court?