SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


merge a result set with a table but update according to existing values in target


merge a result set with a table but update according to existing values in target

Author
Message
Sarsoura
Sarsoura
SSC Veteran
SSC Veteran (212 reputation)SSC Veteran (212 reputation)SSC Veteran (212 reputation)SSC Veteran (212 reputation)SSC Veteran (212 reputation)SSC Veteran (212 reputation)SSC Veteran (212 reputation)SSC Veteran (212 reputation)

Group: General Forum Members
Points: 212 Visits: 164
I have a table sectioninstructor
Sectionid,
Instructorid,
Instructorrole
-----------------------------------------------------------------------
Instructorrole (1 for primary instructor, 2 for secondary instructor)

Note I want to make sure that each section has only one primary instructor
i.e. if the new instructor is assigned primary role and there is already a record in the table that has the same sectionid and instructorrole =1 on inserting the new record I should change the instructorrole to 2
I will receive (a list of sectionids), instructorid, roleid I need to merge this list with the table sectioninstructor

I tried another way but I got an error message matched can’t have two updates
ALTER PROCEDURE [dbo].[usp_SectionInstructor_InsertList]
(
@SectionList nvarchar(1000),
@RoleID int,--(1 for primary and 2 for secondary)
@InstructorID int
)
AS
begin

select T.items as SectionID,
@InstructorID as instructorID,
@RoleID as InstructorROle


from
dbo.fn_SP2_Split(@SectionList,',') T

as Source

on
Target.SectionID=source.SectionID
and
Target.InstructorID=Source.SectionID

when Matched then
update
set Target.InstructorRole=@RoleID,
Target.updatedby=@updatedby

when not matched then
insert
(SectionID,
InstructorID,
InstructorRole
)
values
(Source.SectionID,
Source.InstructorID,
@RoleID
);

---the function


--splits a list to

CREATE FUNCTION [dbo].[fn_SP2_Split]
(@String nvarchar(max),
@Delimiter nchar(1)
)
returns @temptable TABLE (items nvarchar(max))
as
begin
declare @idx int
declare @slice nvarchar(250)

select @idx = 1
if len(@String)<1 or @String is null return

while @idx!= 0
begin
set @idx = charindex(@Delimiter,@String)
if @idx!=0
set @slice = left(@String,@idx - 1)
else
set @slice = @String

if(len(@slice)>0)
insert into @temptable(Items) values(@slice)

set @String = right(@String,len(@String) - @idx)
if len(@String) = 0 break
end
return
end
Stewart "Arturius" Campbell
Stewart "Arturius" Campbell
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16381 Visits: 7413
The error in the MERGE statement is encountered when one row in the target table will be addressed by more than one row in the source data set.
This imples either that the matching criterium is incorrect, or there is more than one update per record in the source dataset.

____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search