SQL Server UPDATE和SET使用SELECT语句

时间:2018-04-25 16:56:50

标签: sql sql-server select sql-update

我需要使用SELECT语句生成的数据填充新列“Better_test”:

SELECT [User Organization], RIGHT([User Organization], LEN([User Organization]) - CHARINDEX(',', [USER ORGANIZATION])) AS Better_test
FROM Logins

下面的代码给出了这个错误: 当未使用EXISTS引入子查询时,只能在选择列表中指定一个表达式。

UPDATE Logins
    SET Better_test = ( SELECT [User Organization], RIGHT([User Organization], LEN([User Organization]) - CHARINDEX(',', [USER ORGANIZATION]))
    FROM Logins

1 个答案:

答案 0 :(得分:1)

您不需要子查询,尤其是返回两个值的子查询。此外,FROM是不必要的:

UPDATE Logins
    SET Better_test = RIGHT([User Organization], LEN([User Organization]) - CHARINDEX(',', [USER ORGANIZATION]);