Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

T-SQL Stored Procedure Question?

Posted on 2011-03-08
16
Medium Priority
?
302 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 668 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 1332 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

916 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