T-SQL stored procedure to return google style “suggested” search results

前端 未结 4 1406
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-03 14:06

Ok, using SQL Server 2008. On my web page I have a textbox with jQuery-UI AutoComplete hooked up.

Now I need a stored procedure to search across all columns of a single

4条回答
  •  天涯浪人
    2021-02-03 14:19

    I have create a sample SQL that will return google style search result. You can try this T-SQL. You can also add more than one table column searching criteria.

    ALTER PROC [dbo].[USP_GetDoctorLookupList]
    (
        @SearchText varchar(50),
        @ItemCount int
    )
    AS
    BEGIN
        SET @SearchText = RTRIM(@SearchText) + '%'
        BEGIN
            SELECT TOP (@ItemCount) * 
            FROM
            (
                SELECT
                    CASE 
                        WHEN RTRIM(LTRIM(d.cdocname)) LIKE @SearchText then 1 
                        WHEN RTRIM(LTRIM(d.cdeano)) LIKE @SearchText then 2 
    
                    END OrderBy,
                    d.docid_PK,
                    d.cdocname,
                    d.cdeano
    
                FROM doctor d
    
                WHERE 
                    (d.cdocname LIKE @SearchText
                    OR d.cdeano LIKE @SearchText
                    )  
            ) Doc ORDER BY OrderBy, cdocname
        END
    END
    

提交回复
热议问题