Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 what's wrong here?

Author  Topic 

gsnk
Starting Member

24 Posts

Posted - 2002-06-21 : 11:21:03
I have two pieces of code:
  • Sample ONE is working perfectly fine inside a stored procedure.

  • Sample TWO returns all the records I want but

  • Sample THREE is the one that's causing me trouble. It will only return one (the last) record.

What am I missing here? Thanks for helping!!

-- Sample ONE:

SELECT @ActiveUsers = @ActiveUsers + ', ' + CAST(e.Username AS varchar(30))
FROM TicketUsers tu
INNER JOIN Employee e ON tu.UserID = e.ID
WHERE tu.TicketID = @ParentID
AND tu.DateClose IS NULL

SET @ActiveUsers = SUBSTRING(@ActiveUsers, 3, 100)
SELECT @ActiveUsers

-- Sample TWO:

SELECT e.* FROM dbo.fn_TaskOwners(@TaskID) t
INNER JOIN Employee e ON t.ID = e.ID
ORDER BY e.LastName

-- Sample THREE:

SELECT @String = @String + ', ' + CAST(e.FirstName + ' ' + e.LastName AS VARCHAR(100))
FROM dbo.fn_TaskOwners(@TaskID) t
INNER JOIN Employee e ON t.ID = e.ID
ORDER BY e.LastName

SET @String = SUBSTRING(@String, 3, 100)

SELECT @String



Gero

**It's better to light a candle than to curse the darkness.**

colinm
Yak Posting Veteran

62 Posts

Posted - 2002-06-21 : 11:42:35
Perhaps a possible NULL value creeping in?
Make sure FirstName & LastName are NOT NULL


Go to Top of Page

gsnk
Starting Member

24 Posts

Posted - 2002-06-21 : 11:46:23
There are no null values in that particular test set I'm using. on top of that I also used coalesce to verify this...

Gero

**It's better to light a candle than to curse the darkness.**
Go to Top of Page

colinm
Yak Posting Veteran

62 Posts

Posted - 2002-06-21 : 11:56:28
I would change this line:

SET @String = SUBSTRING(@String, 3, 100)

to this:

SET @String = SUBSTRING(@String, 3, len(@String))




Go to Top of Page

gsnk
Starting Member

24 Posts

Posted - 2002-06-21 : 12:16:19
Colin, that's a good one. I inherited this code and didn't even think about that... ;) Any thoughts on the original problem, though?

Gero

Edited by - gsnk on 06/21/2002 12:18:24
Go to Top of Page

ToddV
Posting Yak Master

218 Posts

Posted - 2002-06-21 : 13:47:12
This is a bug with the order by. I do not remember the resolution. Perhaps you could order these in a subquery.

SELECT @String = @String + ', ' + CAST(e.FirstName + ' ' + e.LastName AS VARCHAR(100))
FROM (SEleCT TOP 100 PERCENT E.FistName,E.LastName
FROM dbo.fn_TaskOwners(@TaskID) t
INNER JOIN Employee e ON t.ID = e.ID
ORDER BY e.LastName) AS A

SET @String = SUBSTRING(@String, 3, 100)

SELECT @String


Go to Top of Page
   

- Advertisement -