Solved

Stored Procedure

Posted on 2004-04-15
9
225 Views
Last Modified: 2010-04-05
I made a StoredProcedure to do an update on a table.:

ALTER PROCEDURE "CLIENTS"
(
  "CODE" INTEGER,
  "PURCHASE" DOUBLE PRECISION
)
AS
  BEGIN
    update CLIENTS
    set TOTAL = TOTAL + :PURCHASE
    where CLIENT_CODE = :CODE;
  END
 ^

Do I have to put a commit after the update or something ? Because when I execute the StoredProcedure from my application, then right after try to do an Edit on that table it gives me an Update Deadlock ! What´s wrong ??
0
Comment
Question by:fabyola
9 Comments
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10832298

   commit the transaction before execute the stored procedure ...
0
 
LVL 1

Expert Comment

by:psycho_cat_69
ID: 10832780
Try this

ALTER PROCEDURE "CLIENTS"
(
  "CODE" INTEGER,
  "PURCHASE" DOUBLE PRECISION
)
AS
    update CLIENTS
    set TOTAL = TOTAL + :PURCHASE
    where CLIENT_CODE = :CODE;
GO
0
 

Author Comment

by:fabyola
ID: 10833454
But do I have to do a commit after or does it do it for me automaticlly ?
0
 
LVL 12

Accepted Solution

by:
esoftbg earned 50 total points
ID: 10837844
InterBase syntax:

ALTER PROCEDURE CLIENT_UPDATE
(
  CODE INTEGER,
  PURCHASE DOUBLE PRECISION
)
AS
  DECLARE VARIABLE DTOTAL DOUBLE PRECISION;
BEGIN
  select TOTAL from CLIENTS where CLIENT_CODE = :CODE into :DTOTAL;
  update CLIENTS set TOTAL = :DTOTAL + :PURCHASE where CLIENT_CODE = :CODE;
  SUSPEND;
END

//................

Delphi syntax:  IBQ: TIBQuery; IBTR: IBTransaction; CLIENT_UPDATE_: TIBStoredProc;

procedure TFormCLIENTS.Client_Update(Code: Integer; Purchase: Double);
var
  B:      Boolean;
  S:      string;
begin
  B := False;
  S := IBQ.FieldByName('CLIENT_CODE').AsString;
  if not IBTR.Active then
    IBTR.StartTransaction;
  try
    try
      CLIENT_UPDATE_.ParamByName('CODE').AsInteger := Code;
      CLIENT_UPDATE_.ParamByName('PURCHASE').AsFloat := Purchase;
      CLIENT_UPDATE_.ExecProc;
      B := True;
    except
      B := False;
    end;
  finally
    if IBTR.Active then
    begin
      if B then
      begin
        IBTR.Commit;
        IBQ.Active := True;
        IBQ.Locate('CLIENT_CODE', S, []);
      end
      else
        IBTR.Rollback;
    end;
  end;
end;
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 12

Expert Comment

by:esoftbg
ID: 10837863
>But do I have to do a commit after or does it do it for me automaticlly ?
TIBTransaction must be Activated before execution of a TIBStoredProc, and must be Commited after its execution.
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 10839869
A little bit improvement of above code:
      if B then
      begin
        IBTR.Commit;
        IBQ.Active := False; // It is better to be here in case IBQ and CLIENT_UPDATE_ use different TIBTransaction components
        IBQ.Active := True;
        IBQ.Locate('CLIENT_CODE', S, []);
      end
      else
        IBTR.Rollback;
0
 

Author Comment

by:fabyola
ID: 10858401
Ok but I do a Commit right after I execute the StoredProcedure
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 10859821
right-click mouse button over the transaction componens and open the Transaction Editor (or double click). Select Read Commited. This will solve your problem.
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 10863353
if IBQ is a TIBTable and IBTQ is it's TIBTransaction, you must use the events:

procedure TFormCLIENTS.IBQBeforePost(DataSet: TDataSet);
begin
  if not IBTQ.Active then
    IBTQ.StartTransaction;
end;

procedure TFormCLIENTS.IBQAfterPost(DataSet: TDataSet);
begin
  if IBTQ.Active then
    IBTQ.CommitRetaining;
end;
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now