TEST.SQL:
SET @my_var = 50;
CREATE TABLE test_table (
id SMALLINT NOT NULL,
my_text VARCHAR(@my_var),
PRIMARY KEY (id)
);
命令行互动:
mysql> source d:/test.sql;
Query OK, 0 rows affected (0.00 sec)
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near
'@my_var),
PRIMARY KEY (id)
)' at line 3
有办法做到这一点吗?
答案 0 :(得分:2)
我想不出除了将整个CREATE TABLE
编译为字符串并执行它之外的其他方法:
SET @my_var = 50;
PREPARE sql FROM '
CREATE TABLE test_table (
id SMALLINT NOT NULL,
my_text VARCHAR(' + @my_var + '),
PRIMARY KEY (id)
);
'
EXECUTE sql;
DEALLOCATE PREPARE sql;
它非常难看,但应该有用。