2013-03-14 105 views
1

你好我使用的是加入表查詢來獲取一些數據..SQL語句連接表是空查詢

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 [email protected] 
END 
--Select * From HRLeaveMasterTemp 

如果leavebalance爲null,我需要的表格顯示0 ..但聲明顯示錯誤。請幫助我..

+0

嘗試編輯答案,是否有利於你... – 2013-03-14 03:36:40

回答

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 
+0

它仍然給我的錯誤.. :( – user1617943 2013-03-14 03:17:30

+0

@ user1617943:什麼是錯誤 – ankurtr 2013-03-14 03:19:55

+0

不能?在代碼中看到任何錯誤,生成的錯誤信息是什麼? – 2013-03-14 03:25:08

1

在甲骨文更換空函數的使用與NVL功能。在MySql中使用 替換null函數與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 
+0

它的顯示s 消息195,級別15,狀態10,過程spHRLeaveEntitlementBuildTemp,行12 'IFNULL'不是公認的內置函數名稱。 – user1617943 2013-03-14 03:40:56

+0

'選擇 \t \t A.CodeHRLeaveType \t \t,B.LeaveBalance \t(LeaveBalance + ISNULL(LeaveBalance,0))\t \t \t \t \t \t \t 從HRLeaveType甲 \t加入HRLeaveMaster B關於A.CodeHRLeaveType = B. CodeHRLeaveType \t Where B.EmployeeID = @ EmployeeID' – user1617943 2013-03-14 03:42:12

+0

它解決了您的問題。我已經完成了從上述鏈接,並執行SELECT IFNULL(1,0)在我的phpmyadmin.Anyway ..謝謝.. – 2013-03-14 03:47:57