PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Hébergement serveur > comp.db.ms-sqlserver > Trigger and Identity column : what's wrong ?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Trigger and Identity column : what's wrong ?

Réponse
 
LinkBack Outils de la discussion
Vieux 22/03/2008, 23h34   #1
Celine
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Trigger and Identity column : what's wrong ?

Hi,

I have to tables : Rank and Item
Rank contains a FK named R_I_id which references Item's primary key
named I_id, which is also an identity column.

I want the field R_I_id of table Rank to be updated with the value of
Item's I_id when a new Item is inserted. So I wrote the following
trigger :

CREATE TRIGGER [dbo].[trg_oninsertitem]
ON [dbo].[Item]
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON
SET IMPLICIT_TRANSACTIONS OFF
DECLARE @I_id INT
SELECT @I_id = SCOPE_IDENTITY()

BEGIN TRANSACTION
INSERT Rank(R_I_id) VALUES(@I_id)
IF (@@ERROR <> 0) BEGIN
PRINT 'error in trigger trg_oninsertitem'
ROLLBACK TRANSACTION
END
COMMIT
END

But when I insert a value in Item, I got the following message :

Msg 515, Level 16, State 2, Procedure trg_oninsertitem, Line 17
Cannot insert the value NULL into column 'R_T_id', table
'base.dbo.Rank'; column does not allow nulls. INSERT fails.

and I don't understand, because my trigger is fired after the value is
inserted in table Item, so the field I_id should contain a new
identity value, not NULL

my DBMS is SQL Server 2000

thanks in advance for your
  Réponse avec citation
Vieux 23/03/2008, 05h01   #2
Dan Guzman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Trigger and Identity column : what's wrong ?

> and I don't understand, because my trigger is fired after the value is
> inserted in table Item, so the field I_id should contain a new
> identity value, not NULL


I don't see any reference to I_id in the trigger code. However, there are a
couple of more serious issues that need attention.

First, a trigger fires once per statement rather than once per row. You can
use the inserted table in the trigger code to obtain values of the row(s)
just inserted so that multi-row inserts are properly handled. Second, a
trigger always runs in the context of a transaction so you should not issue
a BEGIN TRAN or COMMIT in a trigger.

The example below will insert the I_id values of the newly inserted rows
into the Rank table.

CREATE TRIGGER [dbo].[trg_oninsertitem]
ON [dbo].[Item]
AFTER INSERT
AS
SET NOCOUNT ON

INSERT dbo.Rank(R_I_id)
SELECT I_id FROM inserted

IF @@ERROR <> 0
BEGIN
PRINT 'error in trigger trg_oninsertitem'
ROLLBACK TRANSACTION
END
GO

--
Hope this s.

Dan Guzman
SQL Server MVP
http://weblogs.sqlteam.com/dang/

"Celine" <xhanrot@gmail.com> wrote in message
news:48ff0fc1-ad32-41ab-b0c9-20f9a274cdaa@e10g2000prf.googlegroups.com...
> Hi,
>
> I have to tables : Rank and Item
> Rank contains a FK named R_I_id which references Item's primary key
> named I_id, which is also an identity column.
>
> I want the field R_I_id of table Rank to be updated with the value of
> Item's I_id when a new Item is inserted. So I wrote the following
> trigger :
>
> CREATE TRIGGER [dbo].[trg_oninsertitem]
> ON [dbo].[Item]
> AFTER INSERT
> AS
> BEGIN
> SET NOCOUNT ON
> SET IMPLICIT_TRANSACTIONS OFF
> DECLARE @I_id INT
> SELECT @I_id = SCOPE_IDENTITY()
>
> BEGIN TRANSACTION
> INSERT Rank(R_I_id) VALUES(@I_id)
> IF (@@ERROR <> 0) BEGIN
> PRINT 'error in trigger trg_oninsertitem'
> ROLLBACK TRANSACTION
> END
> COMMIT
> END
>
> But when I insert a value in Item, I got the following message :
>
> Msg 515, Level 16, State 2, Procedure trg_oninsertitem, Line 17
> Cannot insert the value NULL into column 'R_T_id', table
> 'base.dbo.Rank'; column does not allow nulls. INSERT fails.
>
> and I don't understand, because my trigger is fired after the value is
> inserted in table Item, so the field I_id should contain a new
> identity value, not NULL
>
> my DBMS is SQL Server 2000
>
> thanks in advance for your


  Réponse avec citation
Vieux 23/03/2008, 18h58   #3
--CELKO--
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Trigger and Identity column : what's wrong ?

>> I have two tables: Rank and Item Rank contains a FK named R_I_id which references Item's PRIMARY KEY named I_id, which is also an IDENTITY column. <<

1) The singular names say that they each have one row in them. Is
this true?

2) They are not really tables at all. IDENTITY is never a relational
key. It is a physical locator counter that increments when an
insertion was attempted on a table. It has nothing to do with the
data model.

3) The table "rank" should not be here; it is an attribute of an
entity and not an entity itself. The name "Items" is also too vague
to be a data element name.

4) You should not be writing triggers except under the most extreme
circumstance. This looks like a case of "attribute splitting" and an
attempt to repair the design flaws with procedural code.

Post a description of what you are trying to do and we will see if we
can get you a workable schema design that does not need extreme
measures.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h37.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12911 seconds with 11 queries