How do I create a stored procedure that will optionally search columns?

前端 未结 10 678
醉话见心
醉话见心 2020-12-08 08:30

I\'m working on an application for work that is going to query our employee database. The end users want the ability to search based on the standard name/department criteria

相关标签:
10条回答
  • 2020-12-08 09:21

    Write a procedure to insert all employee data whose name start with A in table??

    0 讨论(0)
  • 2020-12-08 09:27

    We can use Generic @Search Parameter and pass any value to it for searching.

    GO
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    -- =============================================
    -- Author: --
    -- Create date:
    -- Description: --
    -- =============================================
    CREATE PROCEDURE [dbo].[usp_StudentList]
        @PageNumber INT    = 1, -- Paging parameter
        @PageSize   INT    = 10,-- Paging parameter
        @Search  VARCHAR(MAX) = NULL, --Generic Search Parameter
        @OrderBy VARCHAR(MAX) = 'FirstName', --Default Column Name 'FirstName' for records ordering
        @SortDir VARCHAR(MAX) = 'asc' --Default ordering 'asc' for records ordering
    AS
    BEGIN
        SET NOCOUNT ON;
    
        --Query required for paging, this query used to show total records
        SELECT COUNT(StudentId) AS RecordsTotal FROM Student
    
        SELECT Student.*, 
            --Query required for paging, this query used to show total records filtered
            COUNT(StudentId) OVER (PARTITION BY 1) AS RecordsFiltered 
        FROM Student
        WHERE 
        --Generic Search 
        -- Below is the column list to add in Generic Serach
        (@Search IS NULL OR Student.FirstName LIKE '%'+ @Search +'%')
        OR (@Search IS NULL OR Student.LastName LIKE '%'+ @Search +'%')
        --Order BY
        -- Below is the column list to allow sorting
        ORDER BY 
        CASE WHEN @SortDir = 'asc' AND @OrderBy = 'FirstName' THEN Student.FirstName END,
        CASE WHEN @SortDir = 'desc' AND @OrderBy = 'FirstName' THEN Student.FirstName  END DESC,
        CASE WHEN @SortDir = 'asc' AND @OrderBy = 'LastName' THEN Student.LastName END,
        CASE WHEN @SortDir = 'desc' AND @OrderBy = 'LastName' THEN Student.LastName  END DESC,
        OFFSET @PageSize * (@PageNumber - 1) ROWS FETCH NEXT @PageSize ROWS ONLY;
    END
    
    0 讨论(0)
  • 2020-12-08 09:28

    I would stick with the NULL/COALESCE method over AdHoc Queries, and then test to make sure you don't have performance problems.

    If it turns out that you have slow running queries because it's doing a table scan when you're searching on columns that are indexed, you could always supplement the generic search stored procedure with additional specific ones that allow searching on these indexed fields. For instance, you could have a special SP that does searches by CustomerID, or Last/First Name.

    0 讨论(0)
  • 2020-12-08 09:34

    My first thought was to write a query something like this...

    SELECT EmpId, NameLast, NameMiddle, NameFirst, DepartmentName
      FROM dbo.Employee
           INNER JOIN dbo.Department ON dbo.Employee.DeptId = dbo.Department.Id
     WHERE IdCrq IS NOT NULL
           AND
           (
              @bitSearchFirstName = 0
              OR
              Employee.NameFirst = @vchFirstName
           )
           AND
           (
              @bitSearchMiddleName = 0
              OR
              Employee.NameMiddle = @vchMiddleName
           )
           AND
           (
              @bitSearchFirstName = 0
              OR
              Employee.NameLast = @vchLastName
           )
           AND
           (
              @bitSearchDepartment = 0
              OR
              Department.Id = @intDeptID
           )
    

    ...which would then have the caller provide a bit flag if they want to search a particular field and then supply the value if they are to search for it, but I don't know if this is creating a sloppy WHERE clause or if I can get away with a CASE statement in the WHERE clause.

    As you can see this particular code is in T-SQL, but I'll gladly look at some PL-SQL / MySQL code as well and adapt accordingly.

    0 讨论(0)
提交回复
热议问题