while loop select in SQL Server










0















I'm sure this is easy but I'm still getting syntax errors when I run it in SQL Server Management Studio v17.4.



I have table T1 which has serial numbers, and I just need to iterate through and check how many times they appear in T2. I tried like



declare @serial int

select @serial = select serial from T1

while @serial
begin
select count(*) from T2 where Serial = @serial
end


But I get an error:




Incorrect syntax near the keyword 'select'




How to do it? Thanks.










share|improve this question
























  • Can't you just SELECT COUNT(*) FROM T2 WHERE Serial = @Serial?

    – Sami
    Nov 15 '18 at 18:58












  • No, some serials are repeated. I need to find which ones are repeated in T2.

    – Chuck Pedro
    Nov 15 '18 at 18:59











  • Are you after a list of those serial numbers from your T1 table where they are in your T2 table more than once?

    – Tim Mylott
    Nov 15 '18 at 19:24
















0















I'm sure this is easy but I'm still getting syntax errors when I run it in SQL Server Management Studio v17.4.



I have table T1 which has serial numbers, and I just need to iterate through and check how many times they appear in T2. I tried like



declare @serial int

select @serial = select serial from T1

while @serial
begin
select count(*) from T2 where Serial = @serial
end


But I get an error:




Incorrect syntax near the keyword 'select'




How to do it? Thanks.










share|improve this question
























  • Can't you just SELECT COUNT(*) FROM T2 WHERE Serial = @Serial?

    – Sami
    Nov 15 '18 at 18:58












  • No, some serials are repeated. I need to find which ones are repeated in T2.

    – Chuck Pedro
    Nov 15 '18 at 18:59











  • Are you after a list of those serial numbers from your T1 table where they are in your T2 table more than once?

    – Tim Mylott
    Nov 15 '18 at 19:24














0












0








0








I'm sure this is easy but I'm still getting syntax errors when I run it in SQL Server Management Studio v17.4.



I have table T1 which has serial numbers, and I just need to iterate through and check how many times they appear in T2. I tried like



declare @serial int

select @serial = select serial from T1

while @serial
begin
select count(*) from T2 where Serial = @serial
end


But I get an error:




Incorrect syntax near the keyword 'select'




How to do it? Thanks.










share|improve this question
















I'm sure this is easy but I'm still getting syntax errors when I run it in SQL Server Management Studio v17.4.



I have table T1 which has serial numbers, and I just need to iterate through and check how many times they appear in T2. I tried like



declare @serial int

select @serial = select serial from T1

while @serial
begin
select count(*) from T2 where Serial = @serial
end


But I get an error:




Incorrect syntax near the keyword 'select'




How to do it? Thanks.







sql sql-server while-loop






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 21:42









marc_s

580k13011191266




580k13011191266










asked Nov 15 '18 at 18:56









Chuck PedroChuck Pedro

8618




8618












  • Can't you just SELECT COUNT(*) FROM T2 WHERE Serial = @Serial?

    – Sami
    Nov 15 '18 at 18:58












  • No, some serials are repeated. I need to find which ones are repeated in T2.

    – Chuck Pedro
    Nov 15 '18 at 18:59











  • Are you after a list of those serial numbers from your T1 table where they are in your T2 table more than once?

    – Tim Mylott
    Nov 15 '18 at 19:24


















  • Can't you just SELECT COUNT(*) FROM T2 WHERE Serial = @Serial?

    – Sami
    Nov 15 '18 at 18:58












  • No, some serials are repeated. I need to find which ones are repeated in T2.

    – Chuck Pedro
    Nov 15 '18 at 18:59











  • Are you after a list of those serial numbers from your T1 table where they are in your T2 table more than once?

    – Tim Mylott
    Nov 15 '18 at 19:24

















Can't you just SELECT COUNT(*) FROM T2 WHERE Serial = @Serial?

– Sami
Nov 15 '18 at 18:58






Can't you just SELECT COUNT(*) FROM T2 WHERE Serial = @Serial?

– Sami
Nov 15 '18 at 18:58














No, some serials are repeated. I need to find which ones are repeated in T2.

– Chuck Pedro
Nov 15 '18 at 18:59





No, some serials are repeated. I need to find which ones are repeated in T2.

– Chuck Pedro
Nov 15 '18 at 18:59













Are you after a list of those serial numbers from your T1 table where they are in your T2 table more than once?

– Tim Mylott
Nov 15 '18 at 19:24






Are you after a list of those serial numbers from your T1 table where they are in your T2 table more than once?

– Tim Mylott
Nov 15 '18 at 19:24













5 Answers
5






active

oldest

votes


















1














First of all, doing:



select @serial = select serial from T1

while @serial




Doesn't mean that it will start to magically loop for every value of serial that T1 has, it will just assign one value of serial to your variable (if it was correctly written anyway select @serial = serial from T1).



What you want doesn't really make sense to do it in a loop; sql works in sets, and you should try to write your code accordingly. In this case, a simple JOIN should do:



SELECT T1.Serial,
ISNULL(COUNT(T2.Serial),0) N
FROM T1
LEFT JOIN T2
ON T1.Serial = T2.Serial
GROUP BY T1.Serial
;





share|improve this answer






























    2














    Instead of while loop, just join the tables and use an aggregate.
    Something like:



    select a.serial, count(a.serial) from t2 a
    inner join t1 b on b.serial = a.serial
    group by a.serial


    Without sample data I can't test it out for you, but that will perform a lot better for you.






    share|improve this answer






























      2














      Simply



       SELECT T1.Serial,
      COUNT(T2.Serial) AppearsInT2
      FROM T1 JOIN T2 ON T1.Serial = T2.Serial
      GROUP BY T1.Serial


      There is no need to declare a variable or to use a loop.



      If you want to return 0 for serials which not exists in the second table use LEFT JOIN instead



      SELECT T1.Serial,
      COUNT(T2.Serial) AppearsInT2
      FROM T1 LEFT JOIN T2 ON T1.Serial = T2.Serial
      GROUP BY T1.Serial;


      Simple Demo






      share|improve this answer
































        1














        Yes it is a syntax error



        select @serial = select serial from T1; -- is wrong it should as written below



        select @serial = serial from T1 ;



        This will select the 1st value from Table T1.



        This will remove the error but the query written will not yeald the required output.



        You need to loop through table T1 and for each value of T1 search in T2.



        See if the below simple query helps



        select serial, count(Serial) as SerilaCount from T1 inner join T2 on T1.serial =T2.Serial
        group by T1.serial.






        share|improve this answer






























          1














          Seems like you could just do this in one quick statement, rather than a loop.



          SELECT T2.Serial, Count(T2.Serial) as NumOfSerial
          FROM T1
          INNER JOIN T2 ON T1.Serial = T2.Serial
          GROUP BY T2.Serial





          share|improve this answer
























            Your Answer






            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: "1"
            ;
            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',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326191%2fwhile-loop-select-in-sql-server%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            5 Answers
            5






            active

            oldest

            votes








            5 Answers
            5






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            First of all, doing:



            select @serial = select serial from T1

            while @serial




            Doesn't mean that it will start to magically loop for every value of serial that T1 has, it will just assign one value of serial to your variable (if it was correctly written anyway select @serial = serial from T1).



            What you want doesn't really make sense to do it in a loop; sql works in sets, and you should try to write your code accordingly. In this case, a simple JOIN should do:



            SELECT T1.Serial,
            ISNULL(COUNT(T2.Serial),0) N
            FROM T1
            LEFT JOIN T2
            ON T1.Serial = T2.Serial
            GROUP BY T1.Serial
            ;





            share|improve this answer



























              1














              First of all, doing:



              select @serial = select serial from T1

              while @serial




              Doesn't mean that it will start to magically loop for every value of serial that T1 has, it will just assign one value of serial to your variable (if it was correctly written anyway select @serial = serial from T1).



              What you want doesn't really make sense to do it in a loop; sql works in sets, and you should try to write your code accordingly. In this case, a simple JOIN should do:



              SELECT T1.Serial,
              ISNULL(COUNT(T2.Serial),0) N
              FROM T1
              LEFT JOIN T2
              ON T1.Serial = T2.Serial
              GROUP BY T1.Serial
              ;





              share|improve this answer

























                1












                1








                1







                First of all, doing:



                select @serial = select serial from T1

                while @serial




                Doesn't mean that it will start to magically loop for every value of serial that T1 has, it will just assign one value of serial to your variable (if it was correctly written anyway select @serial = serial from T1).



                What you want doesn't really make sense to do it in a loop; sql works in sets, and you should try to write your code accordingly. In this case, a simple JOIN should do:



                SELECT T1.Serial,
                ISNULL(COUNT(T2.Serial),0) N
                FROM T1
                LEFT JOIN T2
                ON T1.Serial = T2.Serial
                GROUP BY T1.Serial
                ;





                share|improve this answer













                First of all, doing:



                select @serial = select serial from T1

                while @serial




                Doesn't mean that it will start to magically loop for every value of serial that T1 has, it will just assign one value of serial to your variable (if it was correctly written anyway select @serial = serial from T1).



                What you want doesn't really make sense to do it in a loop; sql works in sets, and you should try to write your code accordingly. In this case, a simple JOIN should do:



                SELECT T1.Serial,
                ISNULL(COUNT(T2.Serial),0) N
                FROM T1
                LEFT JOIN T2
                ON T1.Serial = T2.Serial
                GROUP BY T1.Serial
                ;






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 15 '18 at 19:03









                LamakLamak

                59.3k87998




                59.3k87998























                    2














                    Instead of while loop, just join the tables and use an aggregate.
                    Something like:



                    select a.serial, count(a.serial) from t2 a
                    inner join t1 b on b.serial = a.serial
                    group by a.serial


                    Without sample data I can't test it out for you, but that will perform a lot better for you.






                    share|improve this answer



























                      2














                      Instead of while loop, just join the tables and use an aggregate.
                      Something like:



                      select a.serial, count(a.serial) from t2 a
                      inner join t1 b on b.serial = a.serial
                      group by a.serial


                      Without sample data I can't test it out for you, but that will perform a lot better for you.






                      share|improve this answer

























                        2












                        2








                        2







                        Instead of while loop, just join the tables and use an aggregate.
                        Something like:



                        select a.serial, count(a.serial) from t2 a
                        inner join t1 b on b.serial = a.serial
                        group by a.serial


                        Without sample data I can't test it out for you, but that will perform a lot better for you.






                        share|improve this answer













                        Instead of while loop, just join the tables and use an aggregate.
                        Something like:



                        select a.serial, count(a.serial) from t2 a
                        inner join t1 b on b.serial = a.serial
                        group by a.serial


                        Without sample data I can't test it out for you, but that will perform a lot better for you.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Nov 15 '18 at 19:02









                        Tim MylottTim Mylott

                        1,20319




                        1,20319





















                            2














                            Simply



                             SELECT T1.Serial,
                            COUNT(T2.Serial) AppearsInT2
                            FROM T1 JOIN T2 ON T1.Serial = T2.Serial
                            GROUP BY T1.Serial


                            There is no need to declare a variable or to use a loop.



                            If you want to return 0 for serials which not exists in the second table use LEFT JOIN instead



                            SELECT T1.Serial,
                            COUNT(T2.Serial) AppearsInT2
                            FROM T1 LEFT JOIN T2 ON T1.Serial = T2.Serial
                            GROUP BY T1.Serial;


                            Simple Demo






                            share|improve this answer





























                              2














                              Simply



                               SELECT T1.Serial,
                              COUNT(T2.Serial) AppearsInT2
                              FROM T1 JOIN T2 ON T1.Serial = T2.Serial
                              GROUP BY T1.Serial


                              There is no need to declare a variable or to use a loop.



                              If you want to return 0 for serials which not exists in the second table use LEFT JOIN instead



                              SELECT T1.Serial,
                              COUNT(T2.Serial) AppearsInT2
                              FROM T1 LEFT JOIN T2 ON T1.Serial = T2.Serial
                              GROUP BY T1.Serial;


                              Simple Demo






                              share|improve this answer



























                                2












                                2








                                2







                                Simply



                                 SELECT T1.Serial,
                                COUNT(T2.Serial) AppearsInT2
                                FROM T1 JOIN T2 ON T1.Serial = T2.Serial
                                GROUP BY T1.Serial


                                There is no need to declare a variable or to use a loop.



                                If you want to return 0 for serials which not exists in the second table use LEFT JOIN instead



                                SELECT T1.Serial,
                                COUNT(T2.Serial) AppearsInT2
                                FROM T1 LEFT JOIN T2 ON T1.Serial = T2.Serial
                                GROUP BY T1.Serial;


                                Simple Demo






                                share|improve this answer















                                Simply



                                 SELECT T1.Serial,
                                COUNT(T2.Serial) AppearsInT2
                                FROM T1 JOIN T2 ON T1.Serial = T2.Serial
                                GROUP BY T1.Serial


                                There is no need to declare a variable or to use a loop.



                                If you want to return 0 for serials which not exists in the second table use LEFT JOIN instead



                                SELECT T1.Serial,
                                COUNT(T2.Serial) AppearsInT2
                                FROM T1 LEFT JOIN T2 ON T1.Serial = T2.Serial
                                GROUP BY T1.Serial;


                                Simple Demo







                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Nov 15 '18 at 19:09

























                                answered Nov 15 '18 at 19:03









                                SamiSami

                                8,98831242




                                8,98831242





















                                    1














                                    Yes it is a syntax error



                                    select @serial = select serial from T1; -- is wrong it should as written below



                                    select @serial = serial from T1 ;



                                    This will select the 1st value from Table T1.



                                    This will remove the error but the query written will not yeald the required output.



                                    You need to loop through table T1 and for each value of T1 search in T2.



                                    See if the below simple query helps



                                    select serial, count(Serial) as SerilaCount from T1 inner join T2 on T1.serial =T2.Serial
                                    group by T1.serial.






                                    share|improve this answer



























                                      1














                                      Yes it is a syntax error



                                      select @serial = select serial from T1; -- is wrong it should as written below



                                      select @serial = serial from T1 ;



                                      This will select the 1st value from Table T1.



                                      This will remove the error but the query written will not yeald the required output.



                                      You need to loop through table T1 and for each value of T1 search in T2.



                                      See if the below simple query helps



                                      select serial, count(Serial) as SerilaCount from T1 inner join T2 on T1.serial =T2.Serial
                                      group by T1.serial.






                                      share|improve this answer

























                                        1












                                        1








                                        1







                                        Yes it is a syntax error



                                        select @serial = select serial from T1; -- is wrong it should as written below



                                        select @serial = serial from T1 ;



                                        This will select the 1st value from Table T1.



                                        This will remove the error but the query written will not yeald the required output.



                                        You need to loop through table T1 and for each value of T1 search in T2.



                                        See if the below simple query helps



                                        select serial, count(Serial) as SerilaCount from T1 inner join T2 on T1.serial =T2.Serial
                                        group by T1.serial.






                                        share|improve this answer













                                        Yes it is a syntax error



                                        select @serial = select serial from T1; -- is wrong it should as written below



                                        select @serial = serial from T1 ;



                                        This will select the 1st value from Table T1.



                                        This will remove the error but the query written will not yeald the required output.



                                        You need to loop through table T1 and for each value of T1 search in T2.



                                        See if the below simple query helps



                                        select serial, count(Serial) as SerilaCount from T1 inner join T2 on T1.serial =T2.Serial
                                        group by T1.serial.







                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Nov 15 '18 at 19:15









                                        RoshanRoshan

                                        212




                                        212





















                                            1














                                            Seems like you could just do this in one quick statement, rather than a loop.



                                            SELECT T2.Serial, Count(T2.Serial) as NumOfSerial
                                            FROM T1
                                            INNER JOIN T2 ON T1.Serial = T2.Serial
                                            GROUP BY T2.Serial





                                            share|improve this answer





























                                              1














                                              Seems like you could just do this in one quick statement, rather than a loop.



                                              SELECT T2.Serial, Count(T2.Serial) as NumOfSerial
                                              FROM T1
                                              INNER JOIN T2 ON T1.Serial = T2.Serial
                                              GROUP BY T2.Serial





                                              share|improve this answer



























                                                1












                                                1








                                                1







                                                Seems like you could just do this in one quick statement, rather than a loop.



                                                SELECT T2.Serial, Count(T2.Serial) as NumOfSerial
                                                FROM T1
                                                INNER JOIN T2 ON T1.Serial = T2.Serial
                                                GROUP BY T2.Serial





                                                share|improve this answer















                                                Seems like you could just do this in one quick statement, rather than a loop.



                                                SELECT T2.Serial, Count(T2.Serial) as NumOfSerial
                                                FROM T1
                                                INNER JOIN T2 ON T1.Serial = T2.Serial
                                                GROUP BY T2.Serial






                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Nov 15 '18 at 19:30









                                                Sami

                                                8,98831242




                                                8,98831242










                                                answered Nov 15 '18 at 19:29









                                                Brian SingletonBrian Singleton

                                                112




                                                112



























                                                    draft saved

                                                    draft discarded
















































                                                    Thanks for contributing an answer to Stack Overflow!


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid


                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.

                                                    To learn more, see our tips on writing great answers.




                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function ()
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326191%2fwhile-loop-select-in-sql-server%23new-answer', 'question_page');

                                                    );

                                                    Post as a guest















                                                    Required, but never shown





















































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown

































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown







                                                    Popular posts from this blog

                                                    27

                                                    Top Tejano songwriter Luis Silva dead of heart attack at 64

                                                    Category:Rhetoric