将两列的concat插入到一个原始列中

时间:2016-05-02 14:16:44

标签: sql sql-server

使用SQL-Server。

我有一张表AVQ。其中两列名为问题和说明。我想连接这两列并将结果存回问题。

我已经获得了concat查询SELECT question + ISNULL(' ' + instructions, '') from AVQ;

但我不确定如何将结果返回问题列。

我尝试将该查询用作子查询,如下所示:

update AVQ
set Question = (SELECT question + ISNULL(' ' + instructions, '') from AVQ)
where AVID = 2;

但我收到错误Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

有人能指出我正确的方向吗?

3 个答案:

答案 0 :(得分:3)

此处不需要子查询。

update AVQ
set Question = question + ISNULL(' ' + instructions, '')
where AVID = 2;

答案 1 :(得分:3)

这是一种实际上可能更有效的方法:

update AVG
    set Question = Question + ' ' + instructions
    where AVID = 2 and instructions is not null;

如果instructions为空,为什么还要尝试更新?

答案 2 :(得分:0)

你可以使用case语句一次更新所有这些...或者一次添加一个地方...

Update AVQ
set question = case when instructions is null then question 
                    else Question + ' ' + instructions end
where AVID =2 

如果指令为空,我不想在末尾留下额外的'',所以我使用了一个案例陈述。