I am a novice script writer.
I am trying to write a stored procedure that will include some nested IF statements. The basic concept of the first statment in plain English is:
If condition A is true AND condition B is true, then execute the next If statement:
Here's my statement so far:
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[UpdateCheck] (
@DuplicateId varchar(10),
@MergeToId varchar(10)
)
as
declare @MaxDateDuplicateId datetime
declare @MaxDateMergeToId datetime
declare @MaxChangeDuplicateUser varchar(10)
declare @MaxChangeMergeUser varchar(10)
select @MaxDateDuplicateId = max(DATE_TIME) from Name_Log where ID = @DuplicateId
select @MaxDateMergeToId = max(DATE_TIME) from Name_Log where ID = @MergeToId
select @MaxChangeDuplicateUser = USER_ID from (select m.*, row_number() over (partition by ID order by date desc) as rn from
Name_log m) m2 where m2.rn = 1 and ID = @DuplicateID
select @MaxChangeMergeUser = USER_ID from (select m.*, row_number() over (partition by ID order by date desc) as rn from
Name_log m) m2 where m2.rn = 1 and ID = @MergeToID
IF
@MaxChangeDuplicateUser in (select UserID from UserMain um, Relationship r
where um.contactmaster = r.target_id and r.relation_type = '_ORG-ADMIN')
AND @MaxChangeMergeUser in ((select UserID from UserMain um, Relationship r
where um.contactmaster = r.target_id and r.relation_type = '_ORG-ADMIN')
BEGIN
if (@MaxDateDuplicateId > @MaxDateMergeToId)
BEGIN
Update m
set m.member_type = d.member_type
from Name m, Name d where m.ID = @MergeToID and d.ID = @DuplicateID
END
END
I'm getting the following error:
Incorrect syntax near the keyword 'BEGIN'.
I don't know if it's a problem with my conditional statement before the BEGIN or with the nested IF after the BEGIN.
Thanks.
I am trying to write a stored procedure that will include some nested IF statements. The basic concept of the first statment in plain English is:
If condition A is true AND condition B is true, then execute the next If statement:
If Date A is greater than Date B, then do an update.
Here's my statement so far:
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[UpdateCheck] (
@DuplicateId varchar(10),
@MergeToId varchar(10)
)
as
declare @MaxDateDuplicateId datetime
declare @MaxDateMergeToId datetime
declare @MaxChangeDuplicateUser varchar(10)
declare @MaxChangeMergeUser varchar(10)
select @MaxDateDuplicateId = max(DATE_TIME) from Name_Log where ID = @DuplicateId
select @MaxDateMergeToId = max(DATE_TIME) from Name_Log where ID = @MergeToId
select @MaxChangeDuplicateUser = USER_ID from (select m.*, row_number() over (partition by ID order by date desc) as rn from
Name_log m) m2 where m2.rn = 1 and ID = @DuplicateID
select @MaxChangeMergeUser = USER_ID from (select m.*, row_number() over (partition by ID order by date desc) as rn from
Name_log m) m2 where m2.rn = 1 and ID = @MergeToID
IF
@MaxChangeDuplicateUser in (select UserID from UserMain um, Relationship r
where um.contactmaster = r.target_id and r.relation_type = '_ORG-ADMIN')
AND @MaxChangeMergeUser in ((select UserID from UserMain um, Relationship r
where um.contactmaster = r.target_id and r.relation_type = '_ORG-ADMIN')
BEGIN
if (@MaxDateDuplicateId > @MaxDateMergeToId)
BEGIN
Update m
set m.member_type = d.member_type
from Name m, Name d where m.ID = @MergeToID and d.ID = @DuplicateID
END
END
I'm getting the following error:
Incorrect syntax near the keyword 'BEGIN'.
I don't know if it's a problem with my conditional statement before the BEGIN or with the nested IF after the BEGIN.
Thanks.