you need at least a table variable in betweenlike belowdeclare @TableA table(ID varchar(10),FirstName varchar(50),[Status] int)insert @TableAvalues('01', 'Person A', 1),('02', 'Person B', 1),('03', 'Person C', 0),('04', 'Person D', 0)declare @TableB table(ID varchar(10),FirstName varchar(50))insert @TableBvalues('01', 'Person A'),('02', 'Person B')declare @Inserted_IDs table(ID varchar(10))insert @TableBOUTPUT INSERTED.ID INTO @Inserted_IDsselect ID,FirstnameFROM @TableA aWHERE NOT EXISTS ( SELECT 1 FROM @TableB WHERE FirstName = a.FirstName)UPDATE aSET Status=1FROM @TableA aJOIN @Inserted_IDs iON i.ID = a.IDSELECT * FROM @TableASELECT * FROM @TableBoutput---------------------------------------------------ID FirstName Status-------------------------------------01 Person A 102 Person B 103 Person C 104 Person D 1ID FirstName-----------------------01 Person A02 Person B03 Person C04 Person D
Also not sure why you're having ID field as varchar.Its better to make it integer type otherwise you may have difficulty in manipulations using it------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/