1

您好我正在使用联接表查询来检索一些数据..

USE [AccountingSystem_TEST]
GO
/****** Object:  StoredProcedure [dbo].[spHRLeaveEntitlementBuildTemp]    Script Date: 03/14/2013 09:18:06 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER Procedure [dbo].[spHRLeaveEntitlementBuildTemp]
    (   
     @CreatedUserID as nvarchar(9)
    ,@EmployeeID nvarchar(9) 
    ,@EmpName nvarchar(150)
    )
AS
BEGIN

    Select A.CodeHRLeaveType,
        COALESCE(B.LeaveBalance) LeaveBalance           
    From HRLeaveType A
        LEFT JOIN HRLeaveMaster B 
        On A.CodeHRLeaveType = B.CodeHRLeaveType
    Where   B.EmployeeID = @EmployeeID

    Delete dbo.HrLeaveMasterTemp
    Where UserID = @CreatedUserID 

    INSERT INTO [dbo].[HrLeaveMasterTemp]
       ( [UserID]
        ,[EmployeeID] 
        ,[EmpName]
        ,[CodeHRLeaveType]
        ,[LeaveBalance]
        ,[Status] 
        )
    Select 
         @CreatedUserID             
        ,[EmployeeID]
        ,@EmpName
        ,[CodeHRLeaveType]
        ,[LeaveBalance]
        ,[Status]           
    From dbo.[HRLeaveMaster]
    Where EmployeeID =@EmployeeID
END
--Select * From HRLeaveMasterTemp

如果leavebalance为空,我需要表格显示0..但语句显示错误..请帮助我..

4

2 回答 2

1

在 Oracle 中,空函数的替换与 NVL 函数一起使用。在 MySql 中,空函数的替换与 IFNULL 函数一起使用。

请查看http://www.w3schools.com/sql/sql_isnull.asp。这可能会对您有所帮助。

声明应该是——

Select A.CodeHRLeaveType,
    IFNULL(B.LeaveBalance,0) LeaveBalance           
From HRLeaveType A
    LEFT JOIN HRLeaveMaster B 
    On A.CodeHRLeaveType = B.CodeHRLeaveType
Where   B.EmployeeID = @EmployeeID
于 2013-03-14T03:32:40.763 回答
1

利用COALESCE

Select  A.CodeHRLeaveType,
        COALESCE(B.LeaveBalance) LeaveBalance           
From    HRLeaveType A
        LEFT JOIN HRLeaveMaster B 
            On A.CodeHRLeaveType = B.CodeHRLeaveType
Where   B.EmployeeID = @EmployeeID
于 2013-03-14T03:14:18.723 回答