更新2表的查询

时间:2017-11-03 20:08:07

标签: sql sql-server-2008

我有以下查询

string updateReq = "UPDATE Student SET Student.ID_Request = 
Request.ID_Request FROM Request where Student.St_Code=Request.St_Code   INNER JOIN  Request ON Student.ID_Request = 
Request.ID_Request";

但是当我执行此操作时,我收到此错误:

  

FROM子句中的“Request”和“Request”对象具有相同的公开名称。使用相关名来区分它们。

我该如何解决这个问题?

我试图在临时表中选择记录,但它不起作用或者我做错了

string select = "SELECT * INTO #TempTable FROM Student s where ID_Request IS NULL";

string updateReq = "UPDATE TempTable SET s.ID_Request = Request.ID_Request FROM Request where Student.St_Code=Request.St_Code  INNER JOIN  Request ON Student.ID_Request = Request.ID_Request";

当我执行这些字符串时,我得到相同的错误

2 个答案:

答案 0 :(得分:1)

据推测,您打算这个查询:

UPDATE Student
    SET ID_Request = r.ID_Request
FROM Student s JOIN
     Request r
     ON s.St_Code = r.St_Code ;

答案 1 :(得分:0)

尝试使用使用' AS'

完成的别名

SELECT *
FROM table1 AS first
INNER JOIN table1 AS second ON second.col = first.col;