472,958 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

@@TRANCOUNT difference between SQL 7 and SQL 2000 in trigger

During testing of an application, i noticed a difference between
SQL 2000 and SQL 7, both with identical config.

In a nutshell:
A table has a trigger for UPDATE and DELETE.
When a column in the table is UPDATED the following happens:

In autocommit mode, when entering a trigger the trancount equals
1 for both SQL 7 and 2000.

When the same update is performed in an explicit transaction
in SQL 7 @@TRANCOUNT equal 2, and in SQL 2000 @@TRANCOUNT equals 1.

Configuration is the same and there are no implicit transactions.

I don't need a work around as this will invalidate the migration
process as both products should behave identically.
What would influence the difference or why is there a difference???
Is there something which has been overlooked?

================================================== =======

The following code replicates the problem

Ensure implicit transactions are off in both versions at the server
level, thus defaulting to autocommitted mode.
Ensure sp_configure settings are identical.

Step 1: Create a DB called test:

Step 2: Execute the following under the context of test DB.

if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[trigtest]') and OBJECTPROPERTY(id, Outrigger') = 1)

drop trigger [dbo].[trigtest]
GO

if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[test]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)

drop table [dbo].[test]
GO

if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[trancount]') and OBJECTPROPERTY(id, N'IsUserTable')
= 1)

drop table [dbo].[trancount]
GO

CREATE TABLE [dbo].[test] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[text] [char] (10) NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[trancount] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[trancount] [int] NOT NULL
) ON [PRIMARY]
GO

CREATE TRIGGER trigtest ON [dbo].[test]
FOR UPDATE, DELETE
AS
declare @trancount int

select @trancount = @@TRANCOUNT

insert into trancount ( trancount ) values ( @trancount )
Step 3: Run the following against the DB, then check trancount table.

-- Add a record to the test table (trigger will not fire)
insert into test (text) values ( 'xxxx' );
go

-- Update the value (autocommit mode) to fire trigger
-- Under SQL 7 and 2000, trancount table will only indicate 1
tranaction open.
-- This is being performed in autocommit mode.
update test set text = 'test1'
go

-- Update value using an explicit transaction
-- Under SQL 7, trancount will equal 2 in trigger, in SQL 2000
trancount equals 1
begin transaction
update test set text = 'test2'
commit work
go
Jul 20 '05 #1
5 3469
Neil Rutherford (ne*************@yahoo.com) writes:
During testing of an application, i noticed a difference between
SQL 2000 and SQL 7, both with identical config.

In a nutshell:
A table has a trigger for UPDATE and DELETE.
When a column in the table is UPDATED the following happens:

In autocommit mode, when entering a trigger the trancount equals
1 for both SQL 7 and 2000.

When the same update is performed in an explicit transaction
in SQL 7 @@TRANCOUNT equal 2, and in SQL 2000 @@TRANCOUNT equals 1.

Configuration is the same and there are no implicit transactions.

I don't need a work around as this will invalidate the migration
process as both products should behave identically.
What would influence the difference or why is there a difference???
Is there something which has been overlooked?


Apparently there was - consciously or by chance - a change in SQL2000.
I cannot say why, and indeed 2 would be a more expected result in
this situation.

But I would be interesting to know why this would be an issue? It sounds
to me like your triggers must be doing something quite interesting.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2
BOL says:
"Microsoft SQL Server 2000 increments the transaction count within a
statement only when the transaction count is 0 at the start of the
statement. In SQL Server version 7.0, the transaction count is always
incremented, regardless of the transaction count at the start of the
statement. This can cause the value returned by @@TRANCOUNT in triggers to
be lower in SQL Server 2000 than it is in SQL Server version 7.0.

In SQL Server 2000, if a COMMIT TRANSACTION or COMMIT WORK statement is
executed in a trigger, and there is no corresponding explicit or implicit
BEGIN TRANSACTION statement at the start of the trigger, users may see
different behavior than on SQL Server version 7.0. Placing COMMIT
TRANSACTION or COMMIT WORK statements in a trigger is not recommended."
HTH
Igor
ne*************@yahoo.com (Neil Rutherford) wrote in message news:<d6**************************@posting.google. com>...
During testing of an application, i noticed a difference between
SQL 2000 and SQL 7, both with identical config.

In a nutshell:
A table has a trigger for UPDATE and DELETE.
When a column in the table is UPDATED the following happens:

In autocommit mode, when entering a trigger the trancount equals
1 for both SQL 7 and 2000.

When the same update is performed in an explicit transaction
in SQL 7 @@TRANCOUNT equal 2, and in SQL 2000 @@TRANCOUNT equals 1.

Configuration is the same and there are no implicit transactions.

I don't need a work around as this will invalidate the migration
process as both products should behave identically.
What would influence the difference or why is there a difference???
Is there something which has been overlooked?

================================================== =======

The following code replicates the problem

Ensure implicit transactions are off in both versions at the server
level, thus defaulting to autocommitted mode.
Ensure sp_configure settings are identical.

Step 1: Create a DB called test:

Step 2: Execute the following under the context of test DB.

if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[trigtest]') and OBJECTPROPERTY(id, Outrigger') = 1)

drop trigger [dbo].[trigtest]
GO

if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[test]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)

drop table [dbo].[test]
GO

if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[trancount]') and OBJECTPROPERTY(id, N'IsUserTable')
= 1)

drop table [dbo].[trancount]
GO

CREATE TABLE [dbo].[test] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[text] [char] (10) NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[trancount] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[trancount] [int] NOT NULL
) ON [PRIMARY]
GO

CREATE TRIGGER trigtest ON [dbo].[test]
FOR UPDATE, DELETE
AS
declare @trancount int

select @trancount = @@TRANCOUNT

insert into trancount ( trancount ) values ( @trancount )
Step 3: Run the following against the DB, then check trancount table.

-- Add a record to the test table (trigger will not fire)
insert into test (text) values ( 'xxxx' );
go

-- Update the value (autocommit mode) to fire trigger
-- Under SQL 7 and 2000, trancount table will only indicate 1
tranaction open.
-- This is being performed in autocommit mode.
update test set text = 'test1'
go

-- Update value using an explicit transaction
-- Under SQL 7, trancount will equal 2 in trigger, in SQL 2000
trancount equals 1
begin transaction
update test set text = 'test2'
commit work
go

Jul 20 '05 #3
Igor Raytsin (ig*****@yahoo.com) writes:
BOL says:
"Microsoft SQL Server 2000 increments the transaction count within a
statement only when the transaction count is 0 at the start of the
statement. In SQL Server version 7.0, the transaction count is always
incremented, regardless of the transaction count at the start of the
statement. This can cause the value returned by @@TRANCOUNT in triggers to
be lower in SQL Server 2000 than it is in SQL Server version 7.0.

In SQL Server 2000, if a COMMIT TRANSACTION or COMMIT WORK statement is
executed in a trigger, and there is no corresponding explicit or implicit
BEGIN TRANSACTION statement at the start of the trigger, users may see
different behavior than on SQL Server version 7.0. Placing COMMIT
TRANSACTION or COMMIT WORK statements in a trigger is not recommended."


It's even documented! I didn't know that. Thanks, Igor!

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #4
Thanks for your help guys.

Someone put logic in a trigger to only continue
if the @@TRANCOUNT came from an explicit transaction in
SQL Server 7 and the @@TRANCOUNT > 1

Like mentioned, in SQL 7 this works, but in SQL 2000..
it breached the integrity of a whole data warehouse system
test.

I have to convince the developers and management that
there is a change between versions. The developers
are convinced there is difference between the server
config and they believe that both versions should
work identically.

Unless I'm going blind.. where in books on-line is
the passage above?



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #5
Neil Rutherford (ne*************@yahoo.com) writes:
Someone put logic in a trigger to only continue
if the @@TRANCOUNT came from an explicit transaction in
SQL Server 7 and the @@TRANCOUNT > 1
Dubious usage. I have not checked, but recursive trigger calls
may have slipped.

True, I have stored procedures which barf if you call them without
a transaction in progress, but that is because they perform only
half the job.
I have to convince the developers and management that
there is a change between versions. The developers
are convinced there is difference between the server
config and they believe that both versions should
work identically.
Obviously there is a difference between versions. This is nothing you
configure.
Unless I'm going blind.. where in books on-line is
the passage above?


I searched for the string "the transaction count is always incremented"
and found two hits.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: GJones | last post by:
Is there away to capture a session variable value from IIS and use it in a SQL 2000 trigger with out passing it as a part of a string query. I just want to capture it somehow systemically. ...
3
by: level8 | last post by:
Hi, Everybody, I'm a Hungarian SQL user and I need a little help for SQL Server 7 ! I protect my table against bad data with a trigger. I use ROLLBACK and RAISERROR statement in this trigger....
9
by: Fernand St-Georges | last post by:
Hi, can someone tell how to write a Trigger; I am familiar with Sybase Sql Anywhere trigger syntax. Actually I have three tables MEMBER, CONTRACT and PAYMENT I need to update the MEMBER.BALANCE...
6
by: Scott CM | last post by:
I have a multi-part question regarding trigger performance. First of all, is there performance gain from issuing the following within a trigger: SELECT PrimaryKeyColumn FROM INSERTED opposed...
3
by: hitendra15 | last post by:
Can it be possible to create trigger/procedure in following case 2 server server A and server B A has db1 db1 has tblA B has db2 db2 has tblB can it be possible to create trigger on server...
8
by: Stuart McGraw | last post by:
Is Microsoft full of #*$#*% (again) or am I badly misunderstanding something? Quote from Microsoft's T-SQL doc: > INSTEAD OF triggers are executed instead of the triggering action. > These...
2
by: rdraider | last post by:
I created and successfully tested a trigger on a test database. Now that I want to put this on a production system, the create trigger statement takes way too long to complete. I cancelled after...
9
by: Ots | last post by:
I'm using SQL 2000, which is integrated with a VB.NET 2003 app. I have an Audit trigger that logs changes to tables. I want to apply this trigger to many different tables. It's the same trigger,...
1
by: majastic | last post by:
Hello all, I have a database question for you. I have a table in SQL 2000 that I think I need a trigger for. I'm tring to get the trigger to take the last two numbers from one field and replace...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.