Solved

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

Posted on 2011-03-04
8
556 Views
Last Modified: 2012-05-11
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
Comment
Question by:Itudk_2010
  • 4
  • 2
  • 2
8 Comments
 
LVL 40

Accepted Solution

by:
Sharath earned 250 total points
ID: 35040555
Is this an homework question? If not provide some sample data from your tables.
0
 

Author Comment

by:Itudk_2010
ID: 35040749
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
 
LVL 40

Assisted Solution

by:Sharath
Sharath earned 250 total points
ID: 35040967
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
 

Author Comment

by:Itudk_2010
ID: 35041284
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 15

Assisted Solution

by:derekkromm
derekkromm earned 250 total points
ID: 35041495
(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
 

Author Comment

by:Itudk_2010
ID: 35041528
@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
 
LVL 15

Assisted Solution

by:derekkromm
derekkromm earned 250 total points
ID: 35041539
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
 

Author Closing Comment

by:Itudk_2010
ID: 35041587
No comments
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now