tags:

views:

80

answers:

2

Say I have this two-column table

 id  | column_test

 1   | NULL

 2   | NULL

 3   | NULL

...

as you see, the column_test column now all have value 'NULL'. Now I want to do an update to this table and the final status should look like :

 id  | column_test

 1   | a

 2   | b

 3   | c

...

I know little about compound SQL statements, it looks the query to be used is a small SQL program which is probably going to have local variables involved. Unfortunately I don't know the exact syntax yet(I've tried to build the query myself in the last 20-30 minutes), maybe I can find some SQL gurus here to help me out.-)

[edit:let's stick to mysql.]

A: 

In MySQL:

CREATE TABLE mytable (id INT NOT NULL, column_test CHAR(1));

INSERT
INTO    mytable (id)
VALUES
(1),
(2),
(3),
(4);

SET @r := 0;
UPDATE  mytable
SET     column_test = CHAR(96 + (@r := @r + 1))
WHERE   column_test IS NULL
ORDER BY
        id;

SELECT  *
FROM    mytable;

In SQL Server:

WITH    q AS
        (
        SELECT  *, ROW_NUMBER() OVER (ORDER BY id) AS rn
        FROM    mytable
        WHERE   column_test IS NULL
        )
UPDATE  q
SET     column_test = CHAR(96 + rn)
Quassnoi
Why not use the id column that is already in the table?
IronGoofy
Because the id's can be not consecutive.
Quassnoi
Tried MySQL query, it sets the column values to empty(not null).
Shawn
See the working script in the post update.
Quassnoi
Incorrect string value : '\C7' for 'column_test' at row 1Incorrect string value : '\C8' for 'column_test' at row 2Incorrect string value : '\C9' for 'column_test' at row 3Incorrect string value : '\CA' for 'column_test' at row 4sorry but is this related to my charset which is UTF-8.
Shawn
`C7` is `199` which is far beyond the alphabet range `(97 - 122)`. This means you have at least `104` items. Seems pretending that there are mo more than `25` records helps no more and you need to define what to do after the `Z`.
Quassnoi
Or maybe your forgot to reset `@r`. Did you issue `SET @r := 0` before issuing the query?
Quassnoi
nope I got only 4 rows.
Shawn
`@Shawn`: could you please post the results of `SHOW CREATE TABLE mytable`?
Quassnoi
'mytable', 'CREATE TABLE `mytable` ( `id` int(11) NOT NULL, `column_test` char(1) default NULL) ENGINE=MyISAM DEFAULT CHARSET=utf8'
Shawn
Which version of `MySQL` are you using?
Quassnoi
@Quassnoi: mysql Ver 14.12 Distrib 5.0.45, for win32<ia32>
Shawn
Just checked it on `5.0`, works fine, the connection charset and the database charset are both set to `UTF-8`. Did you copy the script word-to-word and paste it?
Quassnoi
yes. Thanks though I'll try it over again. Thanks for your patience. Will update later.
Shawn
+1  A: 

In general, you can set the column to a function-result of some other columns and therefore set different rows to different values. One example:

UPDATE mytable
SET column_test = cola+colb/colc

Closer to your question, this should work

UPDATE mytable
SET column_test = Char (ASCII('a') + id-1)

Of course this would only work for a limited number of rows in your table.

IronGoofy