Execute new query when current query finalises












1















I am in the process of inserting some data into a SQL Server (2016). Since this is a manual one-time load, I have not bothered spending a lot of time writing it the most efficient way; however some of the loads takes much longer than initially anticipated - so here is my question.



Can I somehow tell the server to execute a nother query immediately after the current one finalises? I would have done this manually by hitting F5 on the next part of the code - but since the load takes longer than expected, I would like the server to somehow execute the next part of the code automatically, when the first part finalises.



Is this at all possible without either 1) waiting for the current query to finalise or 2) cancelling the current query and re-running the whole lot at once including my next steps of my code?



Hope this makes sense!



Thanks :-)










share|improve this question



























    1















    I am in the process of inserting some data into a SQL Server (2016). Since this is a manual one-time load, I have not bothered spending a lot of time writing it the most efficient way; however some of the loads takes much longer than initially anticipated - so here is my question.



    Can I somehow tell the server to execute a nother query immediately after the current one finalises? I would have done this manually by hitting F5 on the next part of the code - but since the load takes longer than expected, I would like the server to somehow execute the next part of the code automatically, when the first part finalises.



    Is this at all possible without either 1) waiting for the current query to finalise or 2) cancelling the current query and re-running the whole lot at once including my next steps of my code?



    Hope this makes sense!



    Thanks :-)










    share|improve this question

























      1












      1








      1








      I am in the process of inserting some data into a SQL Server (2016). Since this is a manual one-time load, I have not bothered spending a lot of time writing it the most efficient way; however some of the loads takes much longer than initially anticipated - so here is my question.



      Can I somehow tell the server to execute a nother query immediately after the current one finalises? I would have done this manually by hitting F5 on the next part of the code - but since the load takes longer than expected, I would like the server to somehow execute the next part of the code automatically, when the first part finalises.



      Is this at all possible without either 1) waiting for the current query to finalise or 2) cancelling the current query and re-running the whole lot at once including my next steps of my code?



      Hope this makes sense!



      Thanks :-)










      share|improve this question














      I am in the process of inserting some data into a SQL Server (2016). Since this is a manual one-time load, I have not bothered spending a lot of time writing it the most efficient way; however some of the loads takes much longer than initially anticipated - so here is my question.



      Can I somehow tell the server to execute a nother query immediately after the current one finalises? I would have done this manually by hitting F5 on the next part of the code - but since the load takes longer than expected, I would like the server to somehow execute the next part of the code automatically, when the first part finalises.



      Is this at all possible without either 1) waiting for the current query to finalise or 2) cancelling the current query and re-running the whole lot at once including my next steps of my code?



      Hope this makes sense!



      Thanks :-)







      sql-server






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 7 hours ago









      ssnssn

      1234




      1234






















          3 Answers
          3






          active

          oldest

          votes


















          3














          For your specific situation: no, there's not a straightforward way to do what you want to do.




          I would have done this manually by hitting F5 on the next part of the code...




          From this I gather you have one big script with many statements, and you're running it one statement at a time by highlighting the statement and pressing "F5."




          Is this at all possible without either 1) waiting for the current query to finalise or 2) cancelling the current query and re-running the whole lot at once including my next steps of my code?




          No, that SSMS window (session) is tied up running that query, and you can't use it again until either the query finishes, or you cancel it.



          There are ways that you could try to monitor for that session going idle (with a SQL Server Agent job for instance), and then run another query. But it's probably not worth the effort and risk of getting it wrong.



          So I would just wait until the current query is finished, and then you can highlight the rest of the script and run it - each statement will automatically run one after the other.






          share|improve this answer































            1














            Assuming it is just a single statement that you are waiting to complete, you can first run the following to get the start time of the current request



            SELECT start_time FROM sys.dm_exec_requests WHERE session_id = <<spid_of_other_connection>>


            And then having got the start_time do some simple polling in another SSMS window



            SET NOCOUNT ON;

            WHILE EXISTS
            (SELECT *
            FROM sys.dm_exec_requests
            WHERE session_id = <<spid_of_other_connection>>
            AND start_time = <<start_time_from_above>>)
            BEGIN
            WAITFOR DELAY '00:00:10'
            END

            PRINT 'DO SOMETHING HERE';


            The filter on start_time rather than just session_id is because the session_id can be recycled if the original connection is closed.






            share|improve this answer































              0














              A possible solution would be to open up another SSMS window and put at the very top a very specific update against the table you're trying to load - a row that you know is going to be locked for the entire run of the first query - until the current locks are released, the second session will wait - then follow the update with your second query. – Scott Hodgin






              share|improve this answer























                Your Answer








                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "182"
                };
                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: false,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                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%2fdba.stackexchange.com%2fquestions%2f230074%2fexecute-new-query-when-current-query-finalises%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                3














                For your specific situation: no, there's not a straightforward way to do what you want to do.




                I would have done this manually by hitting F5 on the next part of the code...




                From this I gather you have one big script with many statements, and you're running it one statement at a time by highlighting the statement and pressing "F5."




                Is this at all possible without either 1) waiting for the current query to finalise or 2) cancelling the current query and re-running the whole lot at once including my next steps of my code?




                No, that SSMS window (session) is tied up running that query, and you can't use it again until either the query finishes, or you cancel it.



                There are ways that you could try to monitor for that session going idle (with a SQL Server Agent job for instance), and then run another query. But it's probably not worth the effort and risk of getting it wrong.



                So I would just wait until the current query is finished, and then you can highlight the rest of the script and run it - each statement will automatically run one after the other.






                share|improve this answer




























                  3














                  For your specific situation: no, there's not a straightforward way to do what you want to do.




                  I would have done this manually by hitting F5 on the next part of the code...




                  From this I gather you have one big script with many statements, and you're running it one statement at a time by highlighting the statement and pressing "F5."




                  Is this at all possible without either 1) waiting for the current query to finalise or 2) cancelling the current query and re-running the whole lot at once including my next steps of my code?




                  No, that SSMS window (session) is tied up running that query, and you can't use it again until either the query finishes, or you cancel it.



                  There are ways that you could try to monitor for that session going idle (with a SQL Server Agent job for instance), and then run another query. But it's probably not worth the effort and risk of getting it wrong.



                  So I would just wait until the current query is finished, and then you can highlight the rest of the script and run it - each statement will automatically run one after the other.






                  share|improve this answer


























                    3












                    3








                    3







                    For your specific situation: no, there's not a straightforward way to do what you want to do.




                    I would have done this manually by hitting F5 on the next part of the code...




                    From this I gather you have one big script with many statements, and you're running it one statement at a time by highlighting the statement and pressing "F5."




                    Is this at all possible without either 1) waiting for the current query to finalise or 2) cancelling the current query and re-running the whole lot at once including my next steps of my code?




                    No, that SSMS window (session) is tied up running that query, and you can't use it again until either the query finishes, or you cancel it.



                    There are ways that you could try to monitor for that session going idle (with a SQL Server Agent job for instance), and then run another query. But it's probably not worth the effort and risk of getting it wrong.



                    So I would just wait until the current query is finished, and then you can highlight the rest of the script and run it - each statement will automatically run one after the other.






                    share|improve this answer













                    For your specific situation: no, there's not a straightforward way to do what you want to do.




                    I would have done this manually by hitting F5 on the next part of the code...




                    From this I gather you have one big script with many statements, and you're running it one statement at a time by highlighting the statement and pressing "F5."




                    Is this at all possible without either 1) waiting for the current query to finalise or 2) cancelling the current query and re-running the whole lot at once including my next steps of my code?




                    No, that SSMS window (session) is tied up running that query, and you can't use it again until either the query finishes, or you cancel it.



                    There are ways that you could try to monitor for that session going idle (with a SQL Server Agent job for instance), and then run another query. But it's probably not worth the effort and risk of getting it wrong.



                    So I would just wait until the current query is finished, and then you can highlight the rest of the script and run it - each statement will automatically run one after the other.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 6 hours ago









                    jadarnel27jadarnel27

                    5,27811736




                    5,27811736

























                        1














                        Assuming it is just a single statement that you are waiting to complete, you can first run the following to get the start time of the current request



                        SELECT start_time FROM sys.dm_exec_requests WHERE session_id = <<spid_of_other_connection>>


                        And then having got the start_time do some simple polling in another SSMS window



                        SET NOCOUNT ON;

                        WHILE EXISTS
                        (SELECT *
                        FROM sys.dm_exec_requests
                        WHERE session_id = <<spid_of_other_connection>>
                        AND start_time = <<start_time_from_above>>)
                        BEGIN
                        WAITFOR DELAY '00:00:10'
                        END

                        PRINT 'DO SOMETHING HERE';


                        The filter on start_time rather than just session_id is because the session_id can be recycled if the original connection is closed.






                        share|improve this answer




























                          1














                          Assuming it is just a single statement that you are waiting to complete, you can first run the following to get the start time of the current request



                          SELECT start_time FROM sys.dm_exec_requests WHERE session_id = <<spid_of_other_connection>>


                          And then having got the start_time do some simple polling in another SSMS window



                          SET NOCOUNT ON;

                          WHILE EXISTS
                          (SELECT *
                          FROM sys.dm_exec_requests
                          WHERE session_id = <<spid_of_other_connection>>
                          AND start_time = <<start_time_from_above>>)
                          BEGIN
                          WAITFOR DELAY '00:00:10'
                          END

                          PRINT 'DO SOMETHING HERE';


                          The filter on start_time rather than just session_id is because the session_id can be recycled if the original connection is closed.






                          share|improve this answer


























                            1












                            1








                            1







                            Assuming it is just a single statement that you are waiting to complete, you can first run the following to get the start time of the current request



                            SELECT start_time FROM sys.dm_exec_requests WHERE session_id = <<spid_of_other_connection>>


                            And then having got the start_time do some simple polling in another SSMS window



                            SET NOCOUNT ON;

                            WHILE EXISTS
                            (SELECT *
                            FROM sys.dm_exec_requests
                            WHERE session_id = <<spid_of_other_connection>>
                            AND start_time = <<start_time_from_above>>)
                            BEGIN
                            WAITFOR DELAY '00:00:10'
                            END

                            PRINT 'DO SOMETHING HERE';


                            The filter on start_time rather than just session_id is because the session_id can be recycled if the original connection is closed.






                            share|improve this answer













                            Assuming it is just a single statement that you are waiting to complete, you can first run the following to get the start time of the current request



                            SELECT start_time FROM sys.dm_exec_requests WHERE session_id = <<spid_of_other_connection>>


                            And then having got the start_time do some simple polling in another SSMS window



                            SET NOCOUNT ON;

                            WHILE EXISTS
                            (SELECT *
                            FROM sys.dm_exec_requests
                            WHERE session_id = <<spid_of_other_connection>>
                            AND start_time = <<start_time_from_above>>)
                            BEGIN
                            WAITFOR DELAY '00:00:10'
                            END

                            PRINT 'DO SOMETHING HERE';


                            The filter on start_time rather than just session_id is because the session_id can be recycled if the original connection is closed.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 2 hours ago









                            Martin SmithMartin Smith

                            63.3k10170254




                            63.3k10170254























                                0














                                A possible solution would be to open up another SSMS window and put at the very top a very specific update against the table you're trying to load - a row that you know is going to be locked for the entire run of the first query - until the current locks are released, the second session will wait - then follow the update with your second query. – Scott Hodgin






                                share|improve this answer




























                                  0














                                  A possible solution would be to open up another SSMS window and put at the very top a very specific update against the table you're trying to load - a row that you know is going to be locked for the entire run of the first query - until the current locks are released, the second session will wait - then follow the update with your second query. – Scott Hodgin






                                  share|improve this answer


























                                    0












                                    0








                                    0







                                    A possible solution would be to open up another SSMS window and put at the very top a very specific update against the table you're trying to load - a row that you know is going to be locked for the entire run of the first query - until the current locks are released, the second session will wait - then follow the update with your second query. – Scott Hodgin






                                    share|improve this answer













                                    A possible solution would be to open up another SSMS window and put at the very top a very specific update against the table you're trying to load - a row that you know is going to be locked for the entire run of the first query - until the current locks are released, the second session will wait - then follow the update with your second query. – Scott Hodgin







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered 1 hour ago









                                    Comment ConverterComment Converter

                                    1,2271325




                                    1,2271325






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Database Administrators Stack Exchange!


                                        • 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%2fdba.stackexchange.com%2fquestions%2f230074%2fexecute-new-query-when-current-query-finalises%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

                                        "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                                        Alcedinidae

                                        RAC Tourist Trophy