Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

T-SQL Stored Procedure Question?

Posted on 2011-03-08
16
Medium Priority
?
301 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 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

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.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

722 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