简体   繁体   English

SQL Server过程从其ID以特定数字开头的表中查找行不起作用

[英]SQL Server procedure to find rows from table that its id starts with a specific number not working

I'm trying to write a SQL Server stored procedure that returns a list of string, then I will use this returned list in AjaxAutoCompleteExtender in my ASP.Net website, so when the user start typing a string (eg 60), the procedure should return the names of all the users where there ids' starts with the input string (601, 602, 605, ..etc). 我正在尝试编写一个返回字符串列表的SQL Server存储过程,然后在我的ASP.Net网站的AjaxAutoCompleteExtender中使用此返回列表,因此,当用户开始键入字符串(例如60)时,该过程应返回其中id以输入字符串(601、602、605等)开头的所有用户的名称。

I wrote the following T-SQL stored procedure: 我编写了以下T-SQL存储过程:

CREATE PROCEDURE [dbo].[_GetUser_AutoComplete]
    @user_id int
AS
    IF ISNULL (CONVERT(nvarchar(50), @user_id), '') is not null
    BEGIN
         SELECT 
             [User].user_fname + ' ' + [User].user_mname + ' ' + [User].user_lname
         FROM 
             [User]
         WHERE 
             CONVERT(nvarchar(50), [user_id]) LIKE CONVERT(nvarchar(50), @user_id)
END

In my ASP.Net website, I have written code to call this procedure like this: 在我的ASP.Net网站上,我编写了如下代码来调用此过程:

public List<string> getUser_AutoComplete(int? user_id)
{
        SqlParameter UserId = new SqlParameter("@user_id ", user_id);
        UserId.SqlDbType = SqlDbType.Int;
        UserId.Direction = ParameterDirection.Input;

        var res = this.Database.SqlQuery<string>("exec _GetUser_AutoComplete @user_id", UserId).ToList<string>();
        return res;
    }

And in my ASP.Net website, I have the following markup in my Default.aspx : 在我的ASP.Net网站上,我的Default.aspx具有以下标记:

<asp:TextBox ID="TextBoxUserId" runat="server"></asp:TextBox>
<act:AutoCompleteExtender 
     ServiceMethod="SearchUsers"
     MinimumPrefixLength="2" CompletionInterval="100" 
     EnableCaching="false" CompletionSetCount="10"
     TargetControlID="TextBoxUserId"
     ID="AutoCompleteExtender1" runat="server" FirstRowSelected="false">
</act:AutoCompleteExtender>

At the SearchUsers code is this: SearchUsers代码上是这样的:

[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static List<string> SearchUsers(string prefixText, int count)
{
    List<string> users = new List<string>();
    int userId;

    bool isInputUserId = Int32.TryParse(prefixText, out userId);

    if(isInputUserId)
    {
            users = getUser_AutoComplete(userId);
    }

    return users;
}

The problem is that there is not any autocompletion until the user types the whole id of the user he is searching for (ie if I type 60 in the textbox nothing happen if there is not a user with the id 60, If I write 601 the autoCompleteExtender works and return the full name user with the id 601). 问题在于,只有在用户键入要搜索的用户的整个ID时,才有任何自动补全功能(即,如果我在文本框中键入60,如果没有ID为60的用户,则不会发生任何事情;如果我输入601, autoCompleteExtender起作用并返回ID为601的全名用户。

I guess that the problem iis in my SQL Server procedure. 我猜问题出在我的SQL Server过程中。

Any ideas, and thanks in advance 任何想法,并在此先感谢

If I understand correctly your objective, you want to retrieve all the users where the their user_id starts with the same digits passed to the stored procedure as @user_id parameter. 如果我正确理解了您的目标,那么您想要检索其user_id以与@user_id参数一起传递给存储过程的相同数字开头的所有用户。

If this is correct you need to add a wildcard to the end of the search text used with the LIKE operator 如果正确,则需要在LIKE运算符使用的搜索文本的末尾添加通配符

ALTER PROCEDURE [dbo].[_GetUser_AutoComplete]
@user_id int
AS
declare @usertext nvarchar(50)
select @usertext = CONVERT(nvarchar(50), @user_id)
IF ISNULL (@usertext, '') is not null
BEGIN
    SELECT [User].user_fname + ' ' + [User].user_mname + ' ' + [User].user_lname
    FROM [User]
    WHERE CONVERT(nvarchar(50), [user_id]) LIKE @usertext + '%'
END

Here, adding the wildcard '%' extends the results of the LIKE search including every user_id that starts with the same digits passed. 在这里,添加通配符'%'扩展了LIKE搜索的结果,包括每个以相同的数字开头的user_id。 For example, if you pass 1 then the result will include the users with user_id equals to 1, 10, 11, 100, 101, 199 and so on... 例如,如果传递1,则结果将包括user_id等于1、10、11、100、101、199的用户,依此类推...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM