?
Solved

Adding a sequence (range) of numbers to a existing table

Posted on 2007-12-03
4
Medium Priority
?
1,081 Views
Last Modified: 2012-06-27
Hi there,

I need to add a sequence of numbers using SQL into a existing table. I need help with making the sql query (not a stored procedure) which can do this.

I have a range of numbers (basically i am adding on to an existing range of numbers in the table) and I need to add the range say between 1000 and 2000 (in to column rangedvalue) in a table with the following columns:

primkey (pk)    |      rangedvalue   |   always0 |    isAlwaysNull
1                              1000                     0               NULL
2                              1001                     0               NULL
3                              1002                     0               NULL
4                              1003                     0               NULL  
.
.
n                              nseq                     0               NULL

You can use my column names for the query.

Many thanks in advance for your support.                  
0
Comment
Question by:ihatelag
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 8

Accepted Solution

by:
i2mental earned 2000 total points
ID: 20398352
declare @count int
declare @pkcount int

set @pkcount = 1
set @count = 1000

while @count <= 2000
begin
insert into table (primkey, rangedvalue, always0, isAlwaysNull)
values (@pkcount, @count, 0, null)
set @count = @count + 1
set @pkcount = @pkcount + 1
end
0
 
LVL 22

Expert Comment

by:dportas
ID: 20398361
WITH t AS
 (SELECT ROW_NUMBER() OVER (ORDER BY primkey)+999 r,
  rangedvalue
  FROM tbl)
UPDATE t SET rangedvalue = r;
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 20398539
UPDATE table
SET rangevalue = primkey + 999
0
 

Author Closing Comment

by:ihatelag
ID: 31412427
PERFECT! :D Thanks for this, most appreciated! You just saved my life :P
0

Featured Post

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question