USE [AceProject]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

create procedure [dbo].[ME_UserAccountDisable]
(
	@deleteuser varchar(100),
	@targetuser varchar(100)
)
as
declare @deleteuserid int
declare @targetuserid int

-- retrieve the user that will receive the assignment for users to be deleted
select @deleteuserid = [user_id] from users where username = @deleteuser
select @targetuserid = [user_id] from users where username = @targetuser

if @targetuserid is null
begin
	print 'Invalid target user name. Please confirm the user names.'
	return -99
end

if @deleteuser is null
begin
	print 'Invalid delete user name. Please confirm the user names.'
	return -99
end

begin try
-- start transaction
	begin tran t1

	update CUSTOM_REPORT set user_id=@targetuserid where user_id=@deleteuserid
	update DISCUSSION_MESSAGE set user_id=@targetuserid where user_id=@deleteuserid
	update DISCUSSION_SUBJECT set user_id=@targetuserid where user_id=@deleteuserid
	update [DOCUMENT] set user_id=@targetuserid where user_id=@deleteuserid
	update [MESSAGE] set sender_user_id=@targetuserid where sender_user_id=@deleteuserid
	update [MESSAGE] set RECIPIENT_USER_ID=@targetuserid where RECIPIENT_USER_ID=@deleteuserid
	update TASK set user_creator_id=@targetuserid where user_creator_id=@deleteuserid
	update TASK_HISTORY set user_id=@targetuserid where user_id=@deleteuserid
	update TASK_USER set user_id=@targetuserid where user_id=@deleteuserid
	update TIMESHEET_INOUT set user_id=@targetuserid where user_id=@deleteuserid
	update TIMESHEET_LINE set user_id=@targetuserid where user_id=@deleteuserid
	update TIMESHEET_PERIOD set user_id=@targetuserid where user_id=@deleteuserid
	delete from USER_PROJECT where user_id=@deleteuserid
	delete from USERS where user_id=@deleteuserid

-- commit
	commit tran t1
end try
begin catch
-- rollback
	rollback tran t1
	select ERROR_MESSAGE() AS ErrorMessage
end catch