Update query for 2 table

  • Hi All
    i have this Query Below
    string updateReq = "UPDATE Student SET Student.ID_Request = Request.ID_Request FROM Request INNER JOIN Request ON Student.ID_Request = Request.ID_Request";

    but when i excute this i get this error : 
    The objects "Request" and "Request" in the FROM clause have the same exposed names. Use correlation names to distinguish them.

    how can i solve this ?

    What I have tried:

    i tried to select the records in the temp table but it doesn't work or maybe i do it wrong
    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 INNER JOIN Request ON Student.ID_Request = Request.ID_Request";

    when i excute these string i get the same error

  • aminhitman0212 - Friday, November 3, 2017 2:06 PM

    Hi All
    i have this Query Below
    string updateReq = "UPDATE Student SET Student.ID_Request = Request.ID_Request FROM Request INNER JOIN Request ON Student.ID_Request = Request.ID_Request";

    but when i excute this i get this error : 
    The objects "Request" and "Request" in the FROM clause have the same exposed names. Use correlation names to distinguish them.

    how can i solve this ?

    What I have tried:

    i tried to select the records in the temp table but it doesn't work or maybe i do it wrong
    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 INNER JOIN Request ON Student.ID_Request = Request.ID_Request";

    when i excute these string i get the same error

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

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Phil Parkin - Friday, November 3, 2017 2:21 PM

    aminhitman0212 - Friday, November 3, 2017 2:06 PM

    Hi All
    i have this Query Below
    string updateReq = "UPDATE Student SET Student.ID_Request = Request.ID_Request FROM Request INNER JOIN Request ON Student.ID_Request = Request.ID_Request";

    but when i excute this i get this error : 
    The objects "Request" and "Request" in the FROM clause have the same exposed names. Use correlation names to distinguish them.

    how can i solve this ?

    What I have tried:

    i tried to select the records in the temp table but it doesn't work or maybe i do it wrong
    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 INNER JOIN Request ON Student.ID_Request = Request.ID_Request";

    when i excute these string i get the same error

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

    thanks a lot for your answer
    but now i found i need to update that record has the same St_Code in 2 table (Request,Student)
    i tried this
    UPDATE s SET ID_Request = r.ID_Request where St_Code=@code FROM Student s JOIN Request r ON s.ID_Request = r.ID_Request
     cmd3.Parameters.Add("@code", SqlDbType.NVarChar).Value = tbStcode.Text; 
    and i get the error
    how put this condition on this update statement ?

  • Qualify the column name.

    UPDATE s
    SET ID_Request = r.ID_Request
    FROM Student s
    INNER JOIN Request r ON s.ID_Request = r.ID_Request
    WHERE s.St_Code=@code

    And WHERE clause goes after the join.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Should it look like this?

    UPDATE S
    SET ID_Request = R.ID_Request
    FROM Student S
    INNER JOIN Request R ON S.ID_Student = R.ID_Student
    where S.ID_Request is null

    _____________
    Code for TallyGenerator

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply