为什么我的查询没有显示正确的结果?

时间:2015-02-03 12:02:32

标签: sql tsql

我有很多表但是例如让我们考虑2个表,即Employees和FileEntries。我从两个表EmployeeID,FileStatus,FileName,EmployeeName中选择这些字段。 这两个表都有1个公共字段,即EmployeeID。即使FileEntries不在Employees表中,它也可以存在EmployeeID。所以我想从两个表中选择所有记录,但如果它不在Employees表中,则新字段应显示'NotRegistered'。

)
@EmployeeID int
)


Alter PROCEDURE [dbo].[Modify_RejectedFiles] -- '2015-01-06 07:41:00', '2015-01-06 07:41:00',1,3,'','20001018783815'
(  
 @FromDate SMALLDATETIME,  
 @ToDate SMALLDATETIME,  
 @OfficeID INT=0,  --it represents id of a company/branch/organization being logged in
 @Type INT=0,      --it represents type of a user being logged in i.e. 1=orgranization, 2=company, 3=branch
 @EmployerUniqueID VARCHAR(15)='',  
 @EmployeeUniqueID VARCHAR(15)=''  
)  
AS  

BEGIN  

DECLARE @Branches TABLE  
(  
 BranchID INT  
)  

IF @Type = 1  --Organization
BEGIN  
 INSERT INTO @Branches 
 SELECT BranchID From vw_OrganizationTree WHERE OrganizationID = @OfficeID  --inserts 3 always because we got only 1 organization i.e. 3
END  

IF @Type = 2  --Company
BEGIN  
 INSERT INTO @Branches 
 SELECT BranchID From vw_OrganizationTree WHERE CompanyID = @OfficeID  --inserts 3 always because we got only 1 company i.e. 3
END  

IF @Type = 3  -- i.e. Branch
BEGIN  
 INSERT INTO @Branches 
 SELECT BranchID From vw_OrganizationTree WHERE BranchID = @OfficeID  
END  

  Declare @IsRegistered varchar(20)= 'Registered'     

  If ((Select count(*) from RegisteredEmployees where EmployeeUniqueID= @EmployeeUniqueID) <1)
  Begin
   Set @IsRegistered = 'Not Yet'
  End

  Select distinct FE.EmployeeUniqueID, RE.EmployeeName, Empr.EmployerName, Br.BranchName,
          FE.IsRejected, FE.RejectedFileCreationDateTime
          From 
          File_EDREntries FE 
          left Join 
          RegisteredEmployees RE
          ON FE.EmployeeUniqueID = RE.EmployeeUniqueID 
          left Join Employers Empr
          ON RE.Employer_ID = Empr.ID 
          left Join Branches Br
          ON Br.BranchID = Empr.Branch_ID
          WHERE 
          FE.IsRejected = 1                                         --20017128203780
          AND Empr.Branch_ID in (Select BranchID from @Branches)

1 个答案:

答案 0 :(得分:0)

当RE.EmployeeUniqueID为空时使用案例然后&#39; Not Yet&#39;其他&#39;注册&#39;在最终的选择查询

中结束