1

我有一个 sql server 表,其中包含一个名为 [type] 的 int 列,该列可以为空。我有以下将类型作为输入的存储过程:

    @Jobtype VARCHAR(50) = '',  
    @Language INT
AS
BEGIN
    SELECT [TabId]      
      ,CASE
            WHEN @Language = 2 THEN [TabNameTextLang2]
            ELSE [TabNameTextLang1]
        END AS TabName
      ,CASE
            WHEN @Language = 2 THEN [PageTitleLang2]
            ELSE [PageTitleLang1]
        END AS PageTitle
      ,[TabLink]
      ,[Type]
      ,[Priority]
      ,[DutyStationId]
      ,[DateCreated]
      ,[DateModified]
      ,[Deleted]
    FROM dbo.hr_Tabs
    WHERE Deleted = 0 AND 
    ((@Jobtype = '' and [Type] is null) or  ([Type] = CASt(@Jobtype as int )))

END

该表有一行类型为空。我希望当我将 @jobtype 作为 '' 传递时,只选择该行,否则行按其类型显示。但是当我尝试传递上面的存储过程参数时

exec myproce '', 1

我得到所有记录

请在我的案例中建议如何根据类型过滤记录

4

2 回答 2

2

Use:

((@Jobtype = '' and [Type] is null) or
 (@Jobtype <> '' and [Type] = CAST(@Jobtype as int )))

(I would also generally suggest to use NULL rather than an empty string as your special marker value)

于 2013-11-04T09:57:48.200 回答
-1

选择

FROM dbo.hr_Tabs
CROSS APPLY (select [Type] as dummy intersect select nullif(@Jobtype, '')) x
WHERE Deleted = 0

为了得到一个完全匹配的先生。史密斯的回答(逻辑相同):

FROM dbo.hr_Tabs
WHERE Deleted = 0 and exists 
(select [Type] as dummy intersect select nullif(@Jobtype, ''))
于 2013-11-04T10:11:54.720 回答