How to write update query for a data set in ms sql?

Greetings, you write a stored procedure that upon confirmation of the application adds or updates the data depending on their existence. In this function you are adding team members from fields in another table . Adding works , but with the update there are problems . How do you implement multiple update . I tried using case fails.

Function code:
USE [fishing_Firm]
GO
/****** Object: StoredProcedure [dbo].[insertBids] Script Date: 21.12.2019 16:49:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[insertBids] @id_bids int
AS
BEGIN
 -- SET NOCOUNT ON added to prevent extra result sets from
 -- interfering with SELECT statements.
 SET NOCOUNT ON;
 if not exists (select id_sailing from Sailings, where date_sail Bids = Bids.[Release date] and date_return = Bids.[Return date] and Bids.Id_bids = @id_bids)
BEGIN
 insert into Sailings (date_sail, date_return, id_boat) values ((select [release date] from Bids where Bids.Id_bids = @id_bids), (select [return date] from Bids where Bids.Id_bids = @id_bids), (select id_boat from BoatPassport, Bids where name = Bids.[Name of vessel] and Bids.Id_bids = @id_bids))
END
else
BEGIN
 Sailings date_sail update set = (select [release date] from Bids where Bids.Id_bids = @id_bids), date_return = (select [return date] from Bids where Bids.Id_bids = @id_bids), id_boat = (select id_boat from BoatPassport, Bids where name = Bids.[Name of vessel] and Bids.Id_bids = @id_bids) from (SELECT TOP 10 * FROM Sailings , where Bids Sailings.date_sail = Bids.[Release date] and Sailings.date_return = BIds.[Return date] ) as Selected
 where Sailings.id_sailing = Selected.id_sailing
END
 if not exists (select TeamName from Teams, Bids where TeamName = Bids.[Team name] and Bids.Id_bids = @id_bids)
BEGIN
 insert into Teams (TeamName, id_sailing) values ((select Bids.[Team name] from Bids where Bids.Id_bids = @id_bids), (id_sailing from select Sailings, where Bids Sailings.date_sail = Bids.[Release date] and Sailings.date_return = Bids.[Return date] and Bids.Id_bids = @id_bids)) 
END
else
BEGIN
 update Teams set TeamName = (select Bids.[Team name] from Bids where Bids.Id_bids = @id_bids), id_sailing = (select id_sailing from Sailings, where Bids Sailings.date_sail = Bids.[Release date] and Sailings.date_return = Bids.[Return date] and Bids.Id_bids = @id_bids) from Teams inner join Bids on TeamName = Bids.[Team name]
END
 if not exists (select name,adress from TeamMembers, Bids where (name = Bids.[Name of member команды1] and adress = Bids.Address1 and Bids.Id_bids = @id_bids) or (name = Bids.[Name of member команды2] and adress = Bids.Address2) or (name = Bids.[Name of member команды3] and adress = Bids.Адрес3 and Bids.Id_bids = @id_bids) or
 (name = Bids.[Name of member команды4] and adress = Bids.Адрес4 and Bids.Id_bids = @id_bids) or (name = Bids.[Name of member команды5] and adress = Bids.Адрес5 and Bids.Id_bids = @id_bids))
BEGIN
 insert into TeamMembers (name,adress, id_position, id_team) values ( (select Bids.[Name of member команды1] from Bids where Bids.Id_bids = @id_bids), (select Bids.Address1 from Bids where Bids.Id_bids = @id_bids), (select id_position from Positions, where namePosition Bids = Bids.Должность1 and Bids.Id_bids = @id_bids), (select id_team from Teams, Bids where TeamName = Bids.[Team name] and Bids.Id_bids = @id_bids)),
 ( (select Bids.[Name of member команды2] from Bids where Bids.Id_bids = @id_bids), (select Bids.Address2 from Bids where Bids.Id_bids = @id_bids), (select id_position from Positions, where namePosition Bids = Bids.Должность2 and Bids.Id_bids = @id_bids), (select id_team from Teams, Bids where TeamName = Bids.[Team name] and Bids.Id_bids = @id_bids)),
 ( (select Bids.[Name of member команды3] from Bids where Bids.Id_bids = @id_bids), (select Bids.Адрес3 from Bids where Bids.Id_bids = @id_bids), (select id_position from Positions, where namePosition Bids = Bids.Должность3 and Bids.Id_bids = @id_bids), (select id_team from Teams, Bids where TeamName = Bids.[Team name] and Bids.Id_bids = @id_bids)),
 ( (select Bids.[Name of member команды4] from Bids where Bids.Id_bids = @id_bids), (select Bids.Адрес4 from Bids where Bids.Id_bids = @id_bids), (select id_position from Positions, where namePosition Bids = Bids.Должность4 and Bids.Id_bids = @id_bids), (select id_team from Teams, Bids where TeamName = Bids.[Team name] and Bids.Id_bids = @id_bids)),
 ( (select Bids.[Name of member команды5] from Bids where Bids.Id_bids = @id_bids), (select Bids.Адрес5 from Bids where Bids.Id_bids = @id_bids), (select id_position from Positions, where namePosition Bids = Bids.Должность5 and Bids.Id_bids = @id_bids), (select id_team from Teams, Bids where TeamName = Bids.[Team name] and Bids.Id_bids = @id_bids))

END
 else 
BEGIN

END
 if not exists (select Catches.id_catch from Catches,Bids, CatchPlacements where Catches.id_catchPlacements = CatchPlacements.id_catchPlacement and namePlacement = Bids.[The fishery] and Bids.Id_bids = @id_bids)
BEGIN
 insert into Catches (id_sailing,id_catchPlacements) values ((select Sailings from id_sailing, Bids where Bids.[Release date] = Sailings.date_sail and Bids.[Return date] = Sailings.date_return and Bids.Id_bids = @id_bids), (select id_catchPlacement from CatchPlacements, where namePlacement Bids = Bids.[The fishery] and Bids.Id_bids = @id_bids)) 
END

END


My version of update data in the table TeamMembers:
update TeamMembers set name = case when name = Bids.[Name of member команды1] then Bids.[Name of member команды1] when name = Bids.[Name of member команды2] then Bids.[Name of member команды2] when name = Bids.[Name of member команды3] then Bids.[Name of member команды3] when name = Bids.[Name of member команды4] then Bids.[Name of member команды4] else Bids.[Name of member команды5] end, address = case when address = Bids.[Address1] then Bids.[Address1] when adress = Bids.[Address2] then Bids.[Address2] when adress = Bids.[Адрес3] then Bids.[Адрес3] 
 when adress = Bids.[Адрес4] then Bids.[Адрес4] else Bids.Адрес5 end, id_position = case when (select id_position from Positions) = (select id_position from Positions, where namePosition Bids = Bids.Должность1 and Bids.Id_bids = @id_bids) then (select id_position from Positions, where namePosition Bids = Bids.Должность1 and Bids.Id_bids = @id_bids) when (select id_position from Positions) = (select id_position from Positions, where namePosition Bids = Bids.Должность2 and Bids.Id_bids = @id_bids) then (select id_position from Positions, where namePosition Bids = Bids.Должность2 and Bids.Id_bids = @id_bids)
 when (select id_position from Positions) = (select id_position from Positions, where namePosition Bids = Bids.Должность3 and Bids.Id_bids = @id_bids) then (select id_position from Positions, where namePosition Bids = Bids.Должность3 and Bids.Id_bids = @id_bids) when (select id_position from Positions) = (select id_position from Positions, where namePosition Bids = Bids.Должность4 and Bids.Id_bids = @id_bids) then (select id_position from Positions, where namePosition Bids = Bids.Должность4 and Bids.Id_bids = @id_bids) else 
 (select id_position from Positions, where namePosition Bids = Bids.Должность5 and Bids.Id_bids = @id_bids) end, id_team = (select id_team from Teams, Bids where TeamName = Bids.[Team name] and Bids.Id_bids = @id_bids) from Bids


Table columns from which the add or update:
5dfe30279b6b2039194230.png
April 3rd 20 at 18:56
1 answer
April 3rd 20 at 18:58
update TeamMembers set name = has no value. For each field you need to write a separate subquery.

And use this design:
DECLARE @INT ID_TeamMembers
SELECT @ID_TeamMembers = ID FROM TeamMembers WHERE ...
IF @ID_TeamMembers IS NULL
 INSERT INTO TeamMembers ...
 SET @ID_TeamMembers = IDENT_CURRENT( 'TeamMembers' ) -- not NULL, may need to be
ELSE
 UPDATE TeamMembers SET ... WHERE ( ID = @ID_TeamMembers )
Can I write some update for different values of the Bids ? - Eryn_Da commented on April 3rd 20 at 19:01
@Eryn_DaCan. - Sigmund_Lang commented on April 3rd 20 at 19:04
@Sigmund_Lang, And can help with the query to insert from Bids in TeamMemebers . And that what I write somehow gets a lot of requests - Eryn_Da commented on April 3rd 20 at 19:07

Find more questions by tags SQLTransact-SQLSQL Server