Solved

T-SQL Stored Procedure Question?

Posted on 2011-03-08
16
292 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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
 
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

Upcoming Webinar: Percona XtraDB Cluster 6/21 10am

Join Percona’s MySQL Practice Manager Kenny Gryp and QA Engineer, Ramesh Sivaraman as they present Percona XtraDB Cluster, Galera Cluster, MySQL Group Replication on Wednesday, June 21, 2017 at 10:00 am PDT / 1:00 pm EDT (UTC-7).

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

728 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