﻿USE [AIADatabaseV5]
GO
/****** Object:  StoredProcedure [dbo].[usp_UpdateAIAUser]    Script Date: 2/1/2018 12:15:55 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		Magic Software
-- Create date: 29-Apr-2009
-- Description:	To create a new AIA User
-- =============================================

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[usp_UpdateAIAUser]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[usp_UpdateAIAUser]
GO

CREATE PROCEDURE [dbo].[usp_UpdateAIAUser] 
	-- Add the parameters for the stored procedure here
	@sLoginId varchar(50), @sPassword varchar(50), @sFirstname varchar(50), @sLastname varchar(50),
	 @sEmailId varchar(50),@id int,
	@iCreatorId int, @isActive tinyint,@Status int out 
AS
BEGIN
	
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	
	BEGIN TRY
		BEGIN TRANSACTION
			DECLARE @iLicenseEditionId int

			
			set @Status = 0;
			-- set the parameters to default values
			
			-- insert the user detail in AIAUser 
			if  (Select count(*) from AIAUser Where LoginId=@sLoginId and Id != @id)>0
			begin 
			set @Status=1 -- UserName Already Exist
			end
			else if  (Select count(*) from AIAUser Where EmailId=@sEmailId and Id != @id)>0
			begin 
			set @Status=2 -- Email Id Already Exist
			end
			else 
			begin
			UPDATE AIAUser
				SET LoginId =@sLoginId, 
				Password=@sPassword , 
				FirstName =@sFirstName , 
				LastName =@sLastName , 
				-- UserTypeId =@iUserTypeId ,
				EmailId =@sEmailId , 
				IsActive =@isActive ,
				ModifierId =@iCreatorId , 
				ModifiedDate =getdate() WHERE Id = @id			
			set @Status=3	
			End
				
			COMMIT TRANSACTION
		--Print @Status
		--SELECT @sErrorStatus as SPStatus
		--SELECT @Status as SPStatus
	END TRY
	BEGIN CATCH
		IF @@TRANCOUNT > 0
			 ROLLBACK TRANSACTION
			--set @Status=4
		--SELECT @Status as SPStatus
		--SELECT Error_Message() as SPStatus
		
	END CATCH
    
END


GO
