我想使用select语句过滤表数据,我有四列,并且我还有四个文本框可以在每一列中进行搜索,并且当我在文本框中输入值时,我可以在任何框中输入值)我想返回与我输入的值匹配的记录,我该怎么做?

ALTER PROCEDURE dbo.test_search

    (
        @ID int,
    @FirstName nvarchar(50),
    @MiddleName nvarchar(50),
    @LastName nvarchar(50)
    )

AS
    SELECT     ID, FirstName, MiddleName, LastName
    FROM         StudentsInformation
    WHERE (@ID IS NULL OR StudentsInformation.ID = @ID) AND
           (@FirstName IS NULL OR StudentsInformation.FirstName = @FirstName )AND
           (@MiddleName IS NULL OR StudentsInformation.MiddleName = @MiddleName )AND
           (@LastName IS NULL OR StudentsInformation.LastName = @LastName )
    RETURN

最佳答案

编辑:

SELECT
id
, firstname
, middlename
, lastname
FROM studentsinformation
WHERE id = @id
OR firstname LIKE '%' + @firstname + '%'
    OR middlename LIKE '%' + @middlename + '%'
    OR lastname LIKE '%' + @lastname + '%'


如果要选择所有复选框都为真的记录,则可以将OR替换为AND。

08-06 22:27