Get a while in a while SQL










0















I'd like to be able to use a while in a while in SQL, so this is my query:



DECLARE My_Cursor CURSOR LOCAL FOR 
SELECT
TimeByDay, Matricule, ResourceNTAccount, CommandNumber,
Post, Actuals, ResourceEmailAddress
FROM
#Results
WHERE
Matricule IS NOT NULL
ORDER BY
TimeByDay, Matricule

OPEN My_Cursor

FETCH NEXT FROM My_Cursor INTO @TimeByDay, @Matricule, @ResourceNTAccount, @CommandNumber, @Post, @Actuals, @ResourceEmailAddress

WHILE @@FETCH_STATUS = 0
BEGIN
DROP TABLE IF EXISTS #POTemp

SELECT *
INTO #POTemp
FROM #Commands
WHERE ResourceWorkEmail = @ResourceEmailAddress
AND @TimeByDay BETWEEN ValidFrom AND ValidTo

DECLARE My_Cursor1 CURSOR LOCAL FOR
SELECT
RemainingHours, CommandNumber, Post, ResourceWorkEmail,
ValidFrom, ValidTo, COObject, Actuals
FROM
#POTemp
WHERE
@Actuals > 0
ORDER BY
ValidFrom

OPEN My_Cursor1

FETCH NEXT FROM My_Cursor1 INTO @RemainingHours, @CommandNumberPO, @PostPO, @ResourceWorkEmail, @ValidFrom, @ValidTo, @COObject, @ActualsPO

WHILE @@FETCH_STATUS = 0
BEGIN
IF @RemainingHours - @Actuals > 0
UPDATE #Commands
SET [Actuals] = @Actuals;
--SET @Actuals=0;
ELSE
UPDATE #ApprovedLines
SET [Actuals] = @Actuals

UPDATE #Commands
SET [ACTUALS] = @Actuals - @RemainingHours

FETCH NEXT FROM My_Cursor1 INTO @RemainingHours, @CommandNumberPO, @PostPO, @ResourceWorkEmail, @ValidFrom, @ValidTo, @COObject, @ActualsPO
CLOSE My_Cursor1
DEALLOCATE My_Cursor1
END

FETCH NEXT FROM My_Cursor INTO @TimeByDay, @Matricule, @ResourceNTAccount, @CommandNumber, @Post, @Actuals, @ResourceEmailAddress
CLOSE My_Cursor
DEALLOCATE My_Cursor
END


But when I execute my query I have the following error messages:




A cursor with the name 'My_Cursor1' already exists.



Msg 16905, Level 16, State 1, Line 179

The cursor is already open.



Msg 16916, Level 16, State 1, Line 198

A cursor with the name 'My_Cursor' does not exist.



Msg 16916, Level 16, State 1, Line 199

A cursor with the name 'My_Cursor' does not exist.



Msg 16916, Level 16, State 1, Line 200

A cursor with the name 'My_Cursor' does not exist.




Any idea of what I'm doing wrong?










share|improve this question



















  • 4





    Yes!!! What you are doing wrong is using loops here in the first place. Nested cursors for nothing more than updating data is completely the wrong way to go about this. You need to do this set based instead.

    – Sean Lange
    Nov 14 '18 at 15:04







  • 2





    Now that your code is indented a little bit, you can probably see what is wrong.

    – GSerg
    Nov 14 '18 at 15:09











  • Don't use cursors. SQL is a set-based language. Cursors are needed only in very rare cases and perform at least N times slower than any query affecting N rows. They are also far more difficult to write than a proper query

    – Panagiotis Kanavos
    Nov 14 '18 at 15:17







  • 3





    While the formatting should shed some light on the logical issue here the best way to fix this is to get this out of RBAR (row by agonizing row) logic. This looks like the entire nested cursor structure could be replaced with a single update statement using some aggregation.

    – Sean Lange
    Nov 14 '18 at 15:18











  • If you want to update some rows based on a query write an UPDATE ... FROM ...WHERE query

    – Panagiotis Kanavos
    Nov 14 '18 at 15:18
















0















I'd like to be able to use a while in a while in SQL, so this is my query:



DECLARE My_Cursor CURSOR LOCAL FOR 
SELECT
TimeByDay, Matricule, ResourceNTAccount, CommandNumber,
Post, Actuals, ResourceEmailAddress
FROM
#Results
WHERE
Matricule IS NOT NULL
ORDER BY
TimeByDay, Matricule

OPEN My_Cursor

FETCH NEXT FROM My_Cursor INTO @TimeByDay, @Matricule, @ResourceNTAccount, @CommandNumber, @Post, @Actuals, @ResourceEmailAddress

WHILE @@FETCH_STATUS = 0
BEGIN
DROP TABLE IF EXISTS #POTemp

SELECT *
INTO #POTemp
FROM #Commands
WHERE ResourceWorkEmail = @ResourceEmailAddress
AND @TimeByDay BETWEEN ValidFrom AND ValidTo

DECLARE My_Cursor1 CURSOR LOCAL FOR
SELECT
RemainingHours, CommandNumber, Post, ResourceWorkEmail,
ValidFrom, ValidTo, COObject, Actuals
FROM
#POTemp
WHERE
@Actuals > 0
ORDER BY
ValidFrom

OPEN My_Cursor1

FETCH NEXT FROM My_Cursor1 INTO @RemainingHours, @CommandNumberPO, @PostPO, @ResourceWorkEmail, @ValidFrom, @ValidTo, @COObject, @ActualsPO

WHILE @@FETCH_STATUS = 0
BEGIN
IF @RemainingHours - @Actuals > 0
UPDATE #Commands
SET [Actuals] = @Actuals;
--SET @Actuals=0;
ELSE
UPDATE #ApprovedLines
SET [Actuals] = @Actuals

UPDATE #Commands
SET [ACTUALS] = @Actuals - @RemainingHours

FETCH NEXT FROM My_Cursor1 INTO @RemainingHours, @CommandNumberPO, @PostPO, @ResourceWorkEmail, @ValidFrom, @ValidTo, @COObject, @ActualsPO
CLOSE My_Cursor1
DEALLOCATE My_Cursor1
END

FETCH NEXT FROM My_Cursor INTO @TimeByDay, @Matricule, @ResourceNTAccount, @CommandNumber, @Post, @Actuals, @ResourceEmailAddress
CLOSE My_Cursor
DEALLOCATE My_Cursor
END


But when I execute my query I have the following error messages:




A cursor with the name 'My_Cursor1' already exists.



Msg 16905, Level 16, State 1, Line 179

The cursor is already open.



Msg 16916, Level 16, State 1, Line 198

A cursor with the name 'My_Cursor' does not exist.



Msg 16916, Level 16, State 1, Line 199

A cursor with the name 'My_Cursor' does not exist.



Msg 16916, Level 16, State 1, Line 200

A cursor with the name 'My_Cursor' does not exist.




Any idea of what I'm doing wrong?










share|improve this question



















  • 4





    Yes!!! What you are doing wrong is using loops here in the first place. Nested cursors for nothing more than updating data is completely the wrong way to go about this. You need to do this set based instead.

    – Sean Lange
    Nov 14 '18 at 15:04







  • 2





    Now that your code is indented a little bit, you can probably see what is wrong.

    – GSerg
    Nov 14 '18 at 15:09











  • Don't use cursors. SQL is a set-based language. Cursors are needed only in very rare cases and perform at least N times slower than any query affecting N rows. They are also far more difficult to write than a proper query

    – Panagiotis Kanavos
    Nov 14 '18 at 15:17







  • 3





    While the formatting should shed some light on the logical issue here the best way to fix this is to get this out of RBAR (row by agonizing row) logic. This looks like the entire nested cursor structure could be replaced with a single update statement using some aggregation.

    – Sean Lange
    Nov 14 '18 at 15:18











  • If you want to update some rows based on a query write an UPDATE ... FROM ...WHERE query

    – Panagiotis Kanavos
    Nov 14 '18 at 15:18














0












0








0








I'd like to be able to use a while in a while in SQL, so this is my query:



DECLARE My_Cursor CURSOR LOCAL FOR 
SELECT
TimeByDay, Matricule, ResourceNTAccount, CommandNumber,
Post, Actuals, ResourceEmailAddress
FROM
#Results
WHERE
Matricule IS NOT NULL
ORDER BY
TimeByDay, Matricule

OPEN My_Cursor

FETCH NEXT FROM My_Cursor INTO @TimeByDay, @Matricule, @ResourceNTAccount, @CommandNumber, @Post, @Actuals, @ResourceEmailAddress

WHILE @@FETCH_STATUS = 0
BEGIN
DROP TABLE IF EXISTS #POTemp

SELECT *
INTO #POTemp
FROM #Commands
WHERE ResourceWorkEmail = @ResourceEmailAddress
AND @TimeByDay BETWEEN ValidFrom AND ValidTo

DECLARE My_Cursor1 CURSOR LOCAL FOR
SELECT
RemainingHours, CommandNumber, Post, ResourceWorkEmail,
ValidFrom, ValidTo, COObject, Actuals
FROM
#POTemp
WHERE
@Actuals > 0
ORDER BY
ValidFrom

OPEN My_Cursor1

FETCH NEXT FROM My_Cursor1 INTO @RemainingHours, @CommandNumberPO, @PostPO, @ResourceWorkEmail, @ValidFrom, @ValidTo, @COObject, @ActualsPO

WHILE @@FETCH_STATUS = 0
BEGIN
IF @RemainingHours - @Actuals > 0
UPDATE #Commands
SET [Actuals] = @Actuals;
--SET @Actuals=0;
ELSE
UPDATE #ApprovedLines
SET [Actuals] = @Actuals

UPDATE #Commands
SET [ACTUALS] = @Actuals - @RemainingHours

FETCH NEXT FROM My_Cursor1 INTO @RemainingHours, @CommandNumberPO, @PostPO, @ResourceWorkEmail, @ValidFrom, @ValidTo, @COObject, @ActualsPO
CLOSE My_Cursor1
DEALLOCATE My_Cursor1
END

FETCH NEXT FROM My_Cursor INTO @TimeByDay, @Matricule, @ResourceNTAccount, @CommandNumber, @Post, @Actuals, @ResourceEmailAddress
CLOSE My_Cursor
DEALLOCATE My_Cursor
END


But when I execute my query I have the following error messages:




A cursor with the name 'My_Cursor1' already exists.



Msg 16905, Level 16, State 1, Line 179

The cursor is already open.



Msg 16916, Level 16, State 1, Line 198

A cursor with the name 'My_Cursor' does not exist.



Msg 16916, Level 16, State 1, Line 199

A cursor with the name 'My_Cursor' does not exist.



Msg 16916, Level 16, State 1, Line 200

A cursor with the name 'My_Cursor' does not exist.




Any idea of what I'm doing wrong?










share|improve this question
















I'd like to be able to use a while in a while in SQL, so this is my query:



DECLARE My_Cursor CURSOR LOCAL FOR 
SELECT
TimeByDay, Matricule, ResourceNTAccount, CommandNumber,
Post, Actuals, ResourceEmailAddress
FROM
#Results
WHERE
Matricule IS NOT NULL
ORDER BY
TimeByDay, Matricule

OPEN My_Cursor

FETCH NEXT FROM My_Cursor INTO @TimeByDay, @Matricule, @ResourceNTAccount, @CommandNumber, @Post, @Actuals, @ResourceEmailAddress

WHILE @@FETCH_STATUS = 0
BEGIN
DROP TABLE IF EXISTS #POTemp

SELECT *
INTO #POTemp
FROM #Commands
WHERE ResourceWorkEmail = @ResourceEmailAddress
AND @TimeByDay BETWEEN ValidFrom AND ValidTo

DECLARE My_Cursor1 CURSOR LOCAL FOR
SELECT
RemainingHours, CommandNumber, Post, ResourceWorkEmail,
ValidFrom, ValidTo, COObject, Actuals
FROM
#POTemp
WHERE
@Actuals > 0
ORDER BY
ValidFrom

OPEN My_Cursor1

FETCH NEXT FROM My_Cursor1 INTO @RemainingHours, @CommandNumberPO, @PostPO, @ResourceWorkEmail, @ValidFrom, @ValidTo, @COObject, @ActualsPO

WHILE @@FETCH_STATUS = 0
BEGIN
IF @RemainingHours - @Actuals > 0
UPDATE #Commands
SET [Actuals] = @Actuals;
--SET @Actuals=0;
ELSE
UPDATE #ApprovedLines
SET [Actuals] = @Actuals

UPDATE #Commands
SET [ACTUALS] = @Actuals - @RemainingHours

FETCH NEXT FROM My_Cursor1 INTO @RemainingHours, @CommandNumberPO, @PostPO, @ResourceWorkEmail, @ValidFrom, @ValidTo, @COObject, @ActualsPO
CLOSE My_Cursor1
DEALLOCATE My_Cursor1
END

FETCH NEXT FROM My_Cursor INTO @TimeByDay, @Matricule, @ResourceNTAccount, @CommandNumber, @Post, @Actuals, @ResourceEmailAddress
CLOSE My_Cursor
DEALLOCATE My_Cursor
END


But when I execute my query I have the following error messages:




A cursor with the name 'My_Cursor1' already exists.



Msg 16905, Level 16, State 1, Line 179

The cursor is already open.



Msg 16916, Level 16, State 1, Line 198

A cursor with the name 'My_Cursor' does not exist.



Msg 16916, Level 16, State 1, Line 199

A cursor with the name 'My_Cursor' does not exist.



Msg 16916, Level 16, State 1, Line 200

A cursor with the name 'My_Cursor' does not exist.




Any idea of what I'm doing wrong?







sql-server while-loop






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 15:15









marc_s

581k13011211268




581k13011211268










asked Nov 14 '18 at 15:02









ToxicboumboumToxicboumboum

56




56







  • 4





    Yes!!! What you are doing wrong is using loops here in the first place. Nested cursors for nothing more than updating data is completely the wrong way to go about this. You need to do this set based instead.

    – Sean Lange
    Nov 14 '18 at 15:04







  • 2





    Now that your code is indented a little bit, you can probably see what is wrong.

    – GSerg
    Nov 14 '18 at 15:09











  • Don't use cursors. SQL is a set-based language. Cursors are needed only in very rare cases and perform at least N times slower than any query affecting N rows. They are also far more difficult to write than a proper query

    – Panagiotis Kanavos
    Nov 14 '18 at 15:17







  • 3





    While the formatting should shed some light on the logical issue here the best way to fix this is to get this out of RBAR (row by agonizing row) logic. This looks like the entire nested cursor structure could be replaced with a single update statement using some aggregation.

    – Sean Lange
    Nov 14 '18 at 15:18











  • If you want to update some rows based on a query write an UPDATE ... FROM ...WHERE query

    – Panagiotis Kanavos
    Nov 14 '18 at 15:18













  • 4





    Yes!!! What you are doing wrong is using loops here in the first place. Nested cursors for nothing more than updating data is completely the wrong way to go about this. You need to do this set based instead.

    – Sean Lange
    Nov 14 '18 at 15:04







  • 2





    Now that your code is indented a little bit, you can probably see what is wrong.

    – GSerg
    Nov 14 '18 at 15:09











  • Don't use cursors. SQL is a set-based language. Cursors are needed only in very rare cases and perform at least N times slower than any query affecting N rows. They are also far more difficult to write than a proper query

    – Panagiotis Kanavos
    Nov 14 '18 at 15:17







  • 3





    While the formatting should shed some light on the logical issue here the best way to fix this is to get this out of RBAR (row by agonizing row) logic. This looks like the entire nested cursor structure could be replaced with a single update statement using some aggregation.

    – Sean Lange
    Nov 14 '18 at 15:18











  • If you want to update some rows based on a query write an UPDATE ... FROM ...WHERE query

    – Panagiotis Kanavos
    Nov 14 '18 at 15:18








4




4





Yes!!! What you are doing wrong is using loops here in the first place. Nested cursors for nothing more than updating data is completely the wrong way to go about this. You need to do this set based instead.

– Sean Lange
Nov 14 '18 at 15:04






Yes!!! What you are doing wrong is using loops here in the first place. Nested cursors for nothing more than updating data is completely the wrong way to go about this. You need to do this set based instead.

– Sean Lange
Nov 14 '18 at 15:04





2




2





Now that your code is indented a little bit, you can probably see what is wrong.

– GSerg
Nov 14 '18 at 15:09





Now that your code is indented a little bit, you can probably see what is wrong.

– GSerg
Nov 14 '18 at 15:09













Don't use cursors. SQL is a set-based language. Cursors are needed only in very rare cases and perform at least N times slower than any query affecting N rows. They are also far more difficult to write than a proper query

– Panagiotis Kanavos
Nov 14 '18 at 15:17






Don't use cursors. SQL is a set-based language. Cursors are needed only in very rare cases and perform at least N times slower than any query affecting N rows. They are also far more difficult to write than a proper query

– Panagiotis Kanavos
Nov 14 '18 at 15:17





3




3





While the formatting should shed some light on the logical issue here the best way to fix this is to get this out of RBAR (row by agonizing row) logic. This looks like the entire nested cursor structure could be replaced with a single update statement using some aggregation.

– Sean Lange
Nov 14 '18 at 15:18





While the formatting should shed some light on the logical issue here the best way to fix this is to get this out of RBAR (row by agonizing row) logic. This looks like the entire nested cursor structure could be replaced with a single update statement using some aggregation.

– Sean Lange
Nov 14 '18 at 15:18













If you want to update some rows based on a query write an UPDATE ... FROM ...WHERE query

– Panagiotis Kanavos
Nov 14 '18 at 15:18






If you want to update some rows based on a query write an UPDATE ... FROM ...WHERE query

– Panagiotis Kanavos
Nov 14 '18 at 15:18













0






active

oldest

votes











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%2f53303153%2fget-a-while-in-a-while-sql%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















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%2f53303153%2fget-a-while-in-a-while-sql%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

Darth Vader #20

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Ondo