如果StoredProcedure

时间:2016-06-20 12:53:38

标签: objective-c sql-server

我有今天发布的iPhone APP。该应用程序的数据来自数据库。测试时我注意到其中一个SP返回了数据集,其中有4个表从Table到Table3。表3某些时间不存在,具体取决于某些客户端的WHERE claus。在应用程序内我假设它存在所以我得到的结果是在数组字典中。我正在直接检查ObjectATIndex。当表没有返回条件失败并且应用程序崩溃时。

自从App今天发布以来,我现在无法更改代码。 我可以在表格中发送一些默认值。

假设我对表的查询是

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================            
-- Author:  <Author,,Name>            
-- ALTER date: <ALTER Date,,>            
-- Description: <Description,,>            
-- =============================================            
ALTER PROCEDURE [dbo].[_spGetEmpList]                  
 @strComCode as varchar(50),
 @iDepID as INT,
 @strType AS varchar(20)

AS            
BEGIN            
 DECLARE @bNameOrder as bit  

 SET NOCOUNT ON;  
 SELECT  @bNameOrder = dbo.svnGetSetupOption(@iDepID,
                                            'LastNamePreceedsFirstName',
                                            @strComCode)
 IF ( @bNameOrder IS NULL )
    SET @bNameOrder = 0

IF(@strType = 'Emp')
BEGIN

    --0**************************************            
     SELECT EmpID AS ResourceID, EmpNumber          
     FROM Emuloyeetbl
        WHERE Emuloyeetbl.IsActive = 1 AND DepID = @iDepID  
     ORDER BY EmpFName

     --1**************************************
     SELECT DepartmentName
     FROM tblDepartment
     WHERE IsActive = 1

     --2**************************************
     SELECT OptionValue AS EmpRole
     FROM EmpRole
     WHERE OptionName = 'EmployeeRole' AND DepID = @iDepID AND IsActive = 1

     --3**************************************

    Select EmployeeFirstName + ' '+EmplouyeeLastName as EmpName 
    from EmpTable
    Where DepID = @iDepID AND EmpSalary < 10
END           

END

上面的查询不会返回任何行,但在我的应用程序内我正在检查表(我假设表将存在),有什么方法我可以检查是否行 是否存在然后为EmpName发送一些空值,至少应用程序不会Crach .....

3 个答案:

答案 0 :(得分:2)

我认为这不是一个很好的解决方案。看到你被卡住了,也许这对你有用。

    Create Table #EmpTable (EmployeeFirstName Varchar(8000), EmployeeLastName Varchar(8000), EmpSalary Decimal(15,2))
    Insert #EmpTable Values ('Jill', 'Jacobs', 20000)
    Insert #EmpTable Values ('Joe', 'Johnson', 60000)

查询如下所示:

If Exists(Select null from #EmpTable Where EmpSalary < @Filter)
        Select EmployeeFirstName + ' '+EmployeeLastName as EmpName from #EmpTable Where EmpSalary < @Filter
        Else
        Select '' as EmpName 


-- If looking for less than 10 a blank row is returned.    
    Declare @Filter Decimal = 10
    If Exists(Select null from #EmpTable Where EmpSalary < @Filter)
    Select EmployeeFirstName + ' '+EmployeeLastName as EmpName from #EmpTable Where EmpSalary < @Filter
    Else
    Select '' as EmpName 

-- This returns the record for Jill Jacobs    
   Declare @Filter Decimal = 30000
   If Exists(Select null from #EmpTable Where EmpSalary < @Filter)
   Select EmployeeFirstName + ' '+EmployeeLastName as EmpName from #EmpTable Where EmpSalary < @Filter
   Else
   Select '' as EmpName 

答案 1 :(得分:0)

这是this post的替代答案:

CREATE TABLE EmpTable( EmployeeFirstName VARCHAR( 10 ), EmplouyeeLastName VARCHAR( 10 ), EmpSalary INT )
INSERT INTO EmpTable
VALUES
( 'John', 'Smith', 9 ),
( 'Bob', 'Smith', 11 )

DECLARE @MaxSalary INT
SET @MaxSalary = 7

;WITH EmpTable2 (EmpName)
AS
(
    SELECT EmployeeFirstName + ' '+EmplouyeeLastName as EmpName
    FROM EmpTable
    WHERE EmpSalary < @MaxSalary
)
SELECT *
FROM EmpTable2
/* Add Dummy row when no records have been returned by CTE */
UNION ALL
SELECT 'None' AS EmpName
WHERE NOT EXISTS( SELECT * FROM EmpTable2 )

上面的代码将CTE放在原始查询的周围。如果CTE没有返回任何记录,则UNION ALL后面的部分会有条件地添加“虚拟”行。

另一种方式:

SELECT EmpName
FROM
    ( SELECT EmpName, COUNT(*) OVER() AS Cnt
    FROM
        ( SELECT EmployeeFirstName + ' ' + EmplouyeeLastName AS EmpName
        FROM EmpTable
        WHERE EmpSalary < @MaxSalary
        UNION ALL
        SELECT 'None' AS EmpName
         ) AS a
    ) AS b
WHERE ( Cnt > 1 AND EmpName != 'None' ) OR Cnt = 1

答案 2 :(得分:0)

我没有创建任何表格,但正如Joe C所提到的,我使用了If Exists。

简单的方法:

IF EXISTS( Select EmployeeFirstName + ' '+EmplouyeeLastName as EmpName 
    from EmpTable
    Where DepID = @iDepID AND EmpSalary < 10)
     BEGIN
         Select EmployeeFirstName + ' '+EmplouyeeLastName as EmpName 
         from EmpTable
         Where DepID = @iDepID AND EmpSalary < 10
        ORDER BY EmpName
     END
     ELSE
     BEGIN
        SELECT '' AS EmpName
     END

感谢Joe C的快速帮助。