Sql Alter table add column,其中包含从另一列填充的数据

Sql Alter table add column,其中包含从另一列填充的数据,sql,sql-server-2008,Sql,Sql Server 2008,我正在尝试向表中添加一列,该列需要从表中的现有列中获取一个varchar值,并将其转换为if/then格式的int 例如: if size = d then size_int = 1 else if size = f then size_int = 2 else if size = t then size_int = 3 else if size = s then size_int = 4 else if size = m then size_int = 5 else if size = l t

我正在尝试向表中添加一列,该列需要从表中的现有列中获取一个varchar值,并将其转换为if/then格式的int

例如:

if size = d then size_int = 1
else if size = f then size_int = 2
else if size = t then size_int = 3
else if size = s then size_int = 4
else if size = m then size_int = 5
else if size = l then size_int = 6
else if size = h then size_int = 7
else if size = g then size_int = 8
else if size = c then size_int = 9
如果有一种更简单的方法可以做到这一点,那就是先添加列,然后对其进行修改,这样也会起作用。

尝试以下方法:

ALTER TABLE <YOUR_TABLE> ADD size_int INT;
UPDATE <YOUR_TABLE> SET size_int = 
CASE size 
    WHEN 'd' THEN 1
    WHEN 'f' THEN 2
    WHEN 't' THEN 3
    WHEN 's' THEN 4
    WHEN 'm' THEN 5
    WHEN 'l' THEN 6
    WHEN 'h' THEN 7
    WHEN 'g' THEN 8
    WHEN 'c' THEN 9
    ELSE NULL
END
ALTER TABLE ADD size\u int;
更新集大小\u int=
箱子尺寸
“d”是什么时候
当‘f’时,则为2
那么什么时候是3点
那是什么时候
我什么时候5岁
当我6岁的时候
当‘h’时,则为7
当‘g’时,则为8
什么时候是c,然后是9
否则无效
结束

如果这两列要保持这种关系,那么使用这种布局将破坏正常形式(并存储派生数据)。最好是在视图中导出size_int,或者在查找表中存储size-to-size_int映射。哇,不是别人而是编码恐怖分子自己编辑的。派生数据的目的只是对表进行快速更新,然后删除大小列并仅使用size_int继续前进。我认为这是最简单的方法,以便编写select语句返回范围内的大小,即=3,或4到6之间,