Link to home
Start Free TrialLog in
Avatar of Itudk_2010
Itudk_2010

asked on

T-SQL Stored Procedure Question?

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
ASKER CERTIFIED SOLUTION
Avatar of Lee
Lee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oh, and then it returns the quantity to the calling application.

Lee
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>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.

Avatar of Itudk_2010
Itudk_2010

ASKER

@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, '')

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

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.
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
This should be a different question really.
@lsavidge: I agree that it should be another topic..but 500 pts for such easy challenges is not a big deal
Ok, But could you explain the steps so I get a clear picture of it?

thanks
True, but it's only easy if you know the answer :)
Any ideas?

Still looking forward to your replies?
(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
good
>>This should be a different question really. <<
And it is.  The author asked the same question 2 days ago, see here:
https://www.experts-exchange.com/questions/26866970/T-SQL-query-explaination.html