Search code examples
c#sqldatabaseinner-join

c# database sql inner join


I want to inner join my ActivityClub table with Clubs table so it can be updated into 2 tables. I'm using method class and SQL database.

System.Data.SqlClient.SqlException: 'Incorrect syntax near the keyword 'INNER'.'

 public int updateactivity(string clubname, string clubdesc, string activity, string achv)
    {
        int status = 0;
        string updateSQL = "UPDATE ActivityClub SET club_desc=@desc, activity=@act, achievement=@acv INNER JOIN Clubs AS C1 ON C1.club_desc = Activity.club_desc WHERE club_name=@club";
        Connect();
        SqlCommand cmd = new SqlCommand(updateSQL, conn);
        
        cmd.Parameters.AddWithValue("@club", clubname);
        cmd.Parameters.AddWithValue("@desc", clubdesc);
        cmd.Parameters.AddWithValue("@act", activity);
        cmd.Parameters.AddWithValue("@acv", achv);
        status = cmd.ExecuteNonQuery();
        return status;
    }

Solution

  • You are using the incorrect syntax. It should be like this. I would suggest you write a stored procedure and use query in the SP and call it from C# code.

    UPDATE 
        t1
    SET 
        t1.c1 = t2.c2,
        t1.c2 = expression,
        ...   
    FROM 
        t1
        [INNER | LEFT] JOIN t2 ON join_predicate
    WHERE 
        where_predicate;
    

    Reference: https://www.sqlservertutorial.net/sql-server-basics/sql-server-update-join/