Solved

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

Posted on 2007-12-03
4
1,055 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:ScottPletcher
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Creating and Managing Databases with phpMyAdmin in cPanel.
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

760 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

17 Experts available now in Live!

Get 1:1 Help Now