Solved

T-SQL Stored Procedure Question?

Posted on 2011-03-08
16
283 Views
Last Modified: 2012-06-21
Hi all,

I have some problem understanding the following stored procedure. Could you guys explain in detail what the following stored procedure does?


CREATE PROCEDURE Test_StoredProcedure
(
@RecipeID int,            ---- recipe ID
@Quantity decimal,        -----ingredient quantity
@IngredientName varchar(1000), ----- ingredient name
@UnitofMeasurement varchar(1000), ----ingredient unit
@Output varchar(max) output
)
AS
BEGIN

if exists (select 1 from RecipeIngredients where RecipeID = @RecipeID and IngredientName = @IngredientName and
quantity = @Quantity)
 
begin
     return -1
end

else if exists (select 1 from RecipeIngredients where RecipeID = @RecipeID and IngredientName = @IngredientName)

begin
     update RecipeIngredients set quantity = @Quantity where RecipeID = @RecipeID and IngredientName=@IngredientName
     
end

else

begin
      INSERT INTO RecipeIngredients(RecipeID, quantity,IngredientName, UnitofMeasurement)
      VALUES     (@RecipeID, @Quantity, @IngredientName,@UnitofMeasurement)
      
end



select @Output = STUFF( (select CONVERT(VARCHAR,quantity) + ' ' + UnitofMeasurement + ' ' + IngredientName + ''
from RecipeIngredients
where RecipeID = @RecipeID FOR XML PATH ('')) ,1,0, '')

END
0
Comment
Question by:Itudk_2010
  • 5
  • 5
  • 4
  • +1
16 Comments
 
LVL 25

Accepted Solution

by:
Lee Savidge earned 167 total points
ID: 35068830
Firstly it checks to see if the ingredient exists in the RecipeIngredients table with a matched quantity and a name. If so it returns -1 (I don't know why as this will be the calling app that deals with that)

If there is no quantity and it exists it will update the RecipeIngredients table with the new quantity.

If it doesn't exist it will add the ingredient into the table with a quantity.


Simply put, if the ingredient isn't there is will add it and the quantity. If it is there with no quantity it will update it. If it already exists with a quantity it doesn't do anything.

Lee
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 35068837
Oh, and then it returns the quantity to the calling application.

Lee
0
 
LVL 18

Assisted Solution

by:BigSchmuh
BigSchmuh earned 333 total points
ID: 35068870
You call Test_StoredProcedure with a RecipID, an IngredientName, a UnitofMeasurement and a Quantity

The proc update the ingredient quantity (if different from old quantity) for this recipe or add it (if not exists) with the adequate UnitofMeasure

Output var contains a string valued with the updated/inserted "quantity UnitofMeasure" or -1 if no operation was issued
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 35069040
>>Could you guys explain in detail what the following stored procedure does?<<
Please do not take this the wrong way, but if you do not know the answer to this question, perhaps you should not be applying for that job.

0
 

Author Comment

by:Itudk_2010
ID: 35069152
@lsavidge, and @BigSchmuh,

Thanks for your quick replies. Could you guys explain the last part as follows ?

select @Output = STUFF( (select CONVERT(VARCHAR,quantity) + ' ' + UnitofMeasurement + ' ' + IngredientName + ''
from RecipeIngredients
where RecipeID = @RecipeID FOR XML PATH ('')) ,1,0, '')

0
 
LVL 18

Assisted Solution

by:BigSchmuh
BigSchmuh earned 333 total points
ID: 35069183
Output var contains an XML valued with the updated/inserted "quantity UnitofMeasure Ingredient" or -1 if no operation was issued.

FOR XML PATH ('') adds XML tags (and no CR) to the results
0
 

Author Comment

by:Itudk_2010
ID: 35069249

great,

One last question? Could you explain the following query in detail, I have some problems understanding it?

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


I really appreciate your help.
0
 
LVL 18

Expert Comment

by:BigSchmuh
ID: 35069340
Your last SQL returns the eventual second word of the RecipeName string (word are considered separated by a space) or 'No Second Word' if there is only 0 or 1 word in RecipeName string
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 25

Expert Comment

by:Lee Savidge
ID: 35069347
This should be a different question really.
0
 
LVL 18

Expert Comment

by:BigSchmuh
ID: 35069358
@lsavidge: I agree that it should be another topic..but 500 pts for such easy challenges is not a big deal
0
 

Author Comment

by:Itudk_2010
ID: 35069367
Ok, But could you explain the steps so I get a clear picture of it?

thanks
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 35069375
True, but it's only easy if you know the answer :)
0
 

Author Comment

by:Itudk_2010
ID: 35069697
Any ideas?

Still looking forward to your replies?
0
 
LVL 18

Expert Comment

by:BigSchmuh
ID: 35069712
(Hoops...I already answered...)

Your last SQL returns the eventual second word of the RecipeName string (word are considered separated by a space) or 'No Second Word' if there is only 0 or 1 word in RecipeName string
0
 

Author Closing Comment

by:Itudk_2010
ID: 35069729
good
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 35071199
>>This should be a different question really. <<
And it is.  The author asked the same question 2 days ago, see here:
http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SQL-Server-2005/Q_26866970.html
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Read about achieving the basic levels of HRIS security in the workplace.
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…

743 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

11 Experts available now in Live!

Get 1:1 Help Now