我想在Outer申请语句中获取一行任务ID,但SQL在外部申请声明中用红线生气我:)任何想法?
declare @OwnerUserIDs as Nvarchar(max)
select bt.*
,cu.NAME+' '+cu.SURNAME as OwnerUserName
,cu2.NAME+' '+cu2.SURNAME as CreatedUserName
,T.Data
from Business_Tasks(nolock) bt
left join CommmerceCoreReleaseV1..CORE_USERS(nolock) cu on cu.USERID=bt.OwnerUserID
left join CommmerceCoreReleaseV1..CORE_USERS(nolock) cu2 on cu2.USERID=bt.CreatedUserID
outer apply
(
SELECT @OwnerUserIDs = Data from ( select COALESCE(@OwnerUserIDs + ';', '') + convert(nvarchar,bto.ownerUserID) as Data from Business_TasksOwner(nolock) bto where bto.TaskID=13)
) T
where bt.IsActive=1 and bt.ID=13
答案 嗨,我找到了答案,我在外部申请声明中改变了:
select bt.*
,cu.NAME+' '+cu.SURNAME as OwnerUserName
,cu2.NAME+' '+cu2.SURNAME as CreatedUserName
,T.OwnerUserIDs
from Business_Tasks(nolock) bt
left join CommmerceCoreReleaseV1..CORE_USERS(nolock) cu on cu.USERID=bt.OwnerUserID
left join CommmerceCoreReleaseV1..CORE_USERS(nolock) cu2 on cu2.USERID=bt.CreatedUserID
outer apply
(
select STUFF((SELECT ';'+ convert(nvarchar,bto.OwnerUserID)
FROM Business_TasksOwner(nolock) bto
where bto.TaskID=bt.ID
FOR XML PATH('')), 1, 1, '') AS OwnerUserIDs
) T
where bt.IsActive=1 and bt.ID=13
答案 0 :(得分:0)
好的,您的查询存在一些问题:
我会尝试为您重写您的查询,但我不确定您要在此处实现的目标。我想你想在@OwnerUserIDs中找到一个用户ID列表,但我不确定应该是什么规则...