在Create Table语句中使用用户定义的变量来指定varchar长度

时间:2019-03-24 19:44:28

标签: mysql sql

我希望能够做这样的事情:

SET @foo_width = 10;

CREATE TABLE test_table (
  foo varchar(@foo_width)
);

确切的问题是,对于不同表中的不同列,我将需要相同的varchar-width。因此,认为最好将其存储在变量中。

还有其他选择吗?

1 个答案:

答案 0 :(得分:2)

您不能直接做到这一点,而且MySQL不支持用户定义的类型。

充其量,您可以像这样使用 PREPARED STATEMENT 或在应用程序中生成查询。

PropertyInfo prop = cache.GetType().GetProperty("EntriesCollection", BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.NonPublic | BindingFlags.Public);
object innerCache = prop.GetValue(cache);
MethodInfo clearMethod = innerCache.GetType().GetMethod("Clear", BindingFlags.Instance | BindingFlags.Public);
clearMethod.Invoke(innerCache, null);