MySQL updating a field to result of a function

Posted by jdborg on Stack Overflow See other posts from Stack Overflow or by jdborg
Published on 2011-11-24T09:40:43Z Indexed on 2011/11/24 9:52 UTC
Read the original article Hit count: 177

Filed under:
mysql> CREATE FUNCTION test ()
    -> RETURNS CHAR(16)
    -> NOT DETERMINISTIC
    -> BEGIN
    -> RETURN 'IWantThisText';
    -> END$$
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT test();
+------------------+
|    test()        |
+------------------+
| IWantThisText    | 
+------------------+
1 row in set (0.00 sec)

mysql> UPDATE `table`
    -> SET field = test()
    -> WHERE id = 1
Query OK, 1 row affected, 1 warning (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 1

mysql> SHOW WARNINGS;
+---------+------+----------------------------------------------------------------+
| Level   | Code | Message                                                        |
+---------+------+----------------------------------------------------------------+
| Warning | 1265 | Data truncated for column 'test' at row 1                      | 
+---------+------+----------------------------------------------------------------+
1 row in set (0.00 sec)


mysql> SELECT field FROM table WHERE id = 1;
+------------------+
| field            |
+------------------+
| NULL             | 
+------------------+
1 row in set (0.00 sec)

What I am doing wrong? I just want field to be set to the returned value of test() Forgot to mention field is VARCHR(255)

© Stack Overflow or respective owner

Related posts about mysql