Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Will an MS SQL db table trigger affect the value returned by scope_identity?

by wreade (Initiate)
on Dec 06, 2011 at 17:20 UTC ( [id://942064]=perlquestion: print w/replies, xml ) Need Help??

wreade has asked for the wisdom of the Perl Monks concerning the following question:

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.
  • Comment on Will an MS SQL db table trigger affect the value returned by scope_identity?
  • Download Code

Replies are listed 'Best First'.
Re: Will an MS SQL db table trigger affect the value returned by scope_identity?
by JavaFan (Canon) on Dec 06, 2011 at 22:46 UTC
    Unless you are getting the result you expect when using the interactive client, I don't think Perlmonks is a useful forum to ask your question. This is a Perl forum, specialists of the MS SQL dialect are found elsewhere.

      JavaFan: thanks for your reply. One goal of my post was to confirm that others did not hit this issue (a scope identity isn't returned from a table with triggers) due to an issue with DBI. In other words, I want to be confident that I can eliminate Perl and DBI as the source of the issue.

Re: Will an MS SQL db table trigger affect the value returned by scope_identity?
by mpeppler (Vicar) on Dec 27, 2011 at 08:40 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://942064]
Approved by ChuckularOne
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-04-25 10:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found