从多对多关系中选择查询

时间:2015-02-20 05:32:11

标签: sql-server

我的数据库中有这些表

enter image description here

我的存储过程是

ALTER procedure [dbo].[SearchEmployee]
        @Name varchar(155),
        @Gender varchar(50),
        @MaterialStatus varchar(50),
        @DOB date,
        @Designation varchar(155),
        @Salary money
AS
    SET NOCOUNT OFF;
    BEGIN
        SELECT
            intEmployeeId, 
            (vchFirstName + ' ' + ISNULL(vchMiddleName,' ') + ' ' +         vchLastName) AS Name, 
            vchGender , 
            vchMaterialStatus,
            dtDOB,
            vchDesignation,
            intSalary,  
            dtmCreatedDate,
            bitActive,
            F.vchFilename As FileName ,
            F.intFileid As Fileid 
        FROM
            Employee AS E
        FULL OUTER JOIN
            EmployeeFiles AS EF ON e.intEmployeeId = EF.intEmployeeIds
        FULL OUTER JOIN
            Files AS F ON EF.intFileids = F.intFileid
        WHERE
            vchFirstName LIKE @Name OR
            vchMiddleName LIKE @Name OR
            vchLastName LIKE @Name 
    END

此查询仅返回一行

exec SearchEmployee @Name='m', @Gender=NULL, @MaterialStatus=NULL, 
                    @DOB='0001-01-01', @Designation=NULL, @Salary=NULL

我有多名员工,其名称包含m个字符。如何从数据库中获取所有行?

1 个答案:

答案 0 :(得分:2)

根据我的理解,您需要获取名称中包含M的所有记录。因此,您需要将外卡字符%告诉刽子手,%前面的M表示M之前和{{1}之前的任意数字字符}表示M之后任意数量的任何字符。 Read more about it here

因此,您需要更改M条件

like