Hey Luke,
I need to create a new stored procedure similar to DSC_User_Search with one more parameter SalesRep. I am trying to modify this, but its little confusing. Can you please help.
Where all do i need to added my parameter for search?? Also @asc is which parameter? What does it do?
Thanks
CREATE PROCEDURE [dbo].[DSC_User_Custom_Search]
@keywords NVARCHAR (MAX)=NULL, @from DATETIME='17530101', @to DATETIME='99991231', @rowsPerPage INT=24, @rowIndex INT=0, @salesRep NVARCHAR(265), @orderBy NVARCHAR (100)='created', @asc BIT=0, @totalUsers INT OUTPUT
AS
BEGIN
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
CREATE TABLE #userAll (rowNumber INT PRIMARY KEY, userId INT, CreateDate DATETIME, Email NVARCHAR(256))
DECLARE @ASC_DESC NVARCHAR(10)
IF (@asc = 1)
SET @ASC_DESC = ''
ELSE
SET @ASC_DESC = ' DESC'
DECLARE @KeywordMatchedUsers NVARCHAR(MAX)
SET @KeywordMatchedUsers = ''
IF ((@keywords IS NOT NULL) AND (LEN(@keywords) > 0))
BEGIN
SELECT @KeywordMatchedUsers = @KeywordMatchedUsers + '
(SELECT u.userId
FROM users usersToCheck
JOIN dbo.DSC_User u
ON usersToCheck.userId = u.userId
WHERE CONTAINS(u.*,' + QUOTENAME(dbo.DSC_Escape(dbo.DSC_Escape(Value, N'\'), N';'), '''') + ')
UNION
SELECT u.userId
FROM users u
JOIN dbo.DSC_Address a
ON (a.userId = u.userId) AND (a.isBilling = 1)
WHERE CONTAINS(a.*,' + QUOTENAME(dbo.DSC_Escape(dbo.DSC_Escape(Value, N'\'), N';'), '''') + ')
UNION
SELECT u.userId
FROM users u
JOIN aspnet_Users aspUser
ON (aspUser.LoweredUserName = u.UserName) AND (aspUser.ApplicationId = ''0d663bb4-8150-479b-880d-cca0342f9e35'')
JOIN aspnet_Membership aspMembership
ON (aspMembership.UserId = aspUser.UserId) AND (aspMembership.ApplicationId = ''0d663bb4-8150-479b-880d-cca0342f9e35'')
WHERE CONTAINS(aspMembership.*,' + QUOTENAME(dbo.DSC_Escape(dbo.DSC_Escape(Value, N'\'), N';'), '''') + '))
INTERSECT'
FROM DSC_Parse_Values(@keywords, DEFAULT)
SET @KeywordMatchedUsers = LEFT(@KeywordMatchedUsers, LEN(@KeywordMatchedUsers) - 9) -- remove tailing INTERSECT
END
ELSE
SET @KeywordMatchedUsers = 'SELECT userId FROM users'
DECLARE @searchStatement NVARCHAR(MAX)
SET @searchStatement = '
;WITH users AS
(
SELECT u.userId, u.UserName
FROM dbo.DSC_User u
WHERE u.userName <> ''_DSCRoot_''
AND @from <= u.created
AND u.created <= @to
),
keywordMatchedUsers AS
(' + @KeywordMatchedUsers + '
),
matchedUsers AS
(
SELECT DISTINCT userId
FROM keywordMatchedUsers
)
INSERT INTO #userAll
SELECT ROW_NUMBER() OVER (ORDER BY ' + QUOTENAME(@OrderBy) + @ASC_DESC + ') AS RowNumber, mu.userId, u.created, aspMembership.Email
FROM matchedUsers mu
JOIN DSC_User u
ON u.userId = mu.userId
JOIN aspnet_Users aspUser
ON (aspUser.LoweredUserName = u.UserName) AND (aspUser.ApplicationId = ''0d663bb4-8150-479b-880d-cca0342f9e35'')
JOIN aspnet_Membership aspMembership
ON (aspMembership.UserId = aspUser.UserId) AND (aspMembership.ApplicationId = ''0d663bb4-8150-479b-880d-cca0342f9e35'')'
--PRINT @searchStatement
EXECUTE sp_executesql @searchStatement, N'@from DATETIME, @to DATETIME', @from, @to
SELECT @totalUsers = COUNT(*)
FROM #userAll
SELECT *
INTO #user
FROM #userAll u
WHERE u.RowNumber BETWEEN @rowIndex + 1 AND @rowIndex + @rowsPerPage
ORDER BY u.RowNumber
DROP TABLE #userAll
EXEC DSC_User_GetByTempTable
DROP TABLE #user
END
|