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
 Transact-SQL (2000)
 nesting a stored procedure

Author  Topic 

shepheml
Starting Member

4 Posts

Posted - 2005-08-25 : 14:46:24
I need to create a stored proc. that returns all contents of one table, but I also need to pass the identity of each row to another stored proc. that will return a value to be included in the result set of the first stored proc. I've been looking for sample code, but don't really know what technical terms I should be searching for. Any assistance would be greatly appreciated.

Shep

X002548
Not Just a Number

15586 Posts

Posted - 2005-08-25 : 15:03:12
quote:
Originally posted by shepheml

I need to create a stored proc.



CREATE PROC MySproc99 AS

quote:

that returns all contents of one table, but I also need to pass the identity of each row to another stored proc.



Why? Why wouldn't you just access the data in the called sproc?

quote:

that will return a value to be included in the result set of the first stored proc.



If the table is referenced in the second sproc, you would have everything you need


Why don't you explain or show us what the second sproc does...

PS Read the hint link below


Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

shepheml
Starting Member

4 Posts

Posted - 2005-08-25 : 15:20:56
I'm sure there is an easier way to do what I need, but I'm not a SQL guy, and I inherited the DB. So here goes...

The two procs hit the same table:

CREATE TABLE [dbo].[organization] (
[ident] [int] IDENTITY (1, 1) NOT NULL ,
[parent_ident] [int] NULL ,
[description] [varchar] (64) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[is_company] [bit] NOT NULL
) ON [PRIMARY]

The table holds organizations that have parent relationships within the table (i.e. Org_A with an OrgId of 1 has a ParentId of 1 which means it is the top level Org., but Org_B has an OrgId of 2 and a ParentId of 1, Org_C has an OrgId of 3 and a ParentId of 2, and so on).

I need to return all Organizations, but I also need to "walk the table" so to speak using the OrgId to also return the Identity of the "top level" Org.

The second SP iterates thru the table to locate the top level Org and it looks like this:

create procedure Organization_Get_Department
(@OrgID as int)
as
Declare @ParentID as int
Declare @Ident as int
Declare @Count as int
SELECT @ParentID = 0
WHILE(@OrgID != @ParentID)
BEGIN
SELECT @ParentID = parent_ident, @OrgID = ident FROM organization WHERE ident = @OrgID
IF @ParentID = @OrgID
BEGIN
SELECT * FROM organization WHERE ident = @OrgID
break
END
ELSE
BEGIN
SELECT @OrgID = @ParentID
SELECT @ParentID = 0
END
END

GO


I apologize. I wish I could be clearer.

Thanks for the help,

Shep
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2005-08-25 : 15:47:44
So the Req is, Given any Particular Org, Find it's top level parent?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

shepheml
Starting Member

4 Posts

Posted - 2005-08-25 : 15:58:59
Yes, but I also do not know how to return that top level parent id with the results from the first proc. The results from Proc 1 look like this:

ident / ParentId / Description
1 / 1 / Org_A
2 / 2 / Org_F
3 / 1 / Org_B
4 / 2 / Org_C
5 / 3 / Org_D
6 / 4 / Org_E

What I need is this:

ident / ParentId / Description / UltimateParentId
1 / 1 / Org_A / 1
2 / 2 / Org_F / 2
3 / 1 / Org_B / 1
4 / 2 / Org_C / 2
5 / 3 / Org_D / 1
6 / 4 / Org_E / 2

Hope that makes sense.

Shep
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2005-08-25 : 16:03:03
How about all in one proc like this?

select [ident]
,[parent_ident]
,root_ident = parent_ident
,[description]
,[is_company]
into #temp
from organization

--one iteration for each nesting level
while @@rowcount > 0
begin
update a set
a.root_ident = b.parent_ident
from #temp a
join #temp b
on a.root_ident = b.ident
where b.Parent_ident <> a.parent_ident
and a.root_ident <> b.parent_ident
end

select [ident]
,[parent_ident]
,root_ident
,[description]
,[is_company]
from #temp



Be One with the Optimizer
TG
Go to Top of Page

shepheml
Starting Member

4 Posts

Posted - 2005-08-25 : 16:09:35
Works beautifully. Thanks dude, very much appreciated.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2005-08-25 : 16:13:41
Can I cheat and have the top Orgs Parent ID Null?

Or is it that way that you know the TOP ID = Parent IDs


Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2005-08-25 : 16:25:53
That's a lot of potential updates...

I was thinking going this route....but it's not correct yet...left so debugging code that screwed it up...now it's works 100%, just cut and paste it and run it in Query Analyzer


USE Northwind
GO

SET NOCOUNT ON
CREATE TABLE myOrg99(ident int, ParentId int, [Description] varchar(25))
GO

INSERT INTO myOrg99(ident, ParentId, [Description])
SELECT 1, 1, 'Org_A' UNION ALL
SELECT 2, 2, 'Org_F' UNION ALL
SELECT 3, 1, 'Org_B' UNION ALL
SELECT 4, 2, 'Org_C' UNION ALL
SELECT 5, 3, 'Org_D' UNION ALL
SELECT 6, 4, 'Org_E'
GO

SELECT * FROM myOrg99
GO

CREATE FUNCTION udf_FindTopOrg
(@ident int)
RETURNS int
AS
BEGIN
-- [D'oh]
-- SET @Ident = 5
-- [/D'oh]
DECLARE @ParentID int, @ident2 int
SELECT @ParentID = 1, @ident2 = 0
WHILE @ident2 <> @ParentID
BEGIN
SELECT @ParentID = ParentID, @Ident2 = ident
FROM myOrg99
WHERE ident = @ident
SET @ident = @ParentID
END
RETURN @Ident2
END
GO

SELECT ident, ParentId, [Description], dbo.udf_FindTopOrg(ident) AS TopOrg
FROM myOrg99
GO

DROP FUNCTION udf_FindTopOrg
DROP TABLE myOrg99
GO




Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2005-08-25 : 22:32:10
Hi shepheml, Welcome to SQL Team.

I've got a couple of questions.

1) Are you going to do this a lot? If consider adding a column with the "RootID" and calculating it in a trigger when something relevant changes. Otherwise the loop stuff will be slow.

2) Is the number of "ancestors" modest? Can you be sure that it will never exceed some small-ish number? if so use OUTER JOINs to get the RootID - I've borrowed Bretts sample This will be nice and quick (and can be used "set-based" style in a multi-row operation)

SELECT C.ident, C.ParentId, C.[Description], dbo.udf_FindTopOrg(C.ident) AS TopOrg,
COALESCE(GGGP.ident, GGP.ident, GP.ident, P.ident, C.ident, -1) as RootID
FROM myOrg99 C
LEFT OUTER JOIN myOrg99 P
ON P.ident = C.ParentID
LEFT OUTER JOIN myOrg99 GP
ON GP.ident = P.ParentID
LEFT OUTER JOIN myOrg99 GGP
ON GGP.ident = GP.ParentID
LEFT OUTER JOIN myOrg99 GGGP
ON GGGP.ident = GGP.ParentID
GO

Kristen
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2005-08-26 : 08:41:02
Dude!

How does that take n level of orgs?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2005-08-27 : 02:07:53
"How does that take n level of orgs?"

It doesn't! EDIT: (And it returns -1 in those circumstances!)

Quote:

Can you be sure that it will never exceed some small-ish number? if so

Kristen
Go to Top of Page
   

- Advertisement -