[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2007-12-03
4
Medium Priority
?
1,088 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 70

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

650 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