سؤال

test.sql:

SET @my_var = 50;

CREATE TABLE test_table (
    id SMALLINT NOT NULL,
    my_text VARCHAR(@my_var),
    PRIMARY KEY (id)
);

Command line interaction:

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

Is there a way to do this?

هل كانت مفيدة؟

المحلول

I can't think of a way other than compiling the whole CREATE TABLE as a string and executing it:

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;

Its pretty ugly, but should work.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top