我正在尝试查看 SQL smallint 的 status_id 字段值 -1 ,并获取不具有 -1 的记录该字段为-1。我存储的proc内容如下:
SELECT DISTINCT TOP 7
cs.case_id,
cs.status_id,
cm.company_name,
cs.created_time,
cs.severity_id,
cs.last_updated_time,
COALESCE(NULLIF(cs.priority,''), 'Medium') AS case_priority
FROM
tblcase cs with (nolock)
INNER JOIN tblcompany cm with (nolock) ON (cs.company_id=cm.company_id)
WHERE
CONVERT(INT, cs.status_id) <> -1 AND
(cs.cas_case_owner = @userId AND cs.is_notify_co = 1) OR
(cs.activity_owner = @userId AND cs.is_notify_ao = 1)
ORDER BY cs.severity_id DESC, cs.case_id ASC
我已经尝试了CONVERT(int, cs.status_id) <> -1)
,cs.status_id <> CONVERT(smallint, -1)
,cs.status_id != CAST('-1' AS smallint)
等等,但我仍然继续使用-1作为status_id获取记录。在downvoting之前,请帮助我理解我做错了什么。
答案 0 :(得分:2)
我认为您忘记使用括号进行OR
操作
cs.status_id <> -1 AND
( -- open
(cs.cas_case_owner = @userId AND cs.is_notify_co = 1)
OR
(cs.activity_owner = @userId AND cs.is_notify_ao = 1)
) -- close
请在此处查看我的另一个答案 - SQL Server Left Join Counting
答案 1 :(得分:1)
试试这个
SELECT DISTINCT TOP 7
cs.case_id,
cs.status_id,
cm.company_name,
cs.created_time,
cs.severity_id,
cs.last_updated_time,
COALESCE(NULLIF(cs.priority,''), 'Medium') AS case_priority
FROM
tblcase cs with (nolock)
INNER JOIN tblcompany cm with (nolock) ON (cs.company_id=cm.company_id)
WHERE ISNULL(CAST(cs.status_id AS INT),0)<> -1
AND cs.is_notify_co = 1
AND
(
cs.cas_case_owner = @userId
OR
cs.activity_owner = @userId
)
ORDER BY cs.severity_id DESC, cs.case_id ASC
答案 2 :(得分:1)
试试这个:
SELECT DISTINCT TOP 7
cs.case_id,
cs.status_id,
cm.company_name,
cs.created_time,
cs.severity_id,
cs.last_updated_time,
COALESCE(NULLIF(cs.priority,''), 'Medium') AS case_priority
FROM
tblcase cs with (nolock)
INNER JOIN tblcompany cm with (nolock) ON (cs.company_id=cm.company_id)
WHERE
(
( cs.status_id <> -1 AND cs.cas_case_owner = @userId) AND
( (cs.is_notify_co = 1) OR (cs.is_notify_ao = 1))
)
ORDER BY cs.severity_id DESC, cs.case_id ASC