当行为空时,绝对不返回任何内容

时间:2013-02-08 20:11:03

标签: sql-server dotnetnuke

我在SQL Server 2008之上使用DotNetNuke。 当以下代码返回NULL值时,我试图绝对没有返回任何内容。我不想要一个空行,我不想要“null”我绝对不想要任何东西。这甚至可能吗?我尝试过使用COALESCE,但我要么缺少某些东西,要么就是为了这个目的。

DECLARE @Category table (Category varchar(15), Deleted bit)

INSERT INTO @Category (Category, Deleted)
SELECT 'Benefits', 0
UNION ALL
SELECT 'UR', 0
UNION ALL
SELECT 'Events', 1

DECLARE @Staff Table (DNNUserID int, InternalStaff bit, 
Deleted bit, InactiveStaff bit)

INSERT INTO @Staff (DNNUserID, InternalStaff, Deleted, InactiveStaff)
SELECT '556', 1, 0, 0
UNION ALL
SELECT '423', 0, 0, 0
UNION ALL
SELECT '111', 1, 0, 1
UNION ALL
SELECT '222', 1, 1, 0

SELECT Category FROM @Category WHERE Deleted = 0
UNION ALL
SELECT CASE WHEN '111' IN 
    (SELECT DnnUserID FROM @Staff WHERE Deleted = 0 AND 
    InactiveStaff = 0 AND InternalStaff = 1) 
    THEN 'Internal Use Only' 
ELSE NULL END

变量表只是为了便于使用,有两个实际的表格与我实际需要使用的名称大致相同。

我不能使用存储过程来执行这些操作,它必须是一个简单的select语句或标量UDF,这样我们使用的telerik控件才能理解它。

我无法直接访问DNN的实际背景(因此我无法编辑任何DNN或Telerik的代码),只能访问服务器,并且只能通过主DNN Web界面将select语句添加到Telerik控件中

这个想法是,如果一个人是我工作的公司的内部成员,我们会看到分类表中的主要列表,以及下拉列表中的“仅供内部使用”条目。如果其他人使用它,比如说,我们客户的一名员工只看到了类别中的列表。

这可行吗? 我现在正试图不使用某个功能,因为我无法在现场系统中测试它(我今天在家工作并且VPN已关闭),但如果有必要,我可以并将在我家测试使用上面的SQL的数据库。

修改

我可以在函数中大致使用它:

DECLARE @DNNUserID int = 111

DECLARE @Category table (Category varchar(15), Deleted bit)

INSERT INTO @Category (Category, Deleted)
SELECT 'Benefits', 0
UNION ALL
SELECT 'UR', 0
UNION ALL
SELECT 'Events', 1

DECLARE @Staff Table (DNNUserID int, InternalStaff bit, Deleted bit, 
InactiveStaff bit)

INSERT INTO @Staff (DNNUserID, InternalStaff, Deleted, InactiveStaff)
SELECT '556', 1, 0, 0
UNION ALL
SELECT '423', 0, 0, 0
UNION ALL
SELECT '111', 1, 0, 1
UNION ALL
SELECT '222', 1, 1, 0

IF @DNNUserID NOT IN (SELECT DnnUserID FROM @Staff WHERE Deleted = 0 
AND InactiveStaff = 0 AND InternalStaff = 1)
    BEGIN
        SELECT Category FROM @Category WHERE Deleted = 0
    END
IF @DNNUserID IN (SELECT DnnUserID FROM @Staff WHERE Deleted = 0 
AND InactiveStaff = 0 AND InternalStaff = 1)
    BEGIN
        SELECT Category FROM @Category WHERE Deleted = 0
        UNION ALL
        SELECT 'Internal Use Only'
    END

但是!我更愿意找到一种方法来做到这一点,而不必诉诸于一个功能这可能吗?

2 个答案:

答案 0 :(得分:1)

DECLARE @Category table (Category varchar(15), Deleted bit)

INSERT INTO @Category (Category, Deleted)
SELECT 'Benefits', 0
UNION ALL
SELECT 'UR', 0
UNION ALL
SELECT 'Events', 1

DECLARE @Staff Table (DNNUserID int, InternalStaff bit, 
Deleted bit, InactiveStaff bit)

INSERT INTO @Staff (DNNUserID, InternalStaff, Deleted, InactiveStaff)
SELECT '556', 1, 0, 0
UNION ALL
SELECT '423', 0, 0, 0
UNION ALL
SELECT '111', 1, 0, 1
UNION ALL
SELECT '222', 1, 1, 0

SELECT Category FROM @Category WHERE Deleted = 0
UNION ALL
Select * from
(
SELECT CASE WHEN '111' IN 
    (SELECT DnnUserID FROM @Staff WHERE Deleted = 0 AND 
    InactiveStaff = 0 AND InternalStaff = 1) 
    THEN 'Internal Use Only' 
ELSE NULL END as ARow
) a 
where ARow is not null

答案 1 :(得分:1)

这可能对你有用。 EXISTS听起来很适合你想要做的事情。

DECLARE @Category table (Category varchar(15), Deleted bit)

INSERT INTO @Category (Category, Deleted)
SELECT 'Benefits', 0
UNION ALL
SELECT 'UR', 0
UNION ALL
SELECT 'Events', 1

DECLARE @Staff Table (DNNUserID int, InternalStaff bit, 
Deleted bit, InactiveStaff bit)

INSERT INTO @Staff (DNNUserID, InternalStaff, Deleted, InactiveStaff)
SELECT '556', 1, 0, 0
UNION ALL
SELECT '423', 0, 0, 0
UNION ALL
SELECT '111', 1, 0, 1
UNION ALL
SELECT '222', 1, 1, 0

SELECT Category FROM @Category WHERE Deleted = 0
UNION ALL

SELECT 'Internal Use Only'
WHERE EXISTS
    (
    SELECT 1 
    FROM @Staff
    WHERE Deleted = 0 AND 
      InactiveStaff = 0 AND 
      InternalStaff = 1 AND
      DNNUserID = 111
    )