SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to update a variable with row numbers


How to update a variable with row numbers

Author
Message
Edvard Korsbæk
Edvard Korsbæk
SSC-Addicted
SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)

Group: General Forum Members
Points: 453 Visits: 367
Hi All:

I have this pretty simple table:

CREATE TABLE [dbo].[Agreement_Area](
[Agreement_Area_Id] [int] IDENTITY(1,1) NOT NULL,
[Agreement_Area_Name] [varchar](50) NULL,
[Agreement_Area_Number] [int] NULL,
CONSTRAINT [PK_Agreement_Area] PRIMARY KEY CLUSTERED
(
[Agreement_Area_Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

Which at the time beeing has the values:

Select * from dbo.Agreement_Area

Agreement_Area_Id Agreement_Area_Name Agreement_Area_Number
1 Regelsæt 1 NULL
2 Regelsæt 2 NULL
3 Regelsæt 3 NULL
4 Regelsæt 4 NULL
5 Regelsæt 5 NULL
6 Regelsæt 6 NULL
7 Regelsæt 7 NULL
1009 Regelsæt 8 NULL

What i want is to update the NULL's with row numbers, i.we , iD 1 = 1 to ID 1009 = 8

I have to update +100 databases with the field Agreement_Area_Number, so just to do it manually is not an option.

Best regards

Edvard Korsbæk
Koen Verbeeck
Koen Verbeeck
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27357 Visits: 13268
The UPDATE statement itself is pretty simple using the ROW_NUMBER function.
Maybe you can create some dynamic SQL: loop over the different tables, insert the table name in the update statement and execute the statement.


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
Edvard Korsbæk
Edvard Korsbæk
SSC-Addicted
SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)

Group: General Forum Members
Points: 453 Visits: 367
The function ROW_NUMBER was new to me. Thanks.

SELECT ROW_NUMBER() OVER(ORDER BY Agreement_Area_Id DESC) AS Row from dbo.Agreement_Area

Gives me the row numbers.

But to set Agreement_Area_Number = ROW_NUMBER() OVER(ORDER BY Agreement_Area_Id DESC) in an update statement is not legal.

So still - How to?

Best regards

Edvard Korsbæk
Koen Verbeeck
Koen Verbeeck
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27357 Visits: 13268
This should do the trick.


WITH CTE_rownumbers AS
(
SELECT Agreement_Area_Id, RID = ROW_NUMBER() OVER(ORDER BY Agreement_Area_Id DESC)
FROM [dbo].[Agreement_Area]
)

UPDATE aa
SET Agreement_Area_Number = cte.RID
FROM [dbo].[Agreement_Area] aa
JOIN CTE_rownumbers cte ON aa.Agreement_Area_Id = cte.Agreement_Area_Id;




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
Edvard Korsbæk
Edvard Korsbæk
SSC-Addicted
SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)SSC-Addicted (453 reputation)

Group: General Forum Members
Points: 453 Visits: 367
Yep, looks nice!

Thanks!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search