Execute new query when current query finalises
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
add a comment |
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
add a comment |
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
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
sql-server
asked 7 hours ago
ssnssn
1234
1234
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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.
add a comment |
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.
add a comment |
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
add a comment |
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
});
}
});
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
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered 6 hours ago
jadarnel27jadarnel27
5,27811736
5,27811736
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered 2 hours ago
Martin SmithMartin Smith
63.3k10170254
63.3k10170254
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered 1 hour ago
Comment ConverterComment Converter
1,2271325
1,2271325
add a comment |
add a comment |
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.
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
Required, but never shown
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
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
Required, but never shown
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
Required, but never shown
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
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