匹配Sql表中两列的值并将其保存到另一列

时间:2015-02-06 17:59:17

标签: sql sql-server

我正在制作一个工具,从表格列中选择UCPost值,并将其与同一表格的UCPre相匹配,然后将其插入到其他表格中,并附上所有其他细节。

表:

  **UCID   UCName     Author        UCPre                          UCPost**
    UC01   Login      Bilal Haider  User must be Registerd         User is Loggined sucessfully
    UC02   Add User   Bilal Haider  User is Loggined sucessfully   User is added
    UC03   Add file   Bilal Haider  User is Loggined sucessfully   File added
    UC04   Delete file Bilal Haider File is selected              File is deleted

从这张桌子我选择 UCID UCPre UCPost 并插入另一个表名称测试

**UCId  UCPre                           UCPost                          UCNext**
UC01    User must be Registerd          User is Loggined sucessfully    NULL
UC02    User is Loggined sucessfully    User is added                   NULL
UC03    File must be selected           File is added                   NULL
UC04    File is selected                File is deleted                 NULL
UC05    User is Loggined sucessfully    User is deleted                 NULL

我想用该用例的UCId填充测试表的UCNext列,其中UCPre与所选用例的UCPost匹配。

与UC01一样,UCPost用户在执行查询时成功登录,它与UC01的UCPost匹配所有用例的UCPre,UC02和UC03具有相同的UCPre,因此UCNext必须填充UC02,UC03但是它给出了错误。

问题: 当我尝试执行以下声明时

更新测试集UCNext =(从UCDetails中选择UCId,其中test.UCPost = UCPre)

它会出现以下错误

Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.

请告诉我如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

来自测试表的

test.UCPost =User is Loggined sucessfullyUCDetails中有多行,这就是您遇到该错误的原因。试试这个

UPDATE A
SET    a.UCNext = b.UCId
FROM   test a
       JOIN UCDetails b
         ON A.UCPost = b.UCPre