I am attempting to execute an MS SQL database transaction that:

1. Inserts a record into table A,
2. gets the identity of the record inserted into table A,
3. inserts a record into table B with the identity from the record in table A as a value in one of the fields of the record to be inserted into table B,
4. inserts a record into table C,
5. and gets the identity of the record inserted into table C.

Steps 1 through 4 perform as expected, but step 5 doesn't return the expected record identity. Instead, zero (0) is the only value returned. If I remove steps 1 through 3 and simply attempt to insert a record into table C, the program still does not return the expected record identity. In other words, the code from steps 1 through 3 are not affecting the code in steps 4 and 5. So, I believe I have isolated the issue to table C. One important difference between table A (from which I can get the identity of the record inserted) and table C is that table A doesn't have any table triggers, while table C has five tiggers (1 insert; 4 updates).

Here is the code that inserts a record into table C and attempts to get the identity of the record inserted into that table.

my $mssql = 'DECLARE @RetVal as Int; INSERT INTO TableC ( [ID], [Amoun +t]) VALUES ( 236, 136400); Set @RetVal = SCOPE_IDENTITY(); Select @Re +tVal as RETURNVALUE;'; my $sth = $msdataconn->prepare($mssql); LogMsg(7, "Executing $mssql"); my $error = $sth->execute(); my @values = $sth->fetchrow_array; $recIDInsurance = $values[0] if defined($values[0]); if ($msdataconn->errstr) { LogMsg(0, $error); LogMsg(0, $msdataconn->errstr); # do some stuff }

So, will an MS SQL db table trigger affect the value returned by "SCOPE_IDENTITY()"?

UPDATE: I've tested the query in $mssql directly on MSSQL Server 2005. The record is inserted and the scope identity is returned. So, this makes me think there is an issue with my version of the Perl module (Sybase.pm,v 1.44 2003/04/03 19:15:13 mpeppler) I am using to connect to the database.

In reply to Will an MS SQL db table trigger affect the value returned by scope_identity? by wreade

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.