This is how the stored procedure would be structured:
[dbo].[getSp] (
@Color varchar(10) = Null,
@Finish varchar(10) = Null,
@Height varchar(10) = Null,
@Type varchar(10) = Null,
@Trim varchar(10) = Null,
@Width varchar(10) = Null
)
This is how the data looks before searched..This data is incomplete, but as an example to get an idea. Every combination would be in here..
Color | Finish | Height | Type | Trim | Width |
Blue | Shiny | Tall | Toaster | Normal | 3.5 |
(NULL) | (NULL) | (NULL) | Toaster | (NULL) | 3.5 |
(NULL) | (NULL) | Tall | Toaster | Deco | 4 |
(NULL) | Shiny | Tall | Toaster | Deco | 4 |
Blue | Shiny | (NULL) | Toaster | Deco | 4 |
(NULL) | Shiny | Tall | Toaster | Deco | 4 |
(NULL) | Satin | Tall | Toaster | Deco | 4 |
Red | (NULL) | Tall | Toaster | Deco | 4 |
The SELECT statement would have Type as the primary lookup field, so it would look something like this:
SELECT * FROM TABLE WHERE Type = @Type And
(
..... Confused about this part,
)
But basically, I would like to run a stored procedure to only include those combinations, which are rows found in the table when called with only a part of the params, like this:
[getSp]
@Color='Blue',
@Finish='Shiny',
@Type='toaster'
And the sp would ultimately return this (every combination)..
1. Blue Shiny Tall Toaster
2. Blue Shiny Toaster
3. Blue Tall Toaster
4. Blue Toaster
5. Tall Toaster
6. Shiny Toaster
7. Shiny Tall Toaster
So I am confused about how to structure the Where statement in the stored procedure to return every combination for ONLY those which you include in the stored procedure params, but the remaining params (like Width and Trim would be left out of the results and searching, would and when passed as NULL by default.
I’ve tried this without any luck…
SELECT * FROM TABLE WHERE
Type = @Type AND
(
COLOR = COALESCE(@Color,COLOR) AND...
... for each other param
)
I’ve tried this without any luck…
SELECT * FROM TABLE WHERE
Type = @Type AND
(
(Color = @Color OR COALESCE(@Color,COLOR) IS NULL) OR...
... for each other param
)
I’ve tried building the sql statement, but have failed as well.. Any suggestions would be helpful.
This should meet your needs, if you provide a param then it uses it to filter otherwise it won’t: