1

I have the following query which returns more than 1 result (max is about 10rows not more!), how can I return all results as a single string?

SELECT DISTINCT(e.Username)
FROM TblLeaveEmployee l
JOIN TblLeaveApprovalsBy a on l.EmployeeId = a.UserID
JOIN TblEmployee e on l.EmployeeId = e.id
where l.EmployeeId IN 
  (select UserID 
   FROM TblLeaveApprovalsBy 
   WHERE ApprovalUserID IN
     (SELECT ApprovalUserID from TblLeaveApprovalsBy where UserID = 77))

Have tried using STUFF as follows but its not working:

DECLARE @CodeNameString varchar(100)
SELECT 
   @CodeNameString = STUFF(
SELECT DISTINCT(e.Username)
FROM TblLeaveEmployee l
JOIN TblLeaveApprovalsBy a on l.EmployeeId = a.UserID
JOIN TblEmployee e on l.EmployeeId = e.id
where l.EmployeeId IN 
  (select UserID 
   FROM TblLeaveApprovalsBy
   WHERE ApprovalUserID IN
     (SELECT ApprovalUserID from TblLeaveApprovalsBy where UserID = 77)))
rikket
  • 2,257
  • 7
  • 45
  • 72
  • 1
    try this [http://stackoverflow.com/questions/17990986/concatenate-rows-for-one-column-sql](http://stackoverflow.com/questions/17990986/concatenate-rows-for-one-column-sql) – Bhavesh Parekh Dec 11 '13 at 09:24

3 Answers3

1
   DECLARE @CodeNameString VARCHAR(MAX)=NULL
   SELECT @CodeNameString = COALESCE(@CodeNameString+',' ,'') + Username 
   FROM
   (
   SELECT DISTINCT(e.Username) Username
   FROM TblLeaveEmployee l
   JOIN TblLeaveApprovalsBy a on l.EmployeeId = a.UserID
   JOIN TblEmployee e on l.EmployeeId = e.id
   where   l.EmployeeId IN (select UserID FROM TblLeaveApprovalsBy
              WHERE ApprovalUserID IN(SELECT ApprovalUserID from TblLeaveApprovalsBy    where UserID = 77))) x
   SELECT @CodeNameString
Jayvee
  • 10,270
  • 3
  • 26
  • 39
  • Msg 156, Level 15, State 1, Line 11 Incorrect syntax near the keyword 'SELECT'. – rikket Dec 11 '13 at 09:43
  • That's within your query, I just wrapped it and added the first three lines and the last line. Try running the inner query and see if you can find the problem there – Jayvee Dec 11 '13 at 09:47
  • Ok, I see what the problem is; just edited it (put an x to alias the inner query) – Jayvee Dec 11 '13 at 09:56
1
DECLARE t CURSOR FOR  
SELECT DISTINCT(e.Username)
FROM TblLeaveEmployee l
JOIN TblLeaveApprovalsBy a on l.EmployeeId = a.UserID
JOIN TblEmployee e on l.EmployeeId = e.id
where l.EmployeeId IN 
(select UserID 
   FROM TblLeaveApprovalsBy 
   WHERE ApprovalUserID IN
     (SELECT ApprovalUserID from TblLeaveApprovalsBy where UserID = 77))

open t

declare @v varchar(255)
declare @Res varchar(max)
 set @Res = ''
 set @v = ''
FETCH NEXT FROM t into @v
while (@@FETCH_STATUS = 0) begin
    set @Res = @Res + @v + '|'
    FETCH NEXT FROM t into @v
end
print @Res

close t
deallocate t
Jade
  • 2,922
  • 1
  • 10
  • 9
0

This will be a problem if you have huge amount of row.

Try This

DECLARE t CURSOR FOR  
select Col1 from [Table] where crite1 = ???

open t

declare @v varchar(255)
declare @Res varchar(max)
 set @Res = ''
 set @v = ''
FETCH NEXT FROM t into @v
while (@@FETCH_STATUS = 0) begin
    set @Res = @Res + @v + '|'
    FETCH NEXT FROM t into @v
end
print @Res

close t
deallocate t
Jade
  • 2,922
  • 1
  • 10
  • 9