使用SQL加入从函数返回的逗号分隔数据

时间:2014-04-08 10:41:36

标签: sql sql-server sql-server-2008 join

我正在努力解决这个问题,但我无法解决。我有数据以逗号分隔列,我将其与函数分开,我需要返回所有以逗号分隔的值。

我正在执行类似外部联接ApplicationDocument表

的操作

以下是查询:

SELECT PA.Id,
       Ad.Id,
       Ad.DocumentTitle,
       Ad.Path
       ,
       S.value
FROM dbo.ApplicationDocument AS Ad
right JOIN dbo.PostApplication AS PA
ON Ad.ApplicationId = PA.Id
JOIN dbo.Post P
ON PA.JobId = P.Id
outer APPLY dbo.fnSplit(P.RequiredDocuments,',') as S
WHERE S.Value = Ad.DocumentTitle

4 个答案:

答案 0 :(得分:2)

CROSS APPlY  (select value 
             from  dbo.fnSplit(P.RequiredDocuments,',')  
             WHERE Value =Ad.DocumentTitle) as  S 

答案 1 :(得分:1)

SELECT PA.Id, 
       Ad.Id,
       Ad.DocumentTitle,
       Ad.Path,
   X.Value
FROM dbo.ApplicationDocument AS Ad  
JOIN dbo.PostApplication AS PA 
ON Ad.ApplicationId = PA.Id 
JOIN dbo.Post P 
ON PA.JobId = P.Id
OUTER APPLY 
(SELECT Value FROM dbo.fnSplit(P.RequiredDocuments,',') as S 
WHERE S.Value = Ad.DocumentTitle) X

答案 2 :(得分:0)

我在 @Humpty Dumpty @ t-clausen-dk 的帮助下提出了以下查询。

这是我需要的。此查询现在获取所有记录,现在我可以使用where子句进行过滤。

SELECT PA.Id AS ApplicationId,
       Ad.Id AS ApplicationDocumentId,
       Ad.DocumentTitle,
       Ad.Path,
       S.value
FROM dbo.ApplicationDocument AS Ad
right JOIN dbo.PostApplication AS PA
ON Ad.ApplicationId = PA.Id
JOIN dbo.Post P
ON PA.JobId = P.Id
cross APPLY dbo.fnSplit(P.RequiredDocuments,',') as S

where PA.Id = SomeId here

答案 3 :(得分:-1)

检查括号,')'。它使用不当:

 SELECT PA.Id,Ad.Id,Ad.DocumentTitle,Ad.Path 
 FROM dbo.ApplicationDocument AS Ad  
 INNER JOIN dbo.PostApplication AS PA 
  ON Ad.ApplicationId = PA.Id 
 INNER JOIN dbo.Post P 
  ON PA.JobId = P.Id
 CROSS APPlY  (select value from  dbo.fnSplit(P.RequiredDocuments,',') 
  WHERE Value =Ad.DocumentTitle))