IF EXISTS(select 1 from TABLEA trn WITH(NOLOCK)
INNER JOIN TABLEB mst
ON trn.ID = mst.ID
Where trn.ID = 1 and trn.SeqNo=@SeqNo )
OR NOT EXISTS(select 1 from TABLEA trn WITH(NOLOCK)
INNER JOIN TABLEB mst
ON trn.ID = mst.ID
where trn.SeqNo=@SeqNo)
BEGIN
--Do something clever here
END
ELSE
BEGIN
--Throw custom error here
END
如果值为null或满足第一个条件,我有此查询执行某些操作,否则我必须向用户抛出一些自定义错误消息。
有没有其他方法可以做到这一点或者我可以将此查询简化为更快的查询。
答案 0 :(得分:1)
喜欢这个
SELECT CASE
WHEN EXISTS(select 1 from TABLEA trn WITH(NOLOCK)
INNER JOIN TABLEB mst ON trn.ID = mst.ID
Where trn.ID = 1 and trn.SeqNo=@SeqNo)
OR
NOT EXISTS(select 1 from TABLEA trn WITH(NOLOCK)
INNER JOIN TABLEB mst ON trn.ID = mst.ID
where trn.SeqNo=@SeqNo)
THEN 'EXISTS'
ELSE 'NOT EXISTS'
END
答案 1 :(得分:0)
请检查以下查询是否有帮助:
select case when trn.ID = 1 then 'EXISTS' else 'NOT EXISTS' END as status
from TABLEA trn WITH(NOLOCK)
INNER JOIN TABLEB mst
ON trn.ID = mst.ID
Where trn.SeqNo=@SeqNo;
答案 2 :(得分:0)
IF EXISTS(select 1 from TABLEA trn WITH(NOLOCK) INNER JOIN TABLEB mst
ON trn.ID = mst.ID
Where (trn.ID = 1 and trn.SeqNo=@SeqNo) OR trn.SeqNo<>@SeqNo)
BEGIN
Select 'Exists'
END
ELSE
BEGIN
Select 'Not Exists'
END