dbo.GetSearchUserList.sql
17.1 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
USE [AIADatabaseV5]
GO
/****** Object: StoredProcedure [dbo].[GetSearchUserList] Script Date: 1/29/2018 1:04:13 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[GetSearchUserList]--'gagan','','','',0,0,1
-- Add the parameters for the stored procedure here
@sFirstName varchar(100) = '', @sLastName varchar(100) = '', @sEmailId varchar(100) = '',
@sAccoutNumber varchar(100) ='', @iUserTypeId int, @iAccountTypeId int, @iLoginUserType int
AS
BEGIN
IF 1=0 BEGIN
SET FMTONLY OFF
END
DECLARE @SQL NVARCHAR(MAX)
-- create a temporary table to store the desired results of user on the basis of parameter
CREATE TABLE #UserResult
(
Id INT,
FirstName VARCHAR(100),
LastName VARCHAR(100),
LoginId VARCHAR(50),
EmailId VARCHAR(50),
UserTypeTitle VARCHAR(50),
Password VARCHAR(50),
CreationDate DATETIME,
ModifiedDate DATETIME,
AccountNumber VARCHAR(50) DEFAULT '',
AccountTypeTitle VARCHAR(50) DEFAULT '',
EditionType VARCHAR(50) DEFAULT '',
UserStatus VARCHAR(8),
UserTypeId INT,
EditionTypeId INT DEFAULT '',
Createdby VARCHAR(50) DEFAULT '',
Modifiedby VARCHAR(50) DEFAULT '',
DeactivationDate DATETIME
)
/*SET @sFirstName = REPLACE(@sFirstName,' ',' OR ')
SET @sLastName = REPLACE(@sLastName,' ',' OR ')*/
SET @SQL = ''
IF LEN(@sAccoutNumber) > 0 OR @iAccountTypeId > 0
BEGIN
-- fetch account number, state, zip, country of the license to which the user is belonged
SET @SQL = 'INSERT INTO #UserResult (Id, FirstName, LastName, LoginId, EmailId, UserTypeTitle, Password, CreationDate,
ModifiedDate, AccountNumber, AccountTypeTitle, EditionType, UserStatus, UserTypeId, EditionTypeId,Createdby,Modifiedby,DeactivationDate)
SELECT AIAUser.Id, ISNULL(AIAUser.FirstName,''''), ISNULL(AIAUser.LastName,''''), AIAUser.LoginId, ISNULL(AIAUser.EmailId,'''') as EmailId,
UserType.Title as UserTypeTitle, AIAUser.Password, AIAUser.CreationDate, ISNULL(AIAUser.ModifiedDate,'''') as ModifiedDate,
ISNULL(License.AccountNumber,'''') as AccountNumber, ISNULL(AccountType.Title,'''') as AccountTypeTitle,
ISNULL(Edition.Title,'''') as EditionType,
(CASE AIAUser.IsActive WHEN 1 THEN ''Active'' ELSE ''Inactive'' END) as UserStatus,
UserType.Id as UserTypeId, ISNULL(Edition.Id,'''') as EditionTypeId,
(select concat(u.FirstName,'' '',u.LastName) from AIAUser u where u.Id=AIAUser.CreatorId) Createdby,
(select concat(u.FirstName,'' '',u.LastName) from AIAUser u where u.Id=AIAUser.ModifierId) Modifiedby,
AIAUser.DeactivationDate
FROM AIAUser
INNER JOIN UserType ON UserType.Id = AIAUser.UserTypeId
INNER JOIN AIAUserToLicenseEdition ON AIAUser.Id = AIAUserToLicenseEdition.UserId
INNER JOIN LicenseToEdition ON AIAUserToLicenseEdition.LicenseEditionId = LicenseToEdition.Id
INNER JOIN License ON LicenseToEdition.LicenseId = License.Id
INNER JOIN AccountType ON AccountType.Id = License.AccountTypeId
INNER JOIN Edition ON Edition.Id = LicenseToEdition.EditionId
WHERE
License.IsActive = 1
AND UserType.Priority >' +CONVERT(VARCHAR(20),@iLoginUserType)
IF LEN(@sAccoutNumber)>0
BEGIN
SET @SQL = @SQL + ' AND License.AccountNumber = '''+@sAccoutNumber+''''
END
IF @iAccountTypeId > 0
BEGIN
SET @SQL = @SQL + ' AND License.AccountTypeId = '''+CONVERT(VARCHAR(20),@iAccountTypeId)+''''
END
IF LEN(@sFirstName)>0
BEGIN
SET @SQL = @SQL + ' AND (AIAUser.FirstName LIKE ''%'+@sFirstName+'%'')' --CONTAINS(AIAUser.FirstName, '''+@sFirstName+''')'
END
IF LEN(@sLastName)>0
BEGIN
SET @SQL = @SQL + ' AND (AIAUser.LastName LIKE ''%'+@sLastName+'%'')'--CONTAINS(AIAUser.LastName, '''+@sLastName+''')'
END
IF LEN(@sEmailId)>0
BEGIN
SET @SQL = @SQL + ' AND AIAUser.EmailId = '''+@sEmailId+''''
END
IF @iUserTypeId>0
BEGIN
SET @SQL = @SQL + ' AND AIAUser.UserTypeId = '''+CONVERT(VARCHAR(20),@iUserTypeId)+''''
END
Print @SQL
EXEC SP_EXECUTESQL @SQL
END
ELSE
BEGIN
SET @SQL = 'INSERT INTO #UserResult (Id, FirstName, LastName, LoginId, EmailId, UserTypeTitle, Password, CreationDate,
ModifiedDate, UserStatus, UserTypeId,Createdby,Modifiedby,DeactivationDate)
SELECT AIAUser.Id, ISNULL(AIAUser.FirstName,''''), ISNULL(AIAUser.LastName,''''),
AIAUser.LoginId, ISNULL(AIAUser.EmailId,''''), UserType.Title, AIAUser.Password, AIAUser.CreationDate,
ISNULL(AIAUser.ModifiedDate,''''), (CASE AIAUser.IsActive WHEN 1 THEN ''Active'' ELSE ''Inactive'' END),
UserType.Id,
(select concat(u.FirstName,'' '',u.LastName) from AIAUser u where u.Id=AIAUser.CreatorId) Createdby,
(select concat(u.FirstName,'' '',u.LastName) from AIAUser u where u.Id=AIAUser.ModifierId) Modifiedby,
AIAUser.DeactivationDate
FROM AIAUser
INNER JOIN UserType ON UserType.Id = AIAUser.UserTypeId
WHERE UserType.Title in (''General Admin'')'
IF LEN(@sFirstName)>0
BEGIN
SET @SQL = @SQL + ' AND (AIAUser.FirstName LIKE ''%'+@sFirstName+'%'')'--CONTAINS(AIAUser.FirstName, '''+@sFirstName+''')'
END
IF LEN(@sLastName)>0
BEGIN
SET @SQL = @SQL + ' AND (AIAUser.LastName LIKE ''%'+@sLastName+'%'')'--CONTAINS(AIAUser.LastName, '''+@sLastName+''')'
END
IF LEN(@sEmailId)>0
BEGIN
SET @SQL = @SQL + ' AND AIAUser.EmailId = '''+@sEmailId+''''
END
IF @iUserTypeId>0
BEGIN
SET @SQL = @SQL + ' AND AIAUser.UserTypeId = '''+CONVERT(VARCHAR(20),@iUserTypeId)+''''
END
-- Print @SQL
EXEC SP_EXECUTESQL @SQL
-- fetch account number, state, zip, country of the license to which the user is belonged
SET @SQL = 'INSERT INTO #UserResult (Id, FirstName, LastName, LoginId, EmailId, UserTypeTitle, Password, CreationDate,
ModifiedDate, AccountNumber, AccountTypeTitle, EditionType, UserStatus, UserTypeId, EditionTypeId,Createdby,Modifiedby,DeactivationDate)
SELECT AIAUser.Id, ISNULL(AIAUser.FirstName,''''), ISNULL(AIAUser.LastName,''''), AIAUser.LoginId, ISNULL(AIAUser.EmailId,''''),
UserType.Title, AIAUser.Password, AIAUser.CreationDate, ISNULL(AIAUser.ModifiedDate,''''),
License.AccountNumber, AccountType.Title, Edition.Title,
(CASE AIAUser.IsActive WHEN 1 THEN ''Active'' ELSE ''Inactive'' END), UserType.Id, Edition.Id ,
(select concat(u.FirstName,'' '',u.LastName) from AIAUser u where u.Id=AIAUser.CreatorId) Createdby,
(select concat(u.FirstName,'' '',u.LastName) from AIAUser u where u.Id=AIAUser.ModifierId) Modifiedby,
AIAUser.DeactivationDate
FROM AIAUser
INNER JOIN UserType ON UserType.Id = AIAUser.UserTypeId
INNER JOIN AIAUserToLicenseEdition ON AIAUser.Id = AIAUserToLicenseEdition.UserId
INNER JOIN LicenseToEdition ON AIAUserToLicenseEdition.LicenseEditionId = LicenseToEdition.Id
INNER JOIN License ON LicenseToEdition.LicenseId = License.Id
INNER JOIN AccountType ON AccountType.Id = License.AccountTypeId
INNER JOIN Edition ON Edition.Id = LicenseToEdition.EditionId
WHERE
UserType.Title NOT IN (''Super Admin'',''General Admin'')
AND License.IsActive = 1'
IF LEN(@sAccoutNumber)>0
BEGIN
SET @SQL = @SQL + ' AND License.AccountNumber = '''+@sAccoutNumber+''''
END
IF @iAccountTypeId > 0
BEGIN
SET @SQL = @SQL + ' AND License.AccountTypeId = '''+CONVERT(VARCHAR(20),@iAccountTypeId)+''''
END
IF LEN(@sFirstName)>0
BEGIN
SET @SQL = @SQL + ' AND (AIAUser.FirstName LIKE ''%'+@sFirstName+'%'')'--CONTAINS(AIAUser.FirstName, '''+@sFirstName+''')'
END
IF LEN(@sLastName)>0
BEGIN
SET @SQL = @SQL + ' AND (AIAUser.LastName LIKE ''%'+@sLastName+'%'')'--CONTAINS(AIAUser.LastName, '''+@sLastName+''')'
END
IF LEN(@sEmailId)>0
BEGIN
SET @SQL = @SQL + ' AND AIAUser.EmailId = '''+@sEmailId+''''
END
IF @iUserTypeId>0
BEGIN
SET @SQL = @SQL + ' AND AIAUser.UserTypeId = '''+CONVERT(VARCHAR(20),@iUserTypeId)+''''
END
-- Print @SQL
EXEC SP_EXECUTESQL @SQL
END
-- Selecting the desired result from temporary table
SELECT Id, FirstName, LastName, LoginId, EmailId, UserTypeTitle, Password, CreationDate,
ModifiedDate, AccountNumber, AccountTypeTitle, EditionType, UserStatus, UserTypeId, EditionTypeId,Createdby,Modifiedby,DeactivationDate FROM #UserResult
-- Dropping the temporary table
DROP TABLE #UserResult
END