Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 4729

Search Accounts with stored procedure?

$
0
0

Hello everyone, I recently started developing/improving the way my search algorithm works. There is few different queries in the system that will use this approach. I'm new in stored procedures world and from what I researched they should improve security, performance and save some code redundancy. Here is example of what I have done in one of my stored procedures for Account search:

ALTER PROCEDURE [dbo].[SearchAccounts] @Status INT = NULL, @Type INT = NULL, @FilterBy INT = NULL, @Username VARCHAR(50) = NULL, @Email VARCHAR(80) = NULL, @LastName VARCHAR(50) = NULL, @FirstName VARCHAR(50) = NULL, @FullName VARCHAR(100) = NULL WITH RECOMPILE AS DECLARE @AccountStatus INT = @Status; DECLARE @AccountType INT = @Type; DECLARE @AccountFilter INT = @FilterBy; DECLARE @AccountUsername VARCHAR(50) = @Username; DECLARE @AccountEmail VARCHAR(80) = @Email; DECLARE @AccountLast VARCHAR(50) = @LastName; DECLARE @AccountFirst VARCHAR(50) = @FirstName; DECLARE @AccountFull VARCHAR(10) = @FullName; SELECT A.AccountID, A.FirstName, A.LastName, A.Middle, A.Email, A.IsUser, A.ActiveUser, A.SystemAdmin, A.AccessType, A.AccessLevel, A.UserName, A.IsStaff, A.ActiveStaff, A.Position AS PositionCode, M.Name AS Position FROM Accounts AS A LEFT OUTER JOIN Master AS M ON M.Tblid = 'STAFF_POS' AND M.Code = A.Position WHERE ( -- If Account Type is 1 (User) (@AccountType = 1 AND A.IsUser = 1) OR -- If Account Type is 2 (Staff) (@AccountType = 2 AND A.IsStaff = 1) OR -- Or if Account type is 0 (All accounts) (@AccountType = 0 AND 1 = 1) ) AND ( (-- If account type is user and Status is 1 (Active) or 0 (Inactive) or 2(pull active and inactive) @AccountType = 1 AND (@AccountStatus = 0 OR @AccountStatus = 1) AND ActiveUser = @AccountStatus) OR (@AccountType = 1 AND @AccountStatus = 2 AND 1 = 1 ) OR (-- If account type is staff and Status is 1 (Active) or 0 (Inactive) or 2 (pull active and inactive) @AccountType = 2 AND (@AccountStatus = 0 OR @AccountStatus = 1) AND ActiveStaff = @AccountStatus) OR (@AccountType = 2 AND @AccountStatus = 2 AND 1 = 1 ) OR (-- If account type is all pull all accounts active and inactive (@AccountType != 1 AND @AccountType != 2 AND 1 = 1) ) ) AND ( -- Filter is 1 then check user name. (@AccountFilter = 1 AND A.UserName LIKE '%'+@AccountUsername+'%') OR -- Filter is 2 then check email. (@AccountFilter = 2 AND A.Email = @AccountEmail)

-- Here if filter is 3 then I should check First or Last or Full Name.

-- Still not sure what is the best approach to filter on the name fields. ) ORDER BY A.LastName, A.FirstName


As you can see query above has few different filters. First user can choose if they want to search Users, Staff or pull All account types. Then to choose if they want to pull Active, Inactive or all records. Last thing is to pick the filter. I give them an option to search Username, Email or Name. Each Account has First, Last name. What would be a good option to search for those names? Check just one full name or I have to check first and last separately? First and Last name allow this set of characters in the Accounts form : A-Z, space, dash, apostrophe, period, comma - no other special characters


Viewing all articles
Browse latest Browse all 4729

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>