Firebird SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesFirebird SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 8th, 2011, 03:20 AM
biju_ps's Avatar
biju_ps biju_ps is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2008
Posts: 75 biju_ps User rank is Corporal (100 - 500 Reputation Level)biju_ps User rank is Corporal (100 - 500 Reputation Level)biju_ps User rank is Corporal (100 - 500 Reputation Level)biju_ps User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 10 h 31 m 7 sec
Reputation Power: 8
How to know the Records affected in a Transaction

Hi All,

Is there any way to determine the tables & rows affected in a transactions in the DATABASE TRANSACTION COMMIT TRIGGER?


Thanks in advance

Bij

Reply With Quote
  #2  
Old January 9th, 2011, 12:21 AM
clivew clivew is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2006
Location: Carlsbad, CA
Posts: 2,045 clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 37 m
Reputation Power: 382
What are you trying to discover?

Theoretically, one transaction could cover hundreds of different SQL statements.

Reply With Quote
  #3  
Old January 9th, 2011, 01:09 AM
biju_ps's Avatar
biju_ps biju_ps is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2008
Posts: 75 biju_ps User rank is Corporal (100 - 500 Reputation Level)biju_ps User rank is Corporal (100 - 500 Reputation Level)biju_ps User rank is Corporal (100 - 500 Reputation Level)biju_ps User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 10 h 31 m 7 sec
Reputation Power: 8
Thanks Clivew

Thanks Clivew,

I have a table of accounts details with field Debit,Credit.
The sum of the debit ,Credit in this table must be equal.

So i want to check whether the debit , credit total is the same on committing the transaction.

thanks

Reply With Quote
  #4  
Old January 9th, 2011, 12:37 PM
clivew clivew is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2006
Location: Carlsbad, CA
Posts: 2,045 clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 37 m
Reputation Power: 382
Well, run a query to check that they match BEFORE you COMMIT.
If they do not match issue a ROLLBACK instead and issue some appropriate warning.

Naturally you will need to make sure that you do not have any sort of "Commit On Post" option set and handle your own COMMIT and ROLLBACK instructions.

Reply With Quote
  #5  
Old January 9th, 2011, 10:25 PM
biju_ps's Avatar
biju_ps biju_ps is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2008
Posts: 75 biju_ps User rank is Corporal (100 - 500 Reputation Level)biju_ps User rank is Corporal (100 - 500 Reputation Level)biju_ps User rank is Corporal (100 - 500 Reputation Level)biju_ps User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 10 h 31 m 7 sec
Reputation Power: 8
Thanks clivew,
ok . but in my case, i have to be add more than one records to the table before commit.
Quote:
Originally Posted by clivew
Well, run a query to check that they match BEFORE you COMMIT.
If they do not match issue a ROLLBACK instead and issue some appropriate warning.


I think running a query to sum all the records( may have millions) for every insert will slow down my insert process, Instead if i get the affected records in the current transaction , it will will be performance gain .

regards

Reply With Quote
  #6  
Old January 10th, 2011, 12:11 PM
clivew clivew is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2006
Location: Carlsbad, CA
Posts: 2,045 clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 37 m
Reputation Power: 382
I think you need to show us your code / SQL to get a better understanding of what you are doing.

If your logic is watertight and you do all the debits and credits inside a single transaction
there should be no problem if no exceptions are raised.
If any exceptions are raised rollback the entire transaction and address the problem.
There should be no need to do any counting or summing.

Reply With Quote
  #7  
Old January 10th, 2011, 02:03 PM
biju_ps's Avatar
biju_ps biju_ps is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2008
Posts: 75 biju_ps User rank is Corporal (100 - 500 Reputation Level)biju_ps User rank is Corporal (100 - 500 Reputation Level)biju_ps User rank is Corporal (100 - 500 Reputation Level)biju_ps User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 10 h 31 m 7 sec
Reputation Power: 8
Quote:
Originally Posted by clivew
I think you need to show us your code / SQL to get a better understanding of what you are doing.

If your logic is watertight and you do all the debits and credits inside a single transaction
there should be no problem if no exceptions are raised.
If any exceptions are raised rollback the entire transaction and address the problem.
There should be no need to do any counting or summing.


Well, i will explain the things in detail (Kindly forgive my bad english)
I have a account DB to store vouches
I am using two tables Acc_head and ACC_detail tables for that purpose. Following is the basic structure

Acc_head
ID int (primaryKey)
VocDate (date)
Narration varchar(100)

ACC_detail
ID int (primaryKey)
Header_ID int (foreign Key ->Acc_head)
LedgerID int ( foreign Key ->Acc_Ledger)
Debit Numeric,
Credit Numeric

Each voucher may have multiple credit or multiple debit , but the sum of the debit and credit in the vouchers will be the same.. so called tallied

All my other flow is designed based on the assumption that all the vouchers are tallied. So i need to ensure that the sum(Debit) , sum(credit) in the ACC_detail will always tallied.

The user may insert the data through the UI ( here we can check the data validity at client side) or sometimes using the SQL ...

When the user inputs data using SQL, i am not able to validated the data. since the user inputs the sql one another as follows

begin transaction
insert into Acc_head (fid....) Values(.....) returning ID into HID
insert into ACC_detail (Header_id,debit,credit) Values(HID,n,n1)
insert into ACC_detail (Header_id,debit,credit) Values(HID,n2,n3)
insert into ACC_detail (Header_id,debit,credit) Values(HID,n4,n5)

Commit


At this point how can i check the sum(debit),Sum(Credit) in the current transaction is tallied.

If i have the details about the rows affected in the current transaction (In TRANSACTION COMMIT TRIGGER) , then i can write a rule there. So that the validity of the data can be checked inside the database itself.

hope you got some idea
thanks ,

Last edited by biju_ps : January 10th, 2011 at 02:10 PM.

Reply With Quote
  #8  
Old January 10th, 2011, 07:48 PM
clivew clivew is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2006
Location: Carlsbad, CA
Posts: 2,045 clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 37 m
Reputation Power: 382
Isn't a TRANSACTION COMMIT TRIGGER a DATABASE trigger?
i.e. I do not believe you can apply it to a designated transaction; but I could be wrong.

What is wrong with (I am using pseudo code):

SQL Code:
Original - SQL Code
  1. BEGIN transaction
  2.  
  3.     INSERT INTO Acc_head (fid....) VALUES(.....) returning ID INTO HID
  4.     INSERT INTO ACC_detail (Header_id,debit,credit) VALUES(HID,n,n1)
  5.     INSERT INTO ACC_detail (Header_id,debit,credit) VALUES(HID,n2,n3)
  6.     INSERT INTO ACC_detail (Header_id,debit,credit) VALUES(HID,n4,n5)
  7.  
  8.    SELECT (SUM(debit) - SUM(credit))
  9.   FROM ACC_detail
  10.   WHERE (Header_id = HID);
  11.  
  12.   IF the result IS zero
  13.      Commit
  14.   ELSE
  15.      Rollback AND do your fixes
.

Reply With Quote
  #9  
Old January 10th, 2011, 10:53 PM
biju_ps's Avatar
biju_ps biju_ps is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2008
Posts: 75 biju_ps User rank is Corporal (100 - 500 Reputation Level)biju_ps User rank is Corporal (100 - 500 Reputation Level)biju_ps User rank is Corporal (100 - 500 Reputation Level)biju_ps User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 10 h 31 m 7 sec
Reputation Power: 8
thanks clivew ,

.. I will use this ..

thanks a lot

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesFirebird SQL Development > How to know the Records affected in a Transaction

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap