Skip to Content.
Sympa Menu

freetds - Getting @@IDENTITY back from MSSQL

freetds AT lists.ibiblio.org

Subject: FreeTDS Development Group

List archive

Chronological Thread  
  • From: "Scott Cain" <scain AT athersys.com>
  • To: freetds AT franklin.oit.unc.edu
  • Subject: Getting @@IDENTITY back from MSSQL
  • Date: Fri, 9 Nov 2001 11:48:03 -0500


Hello,

I am writing Perl (5.6.1) on a linux box (RH 7.1) with FreeTDS (0.51) and
DBI (1.19) to talk to a MSSQL server on a win2000 box. I want to get
@@IDENTITY from the last insert I made (@@IDENTITY contains that value of
the last autoincrement on a primary key).

Here is test code I created:

#!/usr/local/bin/perl -w
use DBI;
use strict;

$ENV{'SYBASE'} = '/usr/local/freetds';
$ENV{'TDSVER'} = 70;

my $server = "server";
my $db = "db";
my $user = "sqluser";
my $passwd = "sqluserpasswd";

my $dbh =
DBI->connect("dbi:Sybase:server=$server;database=$db",$user,$passwd)
or die "--$!--\n";

my $sth = $dbh->prepare(' insert into sctemp (scratch) values (42)
select @@IDENTITY as foo');
$sth->execute;

my $data = $sth->fetchrow_hashref;
my $seqid= $$data{foo};
my $err = $sth->errstr;
print "$seqid, $err\n";

The problem I have is that $seqid seems to be getting truncated, ie, the
value of @@IDENTITY is about 20, but the value of $seqid that gets printed
out is 2. The query works fine as written in the Query Analyzer on
Windows. At this point, it feels like a problem with FreeTDS. In
addition to fetchrow_hashref, I tried fetch and fetchrow_array. Any
ideas?

Thanks,
Scott




Archive powered by MHonArc 2.6.24.

Top of Page