473,385 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Multi-table UDF not returning all rows

I've been tearing my hair out over this UDF. The code works within a
stored procedure and also run ad-hoc against the database, but does not
run properly within my UDF. We've been using the SP, but I do need a
UDF instead now.

All users, including branch office, sub-companies and companies and so
on up the lines are in the same table. I need a function which returns
a row for each level, eventually getting to the master company all the
way at the top, but this UDF acts as though it can't enter the loop and
only inserts the @userID and @branchID rows. I have played with the
WHILE condition to no avail.

Any ideas on what I am missing?

(Running against SQL Server 2000)
---------------------------------------------------

ALTER FUNCTION udfUplineGetCompany (@userID int)

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

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

--Insert the original user data
INSERT INTO @upline
SELECT tblusersid, companyname, 'userID'
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

--Up one level
INSERT INTO @upline
SELECT tblusersid, companyname, 'branchID'
FROM tblusers
WHERE tblusersid = @branchid

SET @tempID = @branchID

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

--Insert a row for each level up
INSERT INTO @upline
SELECT tblusersid, companyname, 'companyID'
FROM tblusers
WHERE tblusersid = @companyID

SET @tempID = @companyID

END

RETURN

END

Sep 7 '05 #1
2 1731
Well, if I try to run in as an ad-hoc query, it doesn't run properly at
all.

The use of @@rowcount is bound to go wrong. Also, the INSERT into the
table variable will be executed regardless of a match (or miss) of the
parent selection.

You should check the @@rowcount immediately after the "SELECT @CompanyID
= ..." statement. If you do not want to handle the @@rowcount result at
that point, then make sure you move the @@rowcount status to a variable
immediately after the SELECT statement.

Gert-Jan

P.S. If you have proper primary key/foreign keys in place, then joining
to the tblUsers table in the SELECT statement is unnecessary.
ni*****@octitle.com wrote:

I've been tearing my hair out over this UDF. The code works within a
stored procedure and also run ad-hoc against the database, but does not
run properly within my UDF. We've been using the SP, but I do need a
UDF instead now.

All users, including branch office, sub-companies and companies and so
on up the lines are in the same table. I need a function which returns
a row for each level, eventually getting to the master company all the
way at the top, but this UDF acts as though it can't enter the loop and
only inserts the @userID and @branchID rows. I have played with the
WHILE condition to no avail.

Any ideas on what I am missing?

(Running against SQL Server 2000)
---------------------------------------------------

ALTER FUNCTION udfUplineGetCompany (@userID int)

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

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

--Insert the original user data
INSERT INTO @upline
SELECT tblusersid, companyname, 'userID'
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

--Up one level
INSERT INTO @upline
SELECT tblusersid, companyname, 'branchID'
FROM tblusers
WHERE tblusersid = @branchid

SET @tempID = @branchID

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

--Insert a row for each level up
INSERT INTO @upline
SELECT tblusersid, companyname, 'companyID'
FROM tblusers
WHERE tblusersid = @companyID

SET @tempID = @companyID

END

RETURN

END

Sep 7 '05 #2
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

Sep 7 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: ajikoe | last post by:
Hello, Is anyone has experiance in running python code to run multi thread parallel in multi processor. Is it possible ? Can python manage which cpu shoud do every thread? Sincerely Yours,...
4
by: Frank Jona | last post by:
Intellisense with C# and a multi-file assembly is not working. With VB.NET it is working. Is there a fix availible? We're using VisualStudio 2003 Regards Frank
12
by: * ProteanThread * | last post by:
but depends upon the clique: ...
0
by: frankenberry | last post by:
I have multi-page tiff files. I need to extract individual frames from the multi-page tiffs and save them as single-page tiffs. 95% of the time I receive multi-page tiffs containing 1 or more black...
6
by: cody | last post by:
What are multi file assemblies good for? What are the advantages of using multiple assemblies (A.DLL+B.DLL) vs. a single multi file assembly (A.DLL+A.NETMODULE)?
4
by: mimmo | last post by:
Hi! I should convert the accented letters of a string in the correspondent letters not accented. But when I compile with -Wall it give me: warning: multi-character character constant Do the...
5
by: Shane Story | last post by:
I can seem to get the dimensions of a frame in a multiframe tiff. After selecting activeframe, the Width/Height is still really much larger than the page's actual dimensions. When I split a...
5
by: bobwansink | last post by:
Hi, I'm relatively new to programming and I would like to create a C++ multi user program. It's for a project for school. This means I will have to write a paper about the theory too. Does anyone...
5
by: dkelly925 | last post by:
Is there a way to add an If Statement to the following code so if data in a field equals "x" it will launch one report and if it equals "y" it would open another report. Anyone know how to modify...
0
by: Sabri.Pllana | last post by:
We apologize if you receive multiple copies of this call for papers. *********************************************************************** 2008 International Workshop on Multi-Core Computing...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.