Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: SQL Server
Expertise: Beginner
Mar 6, 2000

Filling a Field with Data

Question:
How can I fill a numeric field with a sequence of numbers using SQL?

For example, I want a field named NUMBER to contain the numbers 1 for the first row, 3 for the second row, 5 for the third row, etc.

Answer:
There are several ways to approach this. The easiest would be to make the field a serial field type. Then you'd just do an "INSERT into table_name (fieldname) values (0)". The value inserted will be the next one in the sequence.

You could also enable ROWIDS and do some type of manipulation on that value. It depends on what you mean by first row. None of the methods will actually set the physical position of the row in the database, but that's academic. You could do a SELECT/ORDER BY on the numeric field to bring the data back in the order you wish.

DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap