如果[@myTable] .MessageId,我想从下面的查询中选择所有记录 在连接中将值视为0,不存在。
DECLARE @myTable TABLE
(
MessageId BIGINT ,
RecieverId VARCHAR(100)
)
INSERT INTO @myTable
( MessageId ,
RecieverId
)
SELECT MAX(MessageID) ,
ReceiverID
FROM dbo.CM_MessageStatus
WHERE ReceiverID IN ( SELECT *
FROM string_split(@UserID, ',') )
AND DeliveredDate IS NOT NULL
GROUP BY ReceiverID
SELECT COUNT(CM_MessageStatus.MessageID) AS MessageID ,
dbo.CM_MessageStatus.ReceiverID
FROM dbo.CM_MessageStatus
LEFT JOIN @myTable ON [@myTable].RecieverId = CM_MessageStatus.ReceiverID
AND CM_MessageStatus.MessageID > [@myTable].MessageId
WHERE --DeliveredDate IS NULL AND
- 如果[@myTable] .MessageId不存在,则将其视为0
CM_MessageStatus.MessageID > [@myTable].MessageId
AND ReceiverID IN ( SELECT *
FROM string_split(@UserID, ',') )
GROUP BY dbo.CM_MessageStatus.ReceiverID
注意:我想检查[@myTable] .MessageId是否不存在,考虑是否为0
答案 0 :(得分:0)
INSERT INTO @myTable
( MessageId ,
RecieverId
)
SELECT MAX(MessageID) ,
ReceiverID
FROM dbo.CM_MessageStatus
WHERE ReceiverID IN ( SELECT *
FROM string_split(@UserID, ',') )
AND DeliveredDate IS NOT NULL
GROUP BY ReceiverID
IF @@ROWCOUNT= 0
insert into @myTable values(0,0)
select * from @mytable
ELSE
select * from @myTable