使用案例条件插入表格

时间:2018-01-24 09:24:46

标签: sql-server stored-procedures

我有一个表格,我必须检查列'Type'是否包含数据值为'V',然后我必须将其作为'F'插入,否则无需更改

我正在使用以下内容并将错误视为不正确的语法。

INSERT INTO [dbo].[Table]
           ([A] ,[B],
           [C],[D] , CASE [Type] WHEN 'F'THEN 'V' else [Type] as Service type,[E]) select   ([A] ,[B],
           [C],[D] ,[Type],[E]   from   [dbo].[Table2]

2 个答案:

答案 0 :(得分:2)

您的插入语句错误更正,如case表达式

INSERT INTO [dbo].[Table] ([A], [B], [C], [D], [Type], [E]) 
SELECT [A], [B], [C], [D],
       CASE [Type] WHEN 'V'THEN 'F' else [Type] end [Type],[E]
FROM [dbo].[Table2];

答案 1 :(得分:0)

insert [dbo].[Table] (a, type)
    select a, CASE [Type] WHEN 'F'THEN 'V' else [Type]
    from dbo].[Table2]