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.
| Author |
Topic |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2002-05-30 : 08:58:31
|
| Jordan writes "I want to write an SQL query that lets the user specify at least 3 ingredients and returns drink recipes that contain only those ingredients.Drinks------Drink_id (pk)Drink_nameDrink_descriptionDrink_typeIngredients-----------Ingredient_id (pk)Ingredient_nameAmounts-------Amount_id (pk)Amount_nameIngredients_track-----------------Id (pk)Drink_idIngredient_idAmount_id" |
|
|
Page47
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2002-05-30 : 09:13:22
|
CREATE FUNCTION ufn_ParseArray( @array varchar(8000), @separator varchar(10)) RETURNS @Parsed TABLE( ArrayID INT IDENTITY(1,1), Value VARCHAR(1000))ASBEGINDECLARE @separator_position INTDECLARE @array_value VARCHAR(1000) -- this holds each array value as it is returned--Special caseIF DATALENGTH(ISNULL(@array,'')) = 0BEGIN INSERT INTO @Parsed (Value) VALUES (NULL) RETURNENDSET @array = @array + @separator-- Loop through the string searching for separtor charactersWHILE PATINDEX('%' + @separator + '%' , @array) <> 0 BEGIN SELECT @separator_position = PATINDEX('%' + @separator + '%' , @array) SELECT @array_value = LEFT(@array, @separator_position - 1) INSERT INTO @Parsed (Value) VALUES (@array_value) SELECT @array = STUFF(@array, 1, @separator_position + datalength(@separator)-1, '')ENDRETURNENDGOcreate proc usp_getdrinks @inglist varchar(8000) -- comma delim list of ingrediantsascreate table #inglist ( ingid int, ing varchar(1000))insert #inglistselect * from dbo.ufn_parsearray(@inglist,',')--insert error handling for < 3 ingselect d.drink_namefrom drinks d inner join ingredients_track it on d.drink_id = it.drink_id inner join ingredients i on it.ingredient_id = i.ingredient_id inner join #inglist il on i.ingredient_name = il.inggoexec usp_getdrinks 'grey goose vodka, vermoth, olive'go<O> |
 |
|
|
MakeYourDaddyProud
184 Posts |
Posted - 2002-05-30 : 09:47:21
|
| Assuming... create table #Drinks (Drink_id int primary key, Drink_name varchar(20) not null, Drink_description varchar(100) not null, Drink_type int null )gocreate table #Ingredients (Ingredient_id int primary key, Ingredient_name varchar(20) not null )gocreate table #Amounts (Amount_id int primary key, Amount_name varchar(20) )gocreate table #Ingredients_track -- Or RECIPES([Id] int primary key, Drink_id int not null, Ingredient_id int not null, Amount_id int not null ) -- and hence set up the foreign key relationships if requiredgo-- here is the solutioncreate procedure #p_list_recipes @ingredient_name1 varchar(20), @ingredient_name2 varchar(20), @ingredient_name3 varchar(20)asselect D.*from #Drinks Djoin (select IT.drink_id, IT.[id], count(*) AS TOTAL_INGREDIENTS from #Ingredients_track IT join #Ingredients I ON I.Ingredient_ID = IT.Ingredient_ID and I.Ingredient_name IN (@ingredient_name1,@ingredient_name2,@ingredient_name3) group by IT.[id], IT.Drink_id having count(*) = 3) ONLY3 on ONLY3.Drink_id = D.Drink_ID-- you will need to remove the # symbols from the table names and sp name-- other than that should be OKDaniel Small CEOwww.danielsmall.com |
 |
|
|
MakeYourDaddyProud
184 Posts |
Posted - 2002-05-30 : 09:47:56
|
| Assuming... create table #Drinks (Drink_id int primary key, Drink_name varchar(20) not null, Drink_description varchar(100) not null, Drink_type int null )gocreate table #Ingredients (Ingredient_id int primary key, Ingredient_name varchar(20) not null )gocreate table #Amounts (Amount_id int primary key, Amount_name varchar(20) )gocreate table #Ingredients_track -- Or RECIPES([Id] int primary key, Drink_id int not null, Ingredient_id int not null, Amount_id int not null ) -- and hence set up the foreign key relationships if requiredgo-- here is the solutioncreate procedure #p_list_recipes @ingredient_name1 varchar(20), @ingredient_name2 varchar(20), @ingredient_name3 varchar(20)asselect D.*from #Drinks Djoin (select IT.drink_id, IT.[id], count(*) AS TOTAL_INGREDIENTS from #Ingredients_track IT join #Ingredients I ON I.Ingredient_ID = IT.Ingredient_ID and I.Ingredient_name IN (@ingredient_name1,@ingredient_name2,@ingredient_name3) group by IT.[id], IT.Drink_id having count(*) = 3) ONLY3 on ONLY3.Drink_id = D.Drink_ID-- you will need to remove the # symbols from the table names and sp name-- other than that should be OKDaniel Small CEOwww.danielsmall.com |
 |
|
|
|
|
|
|
|