Hello fellow Monks,

I am trying to get MSSQL and FreeTDS to return the value of an autoincrement of a primary key after an insert. This is not the first time this has been asked here: $code or die asked in DBI and primary keys, and was correctly informed that the system variable @@IDENTITY contained the desired value, and then indicated in a followup to that thread (here) that SQL of the form

insert into table .......... select @@IDENTITY
would get the desired value (MSSQL allows you to put multiple statements on one line like that).

All of this is well and good, but I believe $code or die was working on Perl in a Windows environment; I want to make it just that more difficult and use FreeTDS from a linux box to do the work. Here is the test code I have written:

#!/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) sel +ect @@IDENTITY as foo'); $sth->execute; my $data = $sth->fetchrow_hashref; my $seqid= $$data{foo}; my $err = $sth->errstr; print "$seqid, $err\n";
In addition to fetchrow_hashref, I have tried fetchrow_array and fetch, changing the assignment to $seqid to the right syntax. So here's the problem: the value of $seqid seems to be getting truncated. The value of @@IDENITY is currently on the order of 20, but when I print $seqid, I get 2 (until I got the ID up to 20, I thought I was getting a return value of 1). The error string is empty, as it should be, since nothing seems to be going wrong on the database side (when I run the command in Query Analyzer, it works fine). So it now looks like a bug in FreeTDS, but I am not sure. Does anyone see any other problems in my code that could explain what I am seeing?

Thanks,
Scott


In reply to Getting identity after autoincr in FreeTDS/MSSQL/DBI by scain

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.