Groups | Blog | Home
all groups > sql server programming > april 2007 >

sql server programming : DELETE trigger does not work on this particular table


Joseph I. Ceasar
4/27/2007 3:52:42 PM
I have a very strange table on which I cannot create a delete trigger. I am
using SQL Server 2005. The INSERT trigger does work on this table. The
UPDATE does not work either. I have created the same table on two databases
on two separate servers!

Here is the table:

CREATE TABLE [dbo].[Borrowings3](
[BookId] [smallint] NOT NULL,
[BorrowDate] [datetime] NULL,
[BorrowAmt] [numeric](20, 8) NULL,
[Rate] [numeric](20, 8) NULL,
[UpdateDate] [datetime] NULL,
[UpdateUser] [varchar](128) NULL
) ON [PRIMARY]
GO


And here it the trigger:


CREATE TRIGGER [dbo].[tr_Borrowings3] ON [dbo].[Borrowings3] FOR DELETE AS
BEGIN
select * into mydeleted from deleted
END


When I try to delete a record, this is the message that I get:

---------------------------
Microsoft SQL Server Management Studio
---------------------------
No rows were deleted.

A problem occurred attempting to delete row 1.
Error Source: Microsoft.VisualStudio.DataTools.
Error Message: The row value(s) updated or deleted either do not make the
row unique or they alter multiple rows(2 rows).

Correct the errors and attempt to delete the row again or press ESC to
cancel the change(s).
---------------------------
OK Help
---------------------------


If I remove the trigger, then everything works just fine.


Andrew J. Kelly
4/27/2007 5:30:19 PM
Add a Primary Key constraint to the table so it knows which rows to delete.

--
Andrew J. Kelly SQL MVP

[quoted text, click to view]

TheSQLGuru
4/27/2007 11:41:11 PM
I think a unique index would work too.

If you don't have data that is currently unique using some combination of
columns, you could add an identity-type column and make that the PK.

--
TheSQLGuru
President
Indicium Resources, Inc.

[quoted text, click to view]

Erland Sommarskog
4/28/2007 10:22:06 PM
Joseph I. Ceasar (joseph@harvesttechnology.com) writes:
[quoted text, click to view]

Whatever, the trigger will only work once, since next time the table
mydeleted exists, and you will get an error.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
--CELKO--
4/29/2007 6:10:21 PM
1) You have no key in this table. Was it supposed to be "book_id"
which we would call the ISBN if you knew or cared about industry
standards? Have you looked for redundant duplicates in the data?

2) The MyDeleted table will attempt to be created over and over with
the proprietary INTO clause (in Standard SQL, you would have a
singleton SELECT instead) and it can only exist once.

3) A newbie will often fix this with a base table and an INSERT INTO
change in the trigger. Wrong! You might want to talk the accounting
department about the proper way to do audit trails. The data is kept
outside of the schema so extra meta-data columns like "UpdateDate" and
"UpdateUser" should not be here -- unless you really want to go to
jail :)

Look at third party audit tools and do it right.


Shuurai
4/30/2007 7:29:18 AM

[quoted text, click to view]

As I've said before, you really need to get a clue about this before
you start babbling about this.

First off, you don't even know if his company or his database are even
subject to SOX. Second, your knowledge of the requirements is flawed
- there is nothing in SOX that states you will "go to jail" for having
audit data in your database. You can get into trouble for FAILING to
have audit information outside of your data. Think really hard and
maybe you'll understand the difference.
AddThis Social Bookmark Button