/*
Hi, I have the folowing T-SQL problem.
Here are the tables that I use for my query.
The first table is Categories. In this table there are the
names of attributes that are used for classification of Products.
*/
CREATE TABLE [Categories] (
[CategoryID] [nvarchar] (2) COLLATE Latin1_General_CI_AS NOT NULL ,
[CategoryName] [nvarchar] (15) COLLATE Latin1_General_CI_AS NOT NULL ,
CONSTRAINT [PK_Categories] PRIMARY KEY CLUSTERED
(
[CategoryID]
) ON [PRIMARY]
) ON [PRIMARY]
GO
/*
The second is CategoryValues in which contains the values for every categories.
Every category can have more then one values with their descriptions.
*/
CREATE TABLE [CategoryValues]
(
[CategoryValues] [nvarchar] (10) NOT NULL ,
[CategoryID] [nvarchar] (2) COLLATE Latin1_General_CI_AS NOT NULL ,
[CategoryDescryption] [nvarchar] (50) COLLATE Latin1_General_CI_AS NOT NULL ,
CONSTRAINT [PK_CategoryValues] PRIMARY KEY CLUSTERED
(
[CategoryValues],
[CategoryID]
) ON [PRIMARY] ,
CONSTRAINT [FK_CategoryValues_Categories] FOREIGN KEY
(
[CategoryID]
) REFERENCES [Categories] (
[CategoryID]
)
) ON [PRIMARY]
GO
/*
The next table is Products.
*/
CREATE TABLE [Products]
(
[ProductID] [nvarchar] (10) COLLATE Latin1_General_CI_AS NOT NULL ,
[ProductName] [nvarchar] (50) COLLATE Latin1_General_CI_AS NOT NULL ,
CONSTRAINT [PK_Products] PRIMARY KEY CLUSTERED
(
[ProductID]
) ON [PRIMARY]
) ON [PRIMARY]
GO
/*
And finally ProductsCategory. It connects products with categories.
*/
CREATE TABLE [ProductsCategory]
(
[ProductID] [nvarchar] (10) COLLATE Latin1_General_CI_AS NOT NULL ,
[CategoryID] [nvarchar] (2) COLLATE Latin1_General_CI_AS NOT NULL ,
[CategoryValues] [nvarchar] (10) COLLATE Latin1_General_CI_AS NOT NULL ,
CONSTRAINT [PK_ProductsCategory] PRIMARY KEY CLUSTERED
(
[ProductID],
[CategoryID],
[CategoryValues]
) ON [PRIMARY] ,
CONSTRAINT [FK_ProductsCategory_CategoryValues] FOREIGN KEY
(
[CategoryValues],
[CategoryID]
) REFERENCES [CategoryValues] (
[CategoryValues],
[CategoryID]
),
CONSTRAINT [FK_ProductsCategory_Products] FOREIGN KEY
(
[ProductID]
) REFERENCES [Products] (
[ProductID]
)
) ON [PRIMARY]
GO
/*
I hope that I'll succeed to explain what i realy need. The real problem is more
complicated because there are a few tables more, something like order and
orderdetails in NorthWind, from which I have to calculate some cumulative results.
I need to see all from products where products belong to certain category.
This is not realy hard. So I wrote this query:
*/
SELECT *
FROM Products p
WHERE EXISTS
(
SELECT p1.productid
FROM Products p1 JOIN ProductsCategory pc ON p.productid=pc.productid
JOIN CategoryValues cv ON pc.CategoryValues = cv.CategoryValues
AND pc.CategoryId = cv.CategroryID
WHERE cv.CategoryValues ='01' and cv.CategoryId = '14'
AND p1.productid = p.productid
)
/*
But real problem is that I have to see which products belong for two or more categories for
which CategoryValues are some variables like categories too.
And I do not know how many categories user will send.
So if he send two categories I will add one more exists so the Where clause becomes:
*/
WHERE EXISTS
(
SELECT p1.productid
FROM Products p1 JOIN ProductsCategory pc ON p.productid=pc.productid
JOIN CategoryValues cv ON pc.CategoryValues = cv.CategoryValues
AND pc.CategoryId = cv.CategroryID
WHERE cv.CategoryValues ='01' and cv.CategoryId = '14'
AND p1.productid = p.productid
) and
EXISTS
(
SELECT p1.productid
FROM Products p1 JOIN ProductsCategory pc ON p.productid=pc.productid
JOIN CategoryValues cv ON pc.CategoryValues = cv.CategoryValues
AND pc.CategoryId = cv.CategroryID
WHERE cv.CategoryValues ='03' and cv.CategoryId = '1162'
AND p1.productid = p.productid
)
/*
If the user send one more category I will have to add one more exists...
All this would have to be in one stored proc which user will call from VB6.
So I do not know how many parameters user will send.
Is there any way to pack all of this in one stored proc and not to use dynamic sql
or if I have to use dynamic sql can anyone explain me how.
If there is no way, I think
that I have to generate query from
vb and to send to sql server.
Thanks very much.
*/