-3

我有一个存储过程。我只是想将许多行值连接成单个字符串。此外,最终结果需要返回具有某一列的一行,以将所有行值串联为字符串。我尝试了下面的代码。但它抛出一个错误。请帮我找出问题所在。

代码

ALTER PROCEDURE [dbo].[Usp_getjobdetails] (@jobID INT, 
                                       @foos  VARCHAR(max)) 
--,@l varchar(max),@ts varchar(max)) 
AS 
  BEGIN 
  --select @en as int 
  --select @l as l.Location 
  --select @ts as ts.TechnicalSkills 
  SELECT jobcode, 
         jobtitle, 
         experiences, 
         (SELECT @foos = COALESCE(@foos + ',', '') 
                         + e.educationname 
          FROM   tbleducation 
          WHERE  educationid = je.educationid) AS Edu, 
         l.location,--=COALESCE( @l + ', ', '') + l.Location, 
         ts.technicalskills, 
         --=COALESCE ( @ts + ', ', '' )+ ts.TechnicalSkills,  
         jobdescription 
  FROM   tbljobs j 
         INNER JOIN tbljobs_education je 
                 ON je.jobid = j.jobid 
         INNER JOIN tbljobs_locations jl 
                 ON jl.jobid = j.jobid 
         INNER JOIN tbljobs_technicalskill jts 
                 ON jts.jobid = j.jobid 
         INNER JOIN tbleducation e 
                 ON e.educationid = je.educationid 
         INNER JOIN tbllocations l 
                 ON l.locationid = jl.locationid 
         INNER JOIN tbltechnicalskills ts 
                 ON ts.technicalskillid = jts.technicalskillid 
  WHERE  j.jobid = @jobID 
  END 

tbljobs tbl

JobCode   JobTitle Experience    Edu       Location        Description
PITCS41   JDev     12-14      BSC,BCA,MSC    Delhi      Asp.Net A highly

我需要类似上述结果的东西。

4

1 回答 1

1

我希望您的错误是子查询返回多个结果。或者尝试同时获取数据和编辑变量。

尝试以下操作:

SELECT jobcode, 
         jobtitle, 
         experiences, 
         STUFF((
            SELECT ', ' + e.educationname 
            FROM   tbleducation 
            WHERE  educationid = je.educationid
            FOR XML PATH('')
         ), 1, 2, '')  AS Edu, 
         l.location,
         ts.technicalskills, 
         jobdescription 
  FROM   tbljobs j 
         INNER JOIN tbljobs_education je 
                 ON je.jobid = j.jobid 
         INNER JOIN tbljobs_locations jl 
                 ON jl.jobid = j.jobid 
         INNER JOIN tbljobs_technicalskill jts 
                 ON jts.jobid = j.jobid 
         INNER JOIN tbleducation e 
                 ON e.educationid = je.educationid 
         INNER JOIN tbllocations l 
                 ON l.locationid = jl.locationid 
         INNER JOIN tbltechnicalskills ts 
                 ON ts.technicalskillid = jts.technicalskillid 
  WHERE  j.jobid = @jobID 

下次它会帮助给出错误信息。因为我不知道这是否是你的问题。

于 2013-05-29T10:59:04.697 回答