dbo.usp_InsertAIAUser.sql
7.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
USE [AIADatabaseV5]
GO
/****** Object: StoredProcedure [dbo].[usp_InsertAIAUser] Script Date: 1/31/2018 12:54:16 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
-- =============================================
ALTER PROCEDURE [dbo].[usp_InsertAIAUser]
-- Add the parameters for the stored procedure here
@sLoginId varchar(50), @sPassword varchar(50), @sFirstname varchar(50), @sLastname varchar(50),
@iUserTypeId tinyint, @sEmailId varchar(50), @iSecurityQuesId tinyint, @sSecurityAnswer varchar(50)='',
@iCreatorId int, @iLicenseId int, @iEditionId 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
DECLARE @iAIAUserId int
DECLARE @iActive tinyint
DECLARE @dtCurrentDate datetime
DECLARE @sErrorStatus char(2)
DECLARE @sInvalidLicenseToEdition varchar(100)
-- to store the user type id of general admin
DECLARE @iGAUserTypeId tinyint
-- to store the role id of general admin
DECLARE @iGARoleId tinyint
set @Status = 0;
-- set the parameters to default values
SET @iActive = 1
SET @dtCurrentDate = getdate()
SET @sErrorStatus = 'oks'
SET @sInvalidLicenseToEdition = 'Edition does not exists for this license.'
-- fetch the usertype id of the general admin
SELECT @iGAUserTypeId = Id FROM UserType WHERE Title = 'General Admin'
-- fetch the role id of the general admin
SELECT @iGARoleId = Id FROM Role WHERE Title = 'General Admin Role'
IF @iSecurityQuesId = 0
BEGIN
SET @iSecurityQuesId = NULL
END
IF LEN(@sSecurityAnswer) = 0
BEGIN
SET @sSecurityAnswer = NULL
END
-- insert the user detail in AIAUser
if (Select count(*) from AIAUser Where LoginId=@sLoginId)>0
begin
set @Status=1 -- UserName Already Exist
end
else if (Select count(*) from AIAUser Where EmailId=@sEmailId)>0
begin
set @Status=2 -- Email Id Already Exist
end
else
begin
INSERT INTO AIAUser(LoginId, Password, Firstname, Lastname, UserTypeId, EmailId, IsActive, SecurityQuestionId,
SecurityAnswer, CreatorId, CreationDate, ModifierId, ModifiedDate) VALUES(@sLoginId, @sPassword, @sFirstname,
@sLastname, @iUserTypeId, @sEmailId, @iActive, @iSecurityQuesId, @sSecurityAnswer,
@iCreatorId, @dtCurrentDate, @iCreatorId, @dtCurrentDate)
SET @iAIAUserId = SCOPE_IDENTITY()
-- if user type is general admin then inserts map its role with newly generated UserId
IF @iUserTypeId = @iGAUserTypeId
BEGIN
-- insert the mapping of user with role into AIAUserActivity
INSERT INTO AIAUserActivity(UserId, RoleId) VALUES(@iAIAUserId, @iGARoleId)
END
ELSE
BEGIN
-- select the id of edition mapped with the license id
SELECT @iLicenseEditionId = LicenseToEdition.Id FROM LicenseToEdition
WHERE LicenseToEdition.LicenseId = @iLicenseId AND LicenseToEdition.EditionId = @iEditionId
IF @@ROWCOUNT = 0
BEGIN
RAISERROR(@sInvalidLicenseToEdition,16,61)
END
-- insert the mapping of user with license edition into AIAUserToLicenseEdition
INSERT INTO AIAUserToLicenseEdition(UserId, LicenseEditionId) VALUES(@iAIAUserId, @iLicenseEditionId)
END
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