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)
 Joining tables query

Author  Topic 

hasanali00
Posting Yak Master

207 Posts

Posted - 2005-12-08 : 07:18:01
Hi
I need some help in writng this query.

I have:
Category: CategoryID, CategoryTypeID
CategoryRelations: RecordID, ParentID, ChildID

Now I want to write a query to get all the ChildIDs for a given ParentID, where ChildID is of CategoryTypeID=1

I have written the following:

CREATE PROCEDURE spv_Products_GetCategoryRelations

--------
@CategoryID int,
@CategoryTypeID int


AS

SELECT distinct c.categoryid, c.* , cr.recordid FROM
Categories c
inner join CategoryRelations cr on c.categoryid=cr.childid

where

cr.parentID=@categoryID
and
c.CategoryTypeID = @CategoryTypeID
order by c.orderingNumber asc

GO
---------------
But the above SP does not fetch any record.

Any help appreciated. Thanks

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2005-12-08 : 08:16:35
can you post some sample data and expected result ?

-----------------
[KH]
Dec-05
Go to Top of Page

Wanderer
Master Smack Fu Yak Hacker

1168 Posts

Posted - 2005-12-08 : 08:41:11
I notice you refer to catgeories in the SQL, but Category in the table listing - a typo there, somewhere?
Hmm are you passing in parametre's? (show how you are calling the SP)
Is there data?

Here is a sample that works ... edit to add your necessary, and read the note! :)


set nocount on

create table Category
( CategoryID int,
CategoryTypeID int)

create table CategoryRelations
( RecordID int,
ParentID int,
ChildID int)
go

insert into category
select 1,1 --category 1, type 1
union
select 2,1 --category 2, type 1
union
select 3,1 --category 3, type 1
union
select 4,1 --category 4, type 1
union
select 5,1 --category 5, type 1
union
select 6,2 --category 6, type 2

insert into CategoryRelations
select 1,1,2 -- record1, which is parent 1, child 2
union
select 2,1,3 -- record2, which is parent 1, child 3
union
select 2,1,4 -- record2, which is parent 1, child 4
union
select 2,5,1 -- record2, which is parent 5, child 1
union
select 2,5,6 -- record2, which is parent 5, child 6
go

CREATE PROCEDURE spv_Products_GetCategoryRelations @CategoryID int,@CategoryTypeID int
AS
--note: I haven't added error handling (@@Error), or parametre checking (making sure you receive a value)
--imho you should add these...
select
CR.ChildId --all childid's
from
CategoryRelations CR
inner join
Category C
on c.categoryid=cr.childid
where
cr.parentID = @categoryID
and c.CategoryTypeID = @CategoryTypeID
--order by c.orderingNumber asc There is no ordering number in the example structure.
order by CR.ChildId
go

declare
@CategoryID int,
@CategoryTypeID int

set @CategoryID = 1 --we want children of 1
set @CategoryTypeID = 1 -- for categorytype 1

exec spv_Products_GetCategoryRelations @CategoryID, @CategoryTypeID

drop table category
drop table CategoryRelations
drop procedure spv_Products_GetCategoryRelations

---results
-- ChildId
-- -----------
-- 2
-- 3
-- 4


*##* *##* *##* *##*

Chaos, Disorder and Panic ... my work is done here!
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-12-09 : 01:18:30
Do you need Tree structures?
http://www.nigelrivett.net/RetrieveTreeHierarchy.html

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -