Solved

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

Posted on 2007-12-03
4
1,058 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
4 Comments
 
LVL 8

Accepted Solution

by:
i2mental earned 500 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now