You are here: Re: Multi-table UDF not returning all rows « MsSQL Server « IT news, forums, messages
Re: Multi-table UDF not returning all rows

Posted by nicolec on 09/07/05 23:07

Sometimes you look at something SO long you don't see the obvious...
thanks for the pointer, that worked perfect. For future newsgroup
reference, the new function text is at the bottom.

>P.S. If you have proper primary key/foreign keys in place, then joining
to the tblUsers table in the SELECT statement is unnecessary.

No keys anywhere in this database and it's driving me nuts. Every join
must be explicit.

----------------------------------------------------

ALTER FUNCTION udfUplineGetCompany (@userID int)

RETURNS @upline table (companyID int, companyname varchar(100), info
varchar(100), rows int)
AS
BEGIN

DECLARE @branchID int
DECLARE @companyID int
DECLARE @tempID int
DECLARE @rows int

SET @rows = 1

INSERT INTO @upline
SELECT tblusersid, companyname, 'userID', @rows
FROM tblusers
WHERE tblusersid = @userid

SELECT @branchID = tblUsers.tblUsersID
FROM tblUsers
INNER JOIN tblUsersUsersLnk
ON tblUsers.tblUsersID = tblUsersUsersLnk.tblUsersID_Parent
WHERE tblUsersUsersLnk.tblUsersID_Child = @userid

INSERT INTO @upline
SELECT tblusersid, companyname, 'branchID', @rows
FROM tblusers
WHERE tblusersid = @branchid

SET @tempID = @branchID

WHILE @rows = 1
BEGIN
SELECT @companyID = tblUsers.tblUsersID
FROM tblUsers
INNER JOIN tblUsersUsersLnk
ON tblUsers.tblUsersID = tblUsersUsersLnk.tblUsersID_Parent
WHERE tblUsersUsersLnk.tblUsersID_Child = @tempID
AND tblUsersId <> 6

SET @rows = @@rowcount

INSERT INTO @upline
SELECT tblusersid, companyname, 'companyID', @rows
FROM tblusers
WHERE tblusersid = @companyID
AND @rows = 1

SET @tempID = @companyID


END

RETURN

END

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация