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=@Ingredient Name
end
else
begin
INSERT INTO RecipeIngredients(RecipeID , quantity,IngredientName, UnitofMeasurement)
VALUES (@RecipeID, @Quantity, @IngredientName,@UnitofMea surement)
end
select @Output = STUFF( (select CONVERT(VARCHAR,quantity) + ' ' + UnitofMeasurement + ' ' + IngredientName + ''
from RecipeIngredients
where RecipeID = @RecipeID FOR XML PATH ('')) ,1,0, '')
END
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=@Ingredient
end
else
begin
INSERT INTO RecipeIngredients(RecipeID
VALUES (@RecipeID, @Quantity, @IngredientName,@UnitofMea
end
select @Output = STUFF( (select CONVERT(VARCHAR,quantity) + ' ' + UnitofMeasurement + ' ' + IngredientName + ''
from RecipeIngredients
where RecipeID = @RecipeID FOR XML PATH ('')) ,1,0, '')
END
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>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.
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.
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, '')
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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,CHARI
case
when charindex(' ', SUBSTRING(RecipeName,CHARI
else charindex(' ', SUBSTRING(RecipeName,CHARI
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
ASKER
Ok, But could you explain the steps so I get a clear picture of it?
thanks
thanks
True, but it's only easy if you know the answer :)
ASKER
Any ideas?
Still looking forward to your replies?
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
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
ASKER
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
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
Lee