Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

select in recursive table Expand / Collapse
Author
Message
Posted Thursday, February 14, 2013 2:05 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, February 14, 2013 5:21 AM
Points: 25, Visits: 96
Hi,
in my database i have these two table

CREATE TABLE [dbo].[CATEGORIES](
[ID_CATEGORIE] [int] IDENTITY(1,1) NOT NULL primary key,
[CAT_ID_PARENT] [int] NULL,
[CAT_DESIGNATION] [varchar](150) NOT NULL,
[CAT_LEVEL] [int] )



CREATE TABLE [dbo].[PRODUITS](
[ID_PRODUIT] [int] IDENTITY(1,1) NOT NULL primary key,
[PRD_ID_CATEGORIE] [int] NOT NULL, /* foreign key */
[PRD_DESIGNATION] [varchar](150) NOT NULL,
[PRD_IMAGE] [varchar](150) NULL


the category table is a self joined table with the CAT_ID_PARENT field

i have used this recursive query to display all childrens for a given category

WITH tree (data, id)
AS (SELECT CAT_DESIGNATION, ID_CATEGORIE

FROM CATEGORIES s
WHERE ID_CATEGORIE = 8
UNION ALL
SELECT CAT_DESIGNATION, ID_CATEGORIE
FROM CATEGORIES V
INNER JOIN tree t
ON t.id = V.CAT_ID_PARENT)
SELECT data , id
FROM tree



now i want all the products from the produits table that their PRD_ID_CATEGORIE IN the previous query

btw : is it possible to add a parameter to the first query , i got an error when trying :


WITH tree (data, id)
AS (SELECT CAT_DESIGNATION, ID_CATEGORIE

FROM CATEGORIES s
WHERE ID_CATEGORIE = @c
UNION ALL
SELECT CAT_DESIGNATION, ID_CATEGORIE
FROM CATEGORIES V
INNER JOIN tree t
ON t.id = V.CAT_ID_PARENT)
SELECT data , id
FROM tree



thanks and good day
Post #1419877
Posted Thursday, February 14, 2013 2:16 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, April 12, 2013 3:51 AM
Points: 5,075, Visits: 4,831
Just need to do an inner join from the tree CTE to the Produits table

CREATE TABLE [dbo].[CATEGORIES](
[ID_CATEGORIE] [int] IDENTITY(1,1) NOT NULL primary key,
[CAT_ID_PARENT] [int] NULL,
[CAT_DESIGNATION] [varchar](150) NOT NULL,
[CAT_LEVEL] [int] )

CREATE TABLE [dbo].[PRODUITS](
[ID_PRODUIT] [int] IDENTITY(1,1) NOT NULL primary key,
[PRD_ID_CATEGORIE] [int] NOT NULL, /* foreign key */
[PRD_DESIGNATION] [varchar](150) NOT NULL,
[PRD_IMAGE] [varchar](150) NULL)

INSERT INTO CATEGORIES VALUES
(null,'parent',1),--1
(1,'child1',2),--2
(1,'child2',2),--3
(2,'grandchild1',3),--4
(2,'grandchild2',3),--5
(3,'grandchild3',3),--6
(3,'grandchild4',3)--7

INSERT INTO PRODUITS VALUES
(1,'parent','parentimage'),
(2,'child1','child1image'),
(3,'child2','child2image'),
(4,'grandchild1','grandchild1image'),
(5,'grandchild2','grandchild2image'),
(6,'grandchild3','grandchild3image'),
(7,'grandchild4','grandchild4image')

DECLARE @ID INT = 1

;WITH tree (data, id)
AS (SELECT CAT_DESIGNATION, ID_CATEGORIE
FROM CATEGORIES s
WHERE ID_CATEGORIE = @ID
UNION ALL
SELECT CAT_DESIGNATION, ID_CATEGORIE
FROM CATEGORIES V
INNER JOIN tree t
ON t.id = V.CAT_ID_PARENT)
SELECT p.*
FROM PRODUITS p
inner join tree t on
p.PRD_ID_CATEGORIE = t.id





Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger

Post #1419881
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse