2011-11-24 3 views
4
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) 

내가 뭘 잘못하고 있니? 난 그냥 원하는 fieldtest() 의 반환 값으로 설정할 수는 field는 (255)함수의 결과로 필드를 갱신하는 MySQL

답변

2

VARCHR (아니 진짜 대답하지만, 너무 오래 코멘트)
당신이 시도하십시오 수 있습니다 언급하는 것을 잊었다이 자체 포함 예?

C:\web\xampp\mysql\bin>mysql -u volker -p 
Enter password: *************** 
Welcome to the MySQL monitor. Commands end with ; or \g. 
Your MySQL connection id is 7 
Server version: 5.5.8 MySQL Community Server (GPL) 

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. 

Oracle is a registered trademark of Oracle Corporation and/or its 
affiliates. Other names may be trademarks of their respective 
owners. 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 

mysql> use test; 
Database changed 
mysql> CREATE TEMPORARY TABLE soFoo (id int auto_increment, field varchar(255), primary key(id)); 
Query OK, 0 rows affected (0.00 sec) 

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

mysql> delimiter ; 
mysql> INSERT INTO soFoo (field) VALUES ('abc'),(NULL); 
Query OK, 2 rows affected (0.00 sec) 
Records: 2 Duplicates: 0 Warnings: 0 

mysql> UPDATE `soFoo` SET field = soTest() WHERE id = 1; 
Query OK, 1 row affected (0.00 sec) 
Rows matched: 1 Changed: 1 Warnings: 0 

mysql> UPDATE `soFoo` SET field = soTest() WHERE id = 2; 
Query OK, 1 row affected (0.00 sec) 
Rows matched: 1 Changed: 1 Warnings: 0 

mysql> SELECT * FROM soFoo; 

+----+---------------+ 
| id | field   | 
+----+---------------+ 
| 1 | IWantThisText | 
| 2 | IWantThisText | 
+----+---------------+ 
2 rows in set (0.00 sec) 

mysql> 

그리고 어떤 서버 버전이 사용합니까 : 내 컴퓨터에서 잘 작동

use test; 
CREATE TEMPORARY TABLE soFoo (id int auto_increment, field varchar(255), primary key(id)); 
delimiter $$ 
CREATE FUNCTION soTest() 
RETURNS CHAR(16) 
NOT DETERMINISTIC 
BEGIN 
    RETURN 'IWantThisText'; 
END$$ 
delimiter ; 
INSERT INTO soFoo (field) VALUES ('abc'),(NULL); 
UPDATE `soFoo` SET field = soTest() WHERE id = 1; 
UPDATE `soFoo` SET field = soTest() WHERE id = 2; 
SELECT * FROM soFoo; 
?

1

잘 모르겠어요 왜하지만 난은 R & D를하고 함수의 다음 코드로 완벽한 결과를 얻을 :

mysql> CREATE FUNCTION test() 
    -> RETURNS CHAR(16) 
    -> NOT DETERMINISTIC 
    -> RETURN 'IWantThisText'; 

당신의 다른 코드가 잘됩니다.

관련 문제