Showing posts with label alter. Show all posts
Showing posts with label alter. Show all posts

Friday, March 30, 2012

how to make this stored procedure more performant

ALTER PROCEDURE dbo.sp_AddProdutionPlanning

(

@.ProductionPlanning_Product uniqueidentifier,

@.ProductionPlanning_Date datetime,

@.ProductionPlanning_Quantity int

)

AS

Begin TRAN

Declare @.NewID uniqueidentifier;

Declare @.NewItemID uniqueidentifier;

Declare @.NewItemBatchID uniqueidentifier;

Declare @.Ingr uniqueidentifier;

Declare @.IngrQty int;

Declare @.Batch uniqueidentifier;

Declare @.BtchQty int;

Declare @.BtchConsumed int;

Declare @.MixQty int;

Declare @.QtyLeft int;

Set @.NewID= newid();

INSERT INTO ProductionPlanning

(ProductionPlanning_ID, ProductionPlanning_Product, ProductionPlanning_Date, ProductionPlanning_Quantity, ProductionPlanning_Created,

ProductionPlanning_PreviousUpdate, ProductionPlanning_LastUpdated, ProductionPlanning_Deleted)

VALUES (@.NewID,@.ProductionPlanning_Product,@.ProductionPlanning_Date,@.ProductionPlanning_Quantity,getdate(),getdate(),getdate(),0)

Select NULL mykey, ProductRecipe_ID, ProductRecipe_Ingredient, ProductRecipe_Quantity into #tblRecipe From ProductRecipe Where ((ProductRecipe_Product = @.ProductionPlanning_Product) And (ProductRecipe_Deleted = 0))

Set rowcount 1;

Update #tblRecipe Set mykey=1;

While @.@.Rowcount <>0

Begin

Set rowcount 1;

Select @.Ingr=ProductRecipe_Ingredient,@.IngrQty = ProductRecipe_Quantity From #tblRecipe;

Delete from #tblRecipe where mykey=1;

Set @.MixQty = (@.IngrQty * @.ProductionPlanning_Quantity);

Set @.NewItemID = newid();

INSERT INTO ProductionPlanningItem

(ProductionPlanningItem_ID, ProductionPlanningItem_Ingredient, ProductionPlanningItem_Planning, ProductionPlanningItem_Quantity)

VALUES (@.NewItemID,@.Ingr,@.NewID,@.MixQty)

Select NULL mykey,ProductIngredientBatch_ID, ProductIngredientBatch_Quantity, ProductIngredientBatch_Consumed Into #tblBatch From ProductIngredientBatch Where ProductIngredientBatch_Ingredient = @.Ingr And ProductIngredientBatch_Deleted = 0;

if (@.@.ERROR <> 0) Goto ERR_HANDLER

Set rowcount 1;

Update #tblBatch set mykey=1;

While @.@.rowcount <>0

Begin

set rowcount 1;

Select @.Batch=ProductIngredientBatch_ID, @.BtchQty=ProductIngredientBatch_Quantity, @.BtchConsumed = ProductIngredientBatch_Consumed From #tblBatch

delete from #tblBatch where mykey=1

Set @.NewItemBatchID = newid();

Set @.QtyLeft = @.BtchQty - @.BtchConsumed;

if(@.QtyLeft > @.MixQty)

Begin

INSERT INTO ProductionPlanningItemBatch

(ProductionPlanningItemBatch_ID, ProductionPlanningItemBatch_Item, ProductionPlanningItemBatch_Batch, ProductionPlanningItemBatch_Quantity)

VALUES (@.NewItemBatchID,@.NewItemID,@.Batch,@.MixQty)

if (@.@.ERROR <> 0) Goto ERR_HANDLER

Update ProductIngredientBatch Set ProductIngredientBatch_Consumed = (@.BtchConsumed + @.MixQty) Where ProductIngredientBatch_ID = @.Batch

if (@.@.ERROR <> 0) Goto ERR_HANDLER

End

else

Begin

INSERT INTO ProductionPlanningItemBatch

(ProductionPlanningItemBatch_ID, ProductionPlanningItemBatch_Item, ProductionPlanningItemBatch_Batch, ProductionPlanningItemBatch_Quantity)

VALUES (@.NewItemBatchID,@.NewItemID,@.Batch,@.QtyLeft)

if (@.@.ERROR <> 0) Goto ERR_HANDLER

Update ProductIngredientBatch Set ProductIngredientBatch_Consumed = 0 Where ProductIngredientBatch_ID = @.Batch

if (@.@.ERROR <> 0) Goto ERR_HANDLER

End

if (@.@.ERROR <> 0) Goto ERR_HANDLER

Print @.Batch;

UPDATE ProductIngredient

SET ProductIngredient_Consumed = ProductIngredient_Consumed + @.MixQty

WHERE (ProductIngredient_ID = @.Ingr)

if (@.@.ERROR <> 0) Goto ERR_HANDLER

set rowcount 1;

update #tblBatch set mykey=1

End

Drop table #tblBatch;

Set rowcount 1;

update #tblRecipe set mykey=1

End

Commit Tran

RETURN 0

ERR_HANDLER:

Print 'An error occured';

ROLLBACK TRAN

RETURN 1

Given that nobody has taken a stab at this, I will give you some pointers. Yes, it is possible to rewrite the SP using set-based logic. Start with correlated subqueries that can maintain the running total. Using CTEs in SQL Server 2005 also is an option.

|||And perhaps you could use TRY / CATCH blocks to obtain a better error handling

Wednesday, March 7, 2012

How to let a SP be shorter?

Hi,
The following is one of our stored procedures,
is it possible to make it shorter?
Thanks for help.
Jason
ALTER PROCEDURE UpdateCustomerBtoRoot
AS
SET nocount on
UPDATE Customer SET
CustCName = (SELECT CustCName FROM CustomerB WHERE
CustomerB.CustNo=Customer.CustNo),
CustEName =(SELECT CustEName FROM CustomerB WHERE
CustomerB.CustNo=Customer.CustNo),
AbbrName=(SELECT AbbrName FROM CustomerB WHERE
CustomerB.CustNo=Customer.CustNo),
ZipCode =(SELECT ZipCode FROM CustomerB WHERE
CustomerB.CustNo=Customer.CustNo),
Addr = (SELECT Addr FROM CustomerB WHERE
CustomerB.CustNo=Customer.CustNo),
EZipCode = (SELECT EZipCode FROM CustomerB WHERE
CustomerB.CustNo=Customer.CustNo),
BillZipCode = (SELECT BillZipCode FROM CustomerB WHERE
CustomerB.CustNo=Customer.CustNo),
BillAddr = (SELECT BillAddr FROM CustomerB WHERE
CustomerB.CustNo=Customer.CustNo),
Phone = (SELECT Phone FROM CustomerB WHERE
CustomerB.CustNo=Customer.CustNo),
Fax = (SELECT Fax FROM CustomerB WHERE
CustomerB.CustNo=Customer.CustNo),
Owner = (SELECT Owner FROM CustomerB WHERE
CustomerB.CustNo=Customer.CustNo),
OwnerTitle = (SELECT OwnerTitle FROM CustomerB WHERE
CustomerB.CustNo=Customer.CustNo),
HomePage = (SELECT HomePage FROM CustomerB WHERE
CustomerB.CustNo=Customer.CustNo),
FirstDate = (SELECT FirstDate FROM CustomerB WHERE
CustomerB.CustNo=Customer.CustNo),
DateUpDate = (SELECT DateUpDate FROM CustomerB WHERE
CustomerB.CustNo=Customer.CustNo),
Area = (SELECT Area FROM CustomerB WHERE
CustomerB.CustNo=Customer.CustNo),
WeightMeasure =(SELECT WeightMeasure FROM CustomerB WHERE
CustomerB.CustNo=Customer.CustNo)
WHERE EXISTS (SELECT * FROM
CustomerB WHERE CustomerB.CustNo=Customer.CustNo)
GOHi, Jason
Use something like this:
UPDATE Customer
SET CustCName=B.CustCName,
CustEName=B.CustEName,
AbbrName=B.AbbrName,
[...]
FROM Customer as C INNER JOIN CustomerB as B
ON C.CustNo=B.CustNo
In order to get the expected results, make sure that the CustNo is a
unique key or a primary key in both tables. Although easier to read,
this syntax is less portable than the original statement (the original
syntax was ANSI standard, but this is proprietary to Microsoft SQL
Server).
Razvan