我有应该接受几个不同参数的存储过程。根据参数查询将返回期望的结果。但是,有一个过滤器我不确定100%确定哪种是最佳选择。这是示例:
$('#frm_filterby').on('change', changeAttr);
function changeAttr() {
var elementVal = $(this).val();
$("#frm_search").removeAttr('pattern maxlength title');
switch (elementVal) {
case "1":
$("#frm_search").attr({
"type": "text",
"placeholder": "Example: tcook56",
"pattern": "[a-z0-9_-]{0,50}$",
"title": "User name allows alphanumeric (lowercase only) characters, underscore, dash - no other special characters",
"maxlength": "50",
"disabled": false,
"required": true
}).val("");
break;
case "2":
$("#frm_search").attr({
"type": "email",
"placeholder": "example@gmail.com",
"title": "Enter email address",
"maxlength": "80",
"disabled": false,
"required": true
}).val("");
break;
case "3":
$("#frm_search").attr({
"type": "search",
"placeholder": "(Last, First)",
"pattern": "[a-zA-Z][A-Za-z' .,-]{0,100}$",
"title": "A-Z, space, dash, apostrophe, period, comma - no other special characters",
"maxlength": "100",
"disabled": false,
"required": true
}).val("");
break;
default:
$("#frm_search").attr({
"type": "text",
"placeholder": "Select Search Criteria",
"disabled": true,
"required": false
}).val("");
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script language="javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<form name="frmFind" id="frmFind">
<div class="row">
<div class="form-group col-xs-12 col-sm-12 col-md-3 col-lg-3">
<select class="form-control" name="frm_status" id="frm_status" required>
<option value="">--Status--</option>
<option value="2">All</option>
<option value="1">Active</option>
<option value="0">InActive</option>
</select>
</div>
<div class="form-group col-xs-12 col-sm-12 col-md-3 col-lg-3">
<select class="form-control" name="frm_filterby" id="frm_filterby" required>
<option value="">--Search By--</option>
<option value="1">Username</option>
<option value="2">Email</option>
<option value="3">Name</option>
</select>
</div>
<div class="form-group col-xs-12 col-sm-12 col-md-3 col-lg-3">
<input type="text" class="form-control find-search" name="frm_search" id="frm_search" placeholder="Select Search Criteria" disabled>
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 col-sm-12 col-md-12 col-lg-12">
<button class="btn btn-block btn-primary">
<span class="glyphicon glyphicon-search"></span> Search
</button>
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 col-sm-12 col-md-12 col-lg-12">
<div class="alert message-submit"></div>
</div>
</div>
</form>
在上面的示例中,您可以看到他们可以选择哪些过滤器,但我认为名称以外的所有内容都很混乱。我的想法是让他们只输入第一个或最后一个或第一个和最后一个的组合,并以逗号分隔。问题是我们允许姓和名之间使用逗号,以免引起混淆。假设某人的姓氏为Doe, Junior
,在这种情况下,我的代码将假设Doe
为姓氏,Junior
为姓氏。我想知道在这种情况下什么是一个好的选择?这也是我的SQL搜索查询:
CREATE PROCEDURE [dbo].[SearchUsers]
@Status INT = NULL,
@FilterBy INT = NULL,
@Username VARCHAR(50) = NULL,
@Email VARCHAR(80) = NULL,
@LastName VARCHAR(50) = NULL,
@FirstName VARCHAR(50) = NULL
AS
SELECT A.FirstName, A.LastName, A.Middle, A.Email,
A.UserStatus, A.UserName, A.Password M.Name AS Position
FROM Accounts AS A
LEFT OUTER JOIN Master AS M
ON M.Tblid = 'POSITION'
AND M.Code = A.Position
WHERE A.IsUser = 1
AND
(
((@Status = 0 OR @Status = 1) AND A.UserStatus = @Status)
OR
(@Status = 2 AND 1 = 1)
)
AND
(
(@FilterBy = 1 AND A.UserName = @Username)
OR
(@FilterBy = 2 AND A.Email = @Email)
OR
(
@FilterBy = 3
AND
A.LastName LIKE
CASE WHEN Len(@LastName) > 0 THEN '%' + LastName + '%'
ELSE '%'
END
AND
A.FirstName LIKE
CASE WHEN Len(@FirstName) > 0 THEN '%' + @FirstName + '%'
ELSE '%'
END
)
)
OPTION (RECOMPILE)
当我输入名字和姓氏的一些值时,上面的代码工作正常。例如,如果我输入姓氏Doe
,查询将返回表中的所有记录。是否有解决此问题的好方法,并让用户可以选择搜索bot的名字和姓氏,但同时又给他们提供了仅搜索姓氏或名字的选项?
答案 0 :(得分:3)
在您的查询中,您拥有此
A.LastName LIKE
CASE WHEN Len(@LastName) > 0
THEN '%' + LastName + '%'
ELSE '%'
END
AND
您需要设置参数而不是列
CASE WHEN Len(@LastName) > 0 THEN '%' + **@LastName** + '%'
希望对您有帮助!