TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
 Specialized Dev Zones Research Center eBook Library .NET Java C++ Web Dev Architecture Database Security Open Source Enterprise Mobile Special Reports 10-Minute Solutions DevXtra Blogs Slideshow

 Home » Tip Bank » Database Development » SQL » Transact-sql
Language: SQL Server
Expertise: Beginner
Mar 25, 1997

### WEBINAR:On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning

# Automatically Assigning Values

Question:
[Joe Celko's Hotel Rooms Puzzle]

Ron Hiner put this question on the Watcom forum on CompuServe. He had a data conversion project where he needed to automatically assign some values to use as part of the PRIMARY KEY to a table of hotel rooms.

The floor part of the PRIMARY KEY is the FOREIGN KEY to another table of floors within the building. The part of the hotel room key we need to create is the room number, which has to be a sequential number, starting at x01 for each floor x. The hotel is small enough that we know we will only have three digit numbers. The table is defined as follows:

```    CREATE TABLE Hotel
(floor SMALLINT NOT NULL,
room SMALLINT NOT NULL,
PRIMARY KEY (floor, room),
FOREIGN KEY floor REFERENCES Bldg(floor);
```
Currently, the data in the table looks like this:
```    floor  room
===========
1  NULL
1  NULL
1  NULL
2  NULL
2  NULL
3  NULL
```
Watcom (and other versions of SQL) has a NUMBER(*) function which begins at 1 and returns an incremented value for each row which calls it.

Is there an easy way via the NUMBER(*) function (or some other means) to automatically populate the room column? Mr. Hiner was thinking of some how using a GROUP BY floor number clause to restart the numbering back at 1.

The Watcom support people came up with this approach. First make one updating pass thru the whole database, to fill in the room numbers. This trick will not work unless you can guarantee that the Hotel table is updated in sorted order. As it happens, Watcom can guarantee just that with a clause on the UPDATE statement, thus:

```    UPDATE Hotel
SET room =(floor*100)+NUMBER(*)
ORDER BY floor;
```
Which would give the results:
```    floor room
==========
1   101
1   102
1   103
2   204
2   205
3   306
```
Followed by:
```    UPDATE Hotel
SET room =(room - 3)
WHERE floor =2;

UPDATE Hotel
SET room =(room - 5)
WHERE floor =3;
```
Which would give the correct results:
```    floor room
==========
1   101
1   102
1   103
2   201
2   202
3   301
```
Can you top this — without using the ORDER BY clause?

I would use SQL to write SQL statements. This is a neat trick which is not used enough. Just watch your quotation marks when you do it and remember to convert numerics to characters, thus:

```    SELECT DISTINCT
'UPDATE Hotel SET room =('
|| CAST (floor AS CHAR(1))
|| '* 100)+NUMBER(*) WHERE floor ='
|| CAST (floor AS CHAR(1))
|| ';'
```
This statement will write a result table with one column that has test like this:
```    UPDATE Hotel SET room =(1*100)+NUMBER(*) WHERE floor =1;
UPDATE Hotel SET room =(2*100)+NUMBER(*) WHERE floor =2;
UPDATE Hotel SET room =(3*100)+NUMBER(*) WHERE floor =3;
...
```
This does not depend on the order of the rows in the table.

Puzzle provided courtesy of:
Joe Celko
71062.1056@compuserve.com

DevX Pro

 Submit a Tip Browse "Database Development" Tips Browse All Tips
Comment and Contribute

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

Thanks for your registration, follow us on our social networks to keep up-to-date