• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 571
  • Last Modified:

Urgent help required for T-SQL queries and a Stored Procedure

Hi all,

I have some knowledge of database design and SQL but I have some problems completing some queries and a Stored Procedure. I have the following four tables for Recipe Database.


CREATE TABLE Recipe
(
 ReceipeID        INT IDENTITY NOT NULL,
 ReceipeName      VARCHAR(100) NOT NULL,
 ReceipeDate      DATETIME,
 ReceipeAuthorID  INT NOT NULL,

)

CREATE TABLE RecipeAuthor
(
RecipeAuthorID INT IDENTITY NOT NULL,
AuthorName      VARCHAR(50) NOT NULL,
Reference       VARCHAR(3000)
)

CREATE TABLE RecipeIngredients
(
 ReciepteIngredientID  INT IDENTITY NOT NULL,
 ReceipeID             INT NOT NULL,
 Quantity              Decimal,
 UnitofMeasurement     VARCHAR(1000),
 IngredientName        VARCHAR(1000)


)

CREATE TABLE RecipeDirection
(
RecipeDirectionID  INT IDENTITY NOT NULL,
RecipeID            INT NOT NULL,
StepProcess          VARCHAR(3000)

)


I have populated them with data and I have completed some queries. But I am unable to complete the following questions.

(1) Here are the questions:

What are the names of the three most used ingredients by quantity in all recipes authored by XYZ?
What is the name of all recipes that do not use egg as an ingredient?
What is the second word of each recipe title?


(2) Stored Procedure Question on the same database above.

Write a stored procedure that will add an ingredient to a recipe. The procedure should accept at a minimum the recipe name or ID, ingredient name or ID, ingredient quantity, and ingredient unit.

If the ingredient already exists, update the quantity; an error should occur if the new ingredient and quantity are the same. A return code of 0 should indicate success and -1 if an error was encountered.

Make use of an output parameter that contains a semi-colon separated list of ingredient name and units. The value should be structured similar to the following:

2 cups white sugar;1/2 cup butter;1/2 cup milk;3 tablespoons cocoa powder

Include at the bottom of the same file, SQL code that will execute the procedure and print or select the error code and ingredient list (output parameter).
0
Itudk_2010
Asked:
Itudk_2010
  • 4
  • 2
  • 2
4 Solutions
 
SharathData EngineerCommented:
Is this an homework question? If not provide some sample data from your tables.
0
 
Itudk_2010Author Commented:
Hi Sharath_123,

Thanks for your reply. It is a project not a home work. Could you please help in this regard. Following is the data


Receipe  Table                                  
ReceipeID      Name           Date             AuthorID     createddate      
1                   Sandwich        1/1/2011      1               1/1/2011      
2                   Ham Burger      1/1/2011      2               1/1/2011      
                                   
                                   
                     
RecipeAuthor  Table                                  
AuthorID     AuthorName                            
1                   irene                              
2                    walsh                              
                                   
                             
ReciepeIngredients Table                                  
ReciepeIngredientsID     ReceipeID      Ingredeint Name      UnitofMeasurement      quantity          
1                                          1                Bread                          Cup                               2          
1                                          1                greens                       Tablespoon                     3        
2                                    
                                   
ReceipeDirection                                    
ReceipeStepsID      ReceipeID      StepProcess                
1                                   1                wash the greens                
2                                   1                  toast the bread                
3                                   1                   apply cheese on bread            

I really appreciate your help. Looking forward to your reply.

Best regards.  
0
 
SharathData EngineerCommented:
You have asked lot of questions in one post. Please try to implement the queries and if you need help, we will help you.
I have answered the first part of your questions.

>> What are the names of the three most used ingredients by quantity in all recipes authored by XYZ?
SELECT TOP 3 ri.IngredientName 
    FROM RecipeAuthor ra 
         JOIN Recipe r 
           ON ra.RecipeAuthorID = r.RecipeAuthorID 
         JOIN RecipeIngredients ri 
           ON ri.RecipeID = r.RecipeID 
   WHERE ra.AuthorName = 'irene' -- replace irene with any other name 
ORDER BY ri.Quantity DESC

Open in new window

>> What is the name of all recipes that do not use egg as an ingredient?
SELECT r.RecipeName 
  FROM Recipe r 
       LEFT JOIN RecipeIngredients ri 
         ON r.RecipeID = ri.RecipeID 
            AND ri.IngredientName <> 'egg' 
 WHERE ri.RecipeID IS NULL

Open in new window

>> What is the second word of each recipe title?
SELECT CASE 
         WHEN CHARINDEX(' ',RecipeName) <> 0 THEN SUBSTRING(RecipeName,CHARINDEX(' ',RecipeName) + 1,
                                                            LEN(RecipeName)) 
         ELSE 'No Second Word' 
       END Second_Word 
  FROM Recipe

Open in new window

0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
Itudk_2010Author Commented:
Hi Sharath_123,

The first answer is correct But the following two questions give wrong output. The first one returns no results as it should return bread, greens etc which is in the table above except egg.

(1) What is the name of all recipes that do not use egg as an ingredient?

 SELECT r.RecipeName
  FROM Recipe r
       LEFT JOIN RecipeIngredients ri
         ON r.RecipeID = ri.RecipeID
            AND ri.IngredientName <> 'egg'
 WHERE ri.RecipeID IS NULL

(2) What is the second word of each recipe title?

 SELECT CASE
         WHEN CHARINDEX(' ',RecipeName) <> 0 THEN SUBSTRING(RecipeName,CHARINDEX(' ',RecipeName) + 1,
                                                            LEN(RecipeName))
         ELSE 'No Second Word'
       END Second_Word
  FROM Recipe


This one returns the second and third and fourth words so on. It should return only the second word but not the remaining words. Is there any idea?

I really appreciate your help. Looking forward to your reply.
0
 
derekkrommCommented:
(1) What is the name of all recipes that do not use egg as an ingredient?

select RecipeName from Recipe where RecipeID not in  (select RecipeID from RecipeIngredients where IngredientName = 'egg')


(2) What is the second word of each recipe title?
SELECT
      CASE
         WHEN CHARINDEX(' ',RecipeName) <> 0 THEN
                  SUBSTRING(RecipeName,CHARINDEX(' ',RecipeName) + 1,
                        case
                              when charindex(' ', SUBSTRING(RecipeName,CHARINDEX(' ',RecipeName) + 1, LEN(RecipeName))) = 0 then LEN(RecipeName)
                              else charindex(' ', SUBSTRING(RecipeName,CHARINDEX(' ',RecipeName) + 1, LEN(RecipeName)))
                        end)
         ELSE
                  'No Second Word'
       END Second_Word
FROM Recipe
0
 
Itudk_2010Author Commented:
@derekkromm,

Thanks a lot for your help. Your answer for question 2 is correct. But question 1 is still not working?

select RecipeName from Recipe where RecipeID not in  (select RecipeID from RecipeIngredients where IngredientName = 'egg')

any other ideas?

0
 
derekkrommCommented:
That query definitely works. See below code for example.

Now, its not going to detect "eggs" or "egg----", because that does not equal "egg". Nor should it, because then "egg noodles" would be detected as "egg", which would be undesirable.

declare @recipe table (
	RecipeID int identity,
	RecipeName varchar(100))
	
declare @recipeingredients table (
	RecipeIngredientsID int identity,
	RecipeID int,
	IngredientName varchar(100))
	
insert into @recipe(RecipeName) 
select 'Recipe 1' union select 'Recipe 2' union select 'Recipe 3'

insert into @recipeingredients(RecipeID, IngredientName)
select 1,'egg' union select 1,'flour' union select 1,'rice'
union select 2,'water' union select 2,'rice'
union select 3,'flour' union select 3,'milk'

select recipename from @recipe where RecipeID not in (select RecipeID from @recipeingredients where IngredientName = 'egg')

Open in new window

0
 
Itudk_2010Author Commented:
No comments
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now