简体   繁体   中英

How do to Update Child Table using SQL Query

I would like help in updating 2 tables with the click of a button. I will be updating a Table called WorkSchedule and another table called WorkShiftBid. What I want to achieve is, updating the WorkSchedule table and set the column called "WorkScheduleStatus" to "Pending" (Which I had already done) while having another SQL Query to update another table called WorkShiftBid.

WorkShiftBid Table For Reference

With the reference, I am able to generate new WorkShiftBidID, Capture WSBidDateTime (System Time) and the WSBidStatus. I am stuck at trying to get the WorkScheduleID from the WorkSchedule table, How do I use the SQLQuery to table join and get the WorkScheduleID?

 public async Task<IActionResult> DisplaySchedule(Guid? id)
    {
        SqlConnection con = new SqlConnection();
        SqlCommand com = new SqlCommand();
        SqlDataReader dr;


        
       connectionString();
        con.Open();
        com.Connection = con;
        com.CommandText = @"update WorkSchedule set WorkScheduleStatus ='Test' where WorkScheduleID = @id;
        
        insert into WorkShiftBid (WorkShiftBidID,WSBidDateTime,WSBidStatus) values (NewID(),GetDate(),'Pending')";


        com.Parameters.Add("@id", SqlDbType.UniqueIdentifier).Value = id;
        dr = com.ExecuteReader();

        
     

        if (dr.Read())
        {
            con.Close();
            return RedirectToAction(nameof(DisplaySchedule));
        }
        else
        {
            con.Close();
            return RedirectToAction(nameof(DisplaySchedule));
        }

        

        void connectionString()
        {
            con.ConnectionString = "";

        }
    }
   

You need to add a second output parameter like this:

com.CommandText = @"update WorkSchedule set WorkScheduleStatus ='Test' where WorkScheduleID = @id;
        
insert into WorkShiftBid (WorkShiftBidID,WSBidDateTime,WSBidStatus) values (NewID(),GetDate(),'Pending');
set @newId = scope_identity();";

com.Parameters.Add("@id", SqlDbType.UniqueIdentifier).Value = id;
var p = com.Parameters.Add("@newId", SqlDbType.Int32);
p.Direction = ParameterDirection.Output
dr = com.ExecuteReader();


var newId = (int)dr.Parameters["@newId"].value;

This code is just from the top-of-my-head, so it might require some editing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM