IF-ELSE inside SQL Cursor to change values of recordsWhat is the use of a cursor in SQL Server?MS SQL temporal table update failureHow to flatten and transpose a hybrid table in SQL Server 2016 with JSON arraysSQL Server Temporal Table Creating Duplicate RecordsSQL - UPSERT LOOP through SELECT's resultsetFind matching records based on dynamic columnsCursor based pagination without offset?Cursor Usage in SQL ServerOpening cursor to fetch and update column values of a table based on other column values of same table in oracleIF @@ROWCOUNT = 0, only called once, seems to not be working
Integrability of log of distance function
Is it safe to unplug a blinking USB drive after 'safely' ejecting it?
rule-based deletions from string list
Inquiry answerer
Availability Group Notification when new DB is added
How to ask a man to not take up more than one seat on public transport while avoiding conflict?
All numbers in a 5x5 Minesweeper grid
Why do things cool down?
Twelve Minesweeper mines that make twelve 4s
Audire, with accusative or dative?
Is there a connection between IT and Ghostbusters?
Regular Expressions with `<` and `?` strange matches
What is the expected way to acquire costly material components?
What are the end bytes of *.docx file format
Are lay articles good enough to be the main source of information for PhD research?
Why was Java 8 left out from Debian Buster?
Find all files in directories named foo
US entry with tourist visa but past alcohol abuse
Applications of mathematics in clinical setting
Why does Canada require a minimum rate of climb for ultralights of 300 ft/min?
What was the deeper meaning of Hermione wanting the cloak?
Paradox regarding phase transitions in relativistic systems
Why can't we use uninitialized local variable to access static content of its type?
What is the rail connection between Paris Charles de Gaulle Airport and Gare de Lyon like?
IF-ELSE inside SQL Cursor to change values of records
What is the use of a cursor in SQL Server?MS SQL temporal table update failureHow to flatten and transpose a hybrid table in SQL Server 2016 with JSON arraysSQL Server Temporal Table Creating Duplicate RecordsSQL - UPSERT LOOP through SELECT's resultsetFind matching records based on dynamic columnsCursor based pagination without offset?Cursor Usage in SQL ServerOpening cursor to fetch and update column values of a table based on other column values of same table in oracleIF @@ROWCOUNT = 0, only called once, seems to not be working
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have a SQL Server table that has records with one filed being a number that I need to renumber into a new table, It also has an identity column that would repeat on records when there are multipe. I would like to sort the table on the identity field, then descending on the number field then use a cursor to number the 1st record (1) the next record (2) and so on until the identity field changes, then repeat.
Fields I have are: load_number (identity), stop_type, stop_number
Data would look like so:
load_number -- stop_type -- stop_number
1234 ------------------ 1 ------------------- 1
1234 ------------------ 2 ------------------- 5
1234 ------------------ 2 ------------------- 4
1234 ------------------ 2 ------------------- 3
1234 ------------------ 2 ------------------- 2
1234 ------------------ 2 ------------------- 1
I have other fields that describe each row in more detail, but what I need is for the above to have 5 become 1, 4 become 2, 3 stay 3, 2 become 4 and 1 become 5 (so flip the numbers completely) these numbers could go as high as 10.
DECLARE @lead_key int = 1, @load_number int, @stop_type, @stop_number, @stopnum
CREATE TABLE #renumber
(load_number int, stop_type int, stop_number int)
DECLARE stop_cursor CURSOR FAST_FORWARD FOR
select load_number, stop_type, stop_number
from sometable
order by load_number, stop_type, stop_number desc
OPEN stop_cursor
FETCH NEXT from stop_cursor
INTO @load_number, @stop_type, @stop_number
while @@FETCH_STATUS = 0
BEGIN
If @load_key = 1
BEGIN
SET @load_key = @load_number --initialize @load_key for first record
END
If @load_key = @load_number
BEGIN
If @stop_type = 2
BEGIN
insert into #renumber
select @load_number, @stop_type, @stopnum --use @stopnum counter
SET @stopnum = @stopnum + 1
END
ELSE
BEGIN
insert into #renumber
select @load_number, @stop_type, @stop_number --use @stop_number
SET @stopnum = 1 --reset @stopnum to 1
END
END
ELSE
SET @load_key = @load_number
SET @stopnum = 1
END
FETCH NEXT from stop_cursor
INTO @load_number, @stop_type, @stop_number
CLOSE stop_cursor;
DEALLOCATE stop_cursor;
This query loops and never ends, my ability to debug isn't working on my PC so I can't step through this to see where it's looping so I thought I'd ask here the correct way to get what I am looking for. Thanks for any help!
sql-server-2016 database-cursor
add a comment
|
I have a SQL Server table that has records with one filed being a number that I need to renumber into a new table, It also has an identity column that would repeat on records when there are multipe. I would like to sort the table on the identity field, then descending on the number field then use a cursor to number the 1st record (1) the next record (2) and so on until the identity field changes, then repeat.
Fields I have are: load_number (identity), stop_type, stop_number
Data would look like so:
load_number -- stop_type -- stop_number
1234 ------------------ 1 ------------------- 1
1234 ------------------ 2 ------------------- 5
1234 ------------------ 2 ------------------- 4
1234 ------------------ 2 ------------------- 3
1234 ------------------ 2 ------------------- 2
1234 ------------------ 2 ------------------- 1
I have other fields that describe each row in more detail, but what I need is for the above to have 5 become 1, 4 become 2, 3 stay 3, 2 become 4 and 1 become 5 (so flip the numbers completely) these numbers could go as high as 10.
DECLARE @lead_key int = 1, @load_number int, @stop_type, @stop_number, @stopnum
CREATE TABLE #renumber
(load_number int, stop_type int, stop_number int)
DECLARE stop_cursor CURSOR FAST_FORWARD FOR
select load_number, stop_type, stop_number
from sometable
order by load_number, stop_type, stop_number desc
OPEN stop_cursor
FETCH NEXT from stop_cursor
INTO @load_number, @stop_type, @stop_number
while @@FETCH_STATUS = 0
BEGIN
If @load_key = 1
BEGIN
SET @load_key = @load_number --initialize @load_key for first record
END
If @load_key = @load_number
BEGIN
If @stop_type = 2
BEGIN
insert into #renumber
select @load_number, @stop_type, @stopnum --use @stopnum counter
SET @stopnum = @stopnum + 1
END
ELSE
BEGIN
insert into #renumber
select @load_number, @stop_type, @stop_number --use @stop_number
SET @stopnum = 1 --reset @stopnum to 1
END
END
ELSE
SET @load_key = @load_number
SET @stopnum = 1
END
FETCH NEXT from stop_cursor
INTO @load_number, @stop_type, @stop_number
CLOSE stop_cursor;
DEALLOCATE stop_cursor;
This query loops and never ends, my ability to debug isn't working on my PC so I can't step through this to see where it's looping so I thought I'd ask here the correct way to get what I am looking for. Thanks for any help!
sql-server-2016 database-cursor
add a comment
|
I have a SQL Server table that has records with one filed being a number that I need to renumber into a new table, It also has an identity column that would repeat on records when there are multipe. I would like to sort the table on the identity field, then descending on the number field then use a cursor to number the 1st record (1) the next record (2) and so on until the identity field changes, then repeat.
Fields I have are: load_number (identity), stop_type, stop_number
Data would look like so:
load_number -- stop_type -- stop_number
1234 ------------------ 1 ------------------- 1
1234 ------------------ 2 ------------------- 5
1234 ------------------ 2 ------------------- 4
1234 ------------------ 2 ------------------- 3
1234 ------------------ 2 ------------------- 2
1234 ------------------ 2 ------------------- 1
I have other fields that describe each row in more detail, but what I need is for the above to have 5 become 1, 4 become 2, 3 stay 3, 2 become 4 and 1 become 5 (so flip the numbers completely) these numbers could go as high as 10.
DECLARE @lead_key int = 1, @load_number int, @stop_type, @stop_number, @stopnum
CREATE TABLE #renumber
(load_number int, stop_type int, stop_number int)
DECLARE stop_cursor CURSOR FAST_FORWARD FOR
select load_number, stop_type, stop_number
from sometable
order by load_number, stop_type, stop_number desc
OPEN stop_cursor
FETCH NEXT from stop_cursor
INTO @load_number, @stop_type, @stop_number
while @@FETCH_STATUS = 0
BEGIN
If @load_key = 1
BEGIN
SET @load_key = @load_number --initialize @load_key for first record
END
If @load_key = @load_number
BEGIN
If @stop_type = 2
BEGIN
insert into #renumber
select @load_number, @stop_type, @stopnum --use @stopnum counter
SET @stopnum = @stopnum + 1
END
ELSE
BEGIN
insert into #renumber
select @load_number, @stop_type, @stop_number --use @stop_number
SET @stopnum = 1 --reset @stopnum to 1
END
END
ELSE
SET @load_key = @load_number
SET @stopnum = 1
END
FETCH NEXT from stop_cursor
INTO @load_number, @stop_type, @stop_number
CLOSE stop_cursor;
DEALLOCATE stop_cursor;
This query loops and never ends, my ability to debug isn't working on my PC so I can't step through this to see where it's looping so I thought I'd ask here the correct way to get what I am looking for. Thanks for any help!
sql-server-2016 database-cursor
I have a SQL Server table that has records with one filed being a number that I need to renumber into a new table, It also has an identity column that would repeat on records when there are multipe. I would like to sort the table on the identity field, then descending on the number field then use a cursor to number the 1st record (1) the next record (2) and so on until the identity field changes, then repeat.
Fields I have are: load_number (identity), stop_type, stop_number
Data would look like so:
load_number -- stop_type -- stop_number
1234 ------------------ 1 ------------------- 1
1234 ------------------ 2 ------------------- 5
1234 ------------------ 2 ------------------- 4
1234 ------------------ 2 ------------------- 3
1234 ------------------ 2 ------------------- 2
1234 ------------------ 2 ------------------- 1
I have other fields that describe each row in more detail, but what I need is for the above to have 5 become 1, 4 become 2, 3 stay 3, 2 become 4 and 1 become 5 (so flip the numbers completely) these numbers could go as high as 10.
DECLARE @lead_key int = 1, @load_number int, @stop_type, @stop_number, @stopnum
CREATE TABLE #renumber
(load_number int, stop_type int, stop_number int)
DECLARE stop_cursor CURSOR FAST_FORWARD FOR
select load_number, stop_type, stop_number
from sometable
order by load_number, stop_type, stop_number desc
OPEN stop_cursor
FETCH NEXT from stop_cursor
INTO @load_number, @stop_type, @stop_number
while @@FETCH_STATUS = 0
BEGIN
If @load_key = 1
BEGIN
SET @load_key = @load_number --initialize @load_key for first record
END
If @load_key = @load_number
BEGIN
If @stop_type = 2
BEGIN
insert into #renumber
select @load_number, @stop_type, @stopnum --use @stopnum counter
SET @stopnum = @stopnum + 1
END
ELSE
BEGIN
insert into #renumber
select @load_number, @stop_type, @stop_number --use @stop_number
SET @stopnum = 1 --reset @stopnum to 1
END
END
ELSE
SET @load_key = @load_number
SET @stopnum = 1
END
FETCH NEXT from stop_cursor
INTO @load_number, @stop_type, @stop_number
CLOSE stop_cursor;
DEALLOCATE stop_cursor;
This query loops and never ends, my ability to debug isn't working on my PC so I can't step through this to see where it's looping so I thought I'd ask here the correct way to get what I am looking for. Thanks for any help!
sql-server-2016 database-cursor
sql-server-2016 database-cursor
edited Mar 28 at 14:22
Michael Giusto
asked Mar 28 at 14:11
Michael GiustoMichael Giusto
104 bronze badges
104 bronze badges
add a comment
|
add a comment
|
1 Answer
1
active
oldest
votes
I do not quite understand the question, I just put my idea that you can create a temporary table with an identity column, then do update on that table with a static cursor.
Step 1:
CREATE TABLE #renumber
(ID int identity(1,1), load_number int, stop_type int, stop_number int)
Step 2: Insert table with the Identity created automatically
insert into #renumber(load_number, stop_type, stop_number)
select load_number, stop_type, stop_number
from sometable
order by load_number, stop_type, stop_number desc
Step 3:
DECLARE @id int, @load_number int, @stop_type, @stop_number, @stopnum int
--Static cursor
DECLARE stop_cursor CURSOR STATIC FOR
select id, load_number, stop_type, stop_number
from #renumber
order by id
OPEN stop_cursor
FETCH NEXT from stop_cursor INTO @id, @load_number, @stop_type, @stop_number
while @@FETCH_STATUS = 0
BEGIN
If @load_key = 1
BEGIN
SET @load_key = @load_number --initialize @load_key for first record
END
If @load_key = @load_number
BEGIN
If @stop_type = 2
BEGIN
--Update the value you'd like base on ID
update #renumber set stopnum = @stopnum where id = @id
SET @stopnum = @stopnum + 1
END
ELSE
BEGIN
--Update the value you'd like base on ID
update #renumber set stopnum = @stopnum where id = @id
SET @stopnum = 1 --reset @stopnum to 1
END
END
ELSE
SET @load_key = @load_number
SET @stopnum = 1
END
FETCH NEXT from stop_cursor INTO @id, @load_number, @stop_type, @stop_number
CLOSE stop_cursor;
DEALLOCATE stop_cursor;
add a comment
|
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/4.0/"u003ecc by-sa 4.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%2fstackoverflow.com%2fquestions%2f55399692%2fif-else-inside-sql-cursor-to-change-values-of-records%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I do not quite understand the question, I just put my idea that you can create a temporary table with an identity column, then do update on that table with a static cursor.
Step 1:
CREATE TABLE #renumber
(ID int identity(1,1), load_number int, stop_type int, stop_number int)
Step 2: Insert table with the Identity created automatically
insert into #renumber(load_number, stop_type, stop_number)
select load_number, stop_type, stop_number
from sometable
order by load_number, stop_type, stop_number desc
Step 3:
DECLARE @id int, @load_number int, @stop_type, @stop_number, @stopnum int
--Static cursor
DECLARE stop_cursor CURSOR STATIC FOR
select id, load_number, stop_type, stop_number
from #renumber
order by id
OPEN stop_cursor
FETCH NEXT from stop_cursor INTO @id, @load_number, @stop_type, @stop_number
while @@FETCH_STATUS = 0
BEGIN
If @load_key = 1
BEGIN
SET @load_key = @load_number --initialize @load_key for first record
END
If @load_key = @load_number
BEGIN
If @stop_type = 2
BEGIN
--Update the value you'd like base on ID
update #renumber set stopnum = @stopnum where id = @id
SET @stopnum = @stopnum + 1
END
ELSE
BEGIN
--Update the value you'd like base on ID
update #renumber set stopnum = @stopnum where id = @id
SET @stopnum = 1 --reset @stopnum to 1
END
END
ELSE
SET @load_key = @load_number
SET @stopnum = 1
END
FETCH NEXT from stop_cursor INTO @id, @load_number, @stop_type, @stop_number
CLOSE stop_cursor;
DEALLOCATE stop_cursor;
add a comment
|
I do not quite understand the question, I just put my idea that you can create a temporary table with an identity column, then do update on that table with a static cursor.
Step 1:
CREATE TABLE #renumber
(ID int identity(1,1), load_number int, stop_type int, stop_number int)
Step 2: Insert table with the Identity created automatically
insert into #renumber(load_number, stop_type, stop_number)
select load_number, stop_type, stop_number
from sometable
order by load_number, stop_type, stop_number desc
Step 3:
DECLARE @id int, @load_number int, @stop_type, @stop_number, @stopnum int
--Static cursor
DECLARE stop_cursor CURSOR STATIC FOR
select id, load_number, stop_type, stop_number
from #renumber
order by id
OPEN stop_cursor
FETCH NEXT from stop_cursor INTO @id, @load_number, @stop_type, @stop_number
while @@FETCH_STATUS = 0
BEGIN
If @load_key = 1
BEGIN
SET @load_key = @load_number --initialize @load_key for first record
END
If @load_key = @load_number
BEGIN
If @stop_type = 2
BEGIN
--Update the value you'd like base on ID
update #renumber set stopnum = @stopnum where id = @id
SET @stopnum = @stopnum + 1
END
ELSE
BEGIN
--Update the value you'd like base on ID
update #renumber set stopnum = @stopnum where id = @id
SET @stopnum = 1 --reset @stopnum to 1
END
END
ELSE
SET @load_key = @load_number
SET @stopnum = 1
END
FETCH NEXT from stop_cursor INTO @id, @load_number, @stop_type, @stop_number
CLOSE stop_cursor;
DEALLOCATE stop_cursor;
add a comment
|
I do not quite understand the question, I just put my idea that you can create a temporary table with an identity column, then do update on that table with a static cursor.
Step 1:
CREATE TABLE #renumber
(ID int identity(1,1), load_number int, stop_type int, stop_number int)
Step 2: Insert table with the Identity created automatically
insert into #renumber(load_number, stop_type, stop_number)
select load_number, stop_type, stop_number
from sometable
order by load_number, stop_type, stop_number desc
Step 3:
DECLARE @id int, @load_number int, @stop_type, @stop_number, @stopnum int
--Static cursor
DECLARE stop_cursor CURSOR STATIC FOR
select id, load_number, stop_type, stop_number
from #renumber
order by id
OPEN stop_cursor
FETCH NEXT from stop_cursor INTO @id, @load_number, @stop_type, @stop_number
while @@FETCH_STATUS = 0
BEGIN
If @load_key = 1
BEGIN
SET @load_key = @load_number --initialize @load_key for first record
END
If @load_key = @load_number
BEGIN
If @stop_type = 2
BEGIN
--Update the value you'd like base on ID
update #renumber set stopnum = @stopnum where id = @id
SET @stopnum = @stopnum + 1
END
ELSE
BEGIN
--Update the value you'd like base on ID
update #renumber set stopnum = @stopnum where id = @id
SET @stopnum = 1 --reset @stopnum to 1
END
END
ELSE
SET @load_key = @load_number
SET @stopnum = 1
END
FETCH NEXT from stop_cursor INTO @id, @load_number, @stop_type, @stop_number
CLOSE stop_cursor;
DEALLOCATE stop_cursor;
I do not quite understand the question, I just put my idea that you can create a temporary table with an identity column, then do update on that table with a static cursor.
Step 1:
CREATE TABLE #renumber
(ID int identity(1,1), load_number int, stop_type int, stop_number int)
Step 2: Insert table with the Identity created automatically
insert into #renumber(load_number, stop_type, stop_number)
select load_number, stop_type, stop_number
from sometable
order by load_number, stop_type, stop_number desc
Step 3:
DECLARE @id int, @load_number int, @stop_type, @stop_number, @stopnum int
--Static cursor
DECLARE stop_cursor CURSOR STATIC FOR
select id, load_number, stop_type, stop_number
from #renumber
order by id
OPEN stop_cursor
FETCH NEXT from stop_cursor INTO @id, @load_number, @stop_type, @stop_number
while @@FETCH_STATUS = 0
BEGIN
If @load_key = 1
BEGIN
SET @load_key = @load_number --initialize @load_key for first record
END
If @load_key = @load_number
BEGIN
If @stop_type = 2
BEGIN
--Update the value you'd like base on ID
update #renumber set stopnum = @stopnum where id = @id
SET @stopnum = @stopnum + 1
END
ELSE
BEGIN
--Update the value you'd like base on ID
update #renumber set stopnum = @stopnum where id = @id
SET @stopnum = 1 --reset @stopnum to 1
END
END
ELSE
SET @load_key = @load_number
SET @stopnum = 1
END
FETCH NEXT from stop_cursor INTO @id, @load_number, @stop_type, @stop_number
CLOSE stop_cursor;
DEALLOCATE stop_cursor;
answered Mar 28 at 14:52
Vinh Can CodeVinh Can Code
3062 silver badges11 bronze badges
3062 silver badges11 bronze badges
add a comment
|
add a comment
|
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
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.
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%2fstackoverflow.com%2fquestions%2f55399692%2fif-else-inside-sql-cursor-to-change-values-of-records%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