Solved

T-SQL Stored Procedure Question?

Posted on 2011-03-08
16
295 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
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…

615 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