| 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 ASquote: 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 needWhy don't you explain or show us what the second sproc does...PS Read the hint link belowBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx |
 |
|
|
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)asDeclare @ParentID as intDeclare @Ident as intDeclare @Count as intSELECT @ParentID = 0WHILE(@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 ENDENDGOI apologize. I wish I could be clearer.Thanks for the help,Shep |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
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 / Description1 / 1 / Org_A2 / 2 / Org_F3 / 1 / Org_B4 / 2 / Org_C5 / 3 / Org_D6 / 4 / Org_EWhat I need is this:ident / ParentId / Description / UltimateParentId1 / 1 / Org_A / 12 / 2 / Org_F / 23 / 1 / Org_B / 14 / 2 / Org_C / 25 / 3 / Org_D / 16 / 4 / Org_E / 2Hope that makes sense.Shep |
 |
|
|
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 #tempfrom organization--one iteration for each nesting levelwhile @@rowcount > 0begin 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_identendselect [ident] ,[parent_ident] ,root_ident ,[description] ,[is_company]from #temp Be One with the OptimizerTG |
 |
|
|
shepheml
Starting Member
4 Posts |
Posted - 2005-08-25 : 16:09:35
|
| Works beautifully. Thanks dude, very much appreciated. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
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 AnalyzerUSE NorthwindGOSET NOCOUNT ONCREATE TABLE myOrg99(ident int, ParentId int, [Description] varchar(25))GOINSERT INTO myOrg99(ident, ParentId, [Description])SELECT 1, 1, 'Org_A' UNION ALLSELECT 2, 2, 'Org_F' UNION ALLSELECT 3, 1, 'Org_B' UNION ALLSELECT 4, 2, 'Org_C' UNION ALLSELECT 5, 3, 'Org_D' UNION ALLSELECT 6, 4, 'Org_E'GOSELECT * FROM myOrg99GOCREATE FUNCTION udf_FindTopOrg (@ident int)RETURNS intAS 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 ENDGOSELECT ident, ParentId, [Description], dbo.udf_FindTopOrg(ident) AS TopOrgFROM myOrg99GODROP FUNCTION udf_FindTopOrgDROP TABLE myOrg99GO Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx |
 |
|
|
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 RootIDFROM 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.ParentIDGO Kristen |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
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 |
 |
|
|
|