Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Trigger modifies rowversion for ALL rows

Author  Topic 

kelleyb
Yak Posting Veteran

61 Posts

Posted - 2003-06-23 : 16:54:03
I have a rowversion column in one of my tables, and a trigger for INSERT, UPDATE. When I perform an INSERT, the rowversion is changed for ALL rows of the table, not just the "inserted" as I would have expected. Any thoughts?


CREATE TABLE dbo.ContactParent (
DTModified rowversion NOT NULL ,
ParentGUID uniqueidentifier ROWGUIDCOL DEFAULT NEWID() NOT NULL,
ParentID nchar(16) NOT NULL CONSTRAINT PK_ParentID PRIMARY KEY CLUSTERED,
ParentDesc nvarchar(128) NOT NULL
) ON [PRIMARY]
GO

CREATE TRIGGER TIU_GenParentID
ON dbo.ContactParent
FOR INSERT, UPDATE
AS
SET NOCOUNT ON
IF UPDATE (ParentDesc)
UPDATE ContactParent SET ParentID = LEFT(ParentDesc, 5)
GO

INSERT ContactParent (ParentDesc, ParentID) VALUES ('MyBigCompany', '')
SELECT DTModified FROM ContactParent


byrmol
Shed Building SQL Farmer

1591 Posts

Posted - 2003-06-23 : 18:15:33
Kelly,

Your update query is changing ALL ParentID's in the table and not just the rows that where inserted.

You need to join the ContactParent with the INSERTED table to restrict it to just the effected rows. Look it up in BOL...

It would something like..


UPDATE C SET C.ParentID = LEFT(C.ParentDesc, 5)
FROM ContactParent C
INNER JOIN INSERTED I ON I.ParentID = C.ParentID


A few other questions..

1) What is the base type, Defaults and Rules for rowversion?
2) Besides the extra disk space why isn't the ParentDesc column your Primary Key?
3) Why is this trigger set for an INSERT since you already have the ParentID (NOT NULL)

DavidM

"SQL-3 is an abomination.."
Go to Top of Page

kelleyb
Yak Posting Veteran

61 Posts

Posted - 2003-06-23 : 18:33:49
Hey David,

I think I found my answer. Sometimes it doesn't matter how hard I look through BOL, and I honestly *do* search it pretty extensively before posting something here. (You know, "RTFM"). The solution was simple enough that I must have glossed over it 20 times without actually "seeing" it. What I needed in my trigger was the condition
WHERE ParentID = (SELECT ParentID FROM inserted)
I suppose the JOIN you recommend would save me a table scan in my trigger and I'll probably implement that one instead. (Thanks, by the way).

To answer your questions: First you should know that I posted only example code that I was still farting around with. I'll post full (read: finished) code that I'm having trouble with next time.

According to BOL rowversion is a synonym for timestamp since timestamp behavior will be changing with the next version of SQL. It's equivalent to a varbinary(8). The column (ParentDesc) isn't my key because it will likely contain spaces, mixed case, or alternative characters. I didn't want to mess around with all the string validation as I'm trying to get data from other tables. Finally, The trigger is being used for INSERT because I was actually passing an empty string, knowing that the trigger would ultimately generate the value for this column based on the data from two other columns. (see my post today "Primary Key's")

However, if you have suggestions (or would like to scold me for doing something in a non-standard way) I certainly do welcome your comments. Really! I know enough to get dirty and screw stuff up, but I'm trying to learn the right way to do things.

Thanks,
-Brian

Go to Top of Page
   

- Advertisement -