Skip to Content.
Sympa Menu

freetds - Re: [freetds] SELECT @@IDENTITY AS InsertID

freetds AT lists.ibiblio.org

Subject: FreeTDS Development Group

List archive

Chronological Thread  
  • From: Shaun Craig <s.craig AT andronics.com>
  • To: FreeTDS Development Group <freetds AT lists.ibiblio.org>
  • Subject: Re: [freetds] SELECT @@IDENTITY AS InsertID
  • Date: Wed, 09 Nov 2005 13:10:49 +0000

Thanks Bill,

Only getting back to this now. Just noticed another potential problem, I'm current using the connection pool and my program will grab a single connection and do multiple statements on this connection before releasing it. I've just noticed a returned insertid that definitely wasn't for record that was just entered which is kinda worrying. Thing is any number of connection pool connections can be doing the same thing at the same time but i thought that if it has its own connection it couldn't get another connections insertid, right or wrong?

Thanks,

Shaun.

Thompson, Bill D (London) wrote:

Shaun,

snippets from one of my test programs....

#include <stdio.h>
#include <sybfront.h>
#include <sybdb.h>

/* Forward declarations of the error handler and message handler. */
int err_handler();
int msg_handler();

main(argc, argv)
int argc;
char *argv[];
{
DBPROCESS *dbproc;
.....
/* Initialize DB-Library. */
if (dbinit() == FAIL)
exit(1);

/* Install the user-supplied error-handling and message-handling
* routines. They are defined at the bottom of this source file.
*/
dberrhandle((EHANDLEFUNC)err_handler);
dbmsghandle((MHANDLEFUNC)msg_handler);

.....

int err_handler(dbproc, severity, dberr, oserr, dberrstr, oserrstr)
DBPROCESS *dbproc;
int severity;
int dberr;
int oserr;
char *dberrstr;
char *oserrstr;
{
if ((dbproc == NULL) || (DBDEAD(dbproc))) {
fprintf (stderr, "dbproc NULL or DEAD\n");
return(INT_EXIT);
}
else
{
fprintf (stderr, "DB-Library error:\n\t%s\n", dberrstr);
fprintf (stderr, "severity: %d\nnumber: %d\n", severity, dberr);

if (oserr != DBNOERR)
fprintf (stderr, "Operating-system error:\n\t%s\n",
oserrstr);

return(INT_CANCEL);
}
}

int msg_handler(dbproc, msgno, msgstate, severity, msgtext,
srvname, procname, line)

DBPROCESS *dbproc;
DBINT msgno;
int msgstate;
int severity;
char *msgtext;
char *srvname;
char *procname;
int line;

{
fprintf (stderr, "Msg %ld, Level %d, State %d\n",
msgno, severity, msgstate);

if (strlen(srvname) > 0)
fprintf (stderr, "Server '%s', ", srvname);
if (strlen(procname) > 0)
fprintf (stderr, "Procedure '%s', ", procname);
if (line > 0)
fprintf (stderr, "Line %d", line);

fprintf (stderr, "\n\t%s\n", msgtext);

return(0);
}

-----Original Message-----
From: freetds-bounces AT lists.ibiblio.org
[mailto:freetds-bounces AT lists.ibiblio.org] On Behalf Of Shaun Craig
Sent: 03 November 2005 12:07
To: FreeTDS Development Group
Subject: Re: [freetds] SELECT @@IDENTITY AS InsertID


Thought everything was rosy with this but its not! I'm having a small problem where the insertid i get back is incorrect, i think that the value is not actually returned and the value i get is a memory value. I

have not used any error handlers before with freetds, if someone could give us the code i could probably find out whats wrong.

Code example, take for granted I called the following before the snipet below
DBINT InsertID;
int journey_insert_id //global program variable
etc...
dbcmd()
dbsqlexec()
dbresults()

if ( strstr(statements[z],"SELECT @@IDENTITY AS InsertID") != NULL)
{
if (SUCCEED != dbbind(dbconn, 1, INTBIND, (DBINT)0, (BYTE *)& InsertID))
{
if (error_level > 0)
LogError(GPRS_ERR,15,"Insert", "myDBQuery(), Could Not Bind JourneyInsetID","fail");
return -1;
} while ( dbnextrow(dbconn) != NO_MORE_ROWS) // no error handling here which probably where my problem is
{
journey_insert_id = InsertID;
} }

Hope someone can help me out as I have to get this sorted before christmas, then its off to Australia for a year!

Thanks,

Shaun.

Shaun Craig wrote:


Finally got the insertid back from my insert, I just tinkered around with the code I had and eventually got it back.

Thanks to all who helped me on it.

Shaun.

Shaun Craig wrote:


Just realised my tdsdump file wasn't attached to email yesterday, so here it is.

util.c:269:Starting log file for FreeTDS 0.64.dev.20050228
on 2005-10-12 09:31:17 with debug flags 0x4fff.
iconv.c:364:iconv to convert client-side data to the "ISO-8859-1" character set
iconv.c:517:tds_iconv_info_init: converting "ISO-8859-1"->"UCS-2LE"
net.c:150:Connecting to 127.0.0.1 port 5000.
net.c:650:Sending packet
0000 02 00 02 00 00 00 00 00-73 76 72 2d 73 75 73 65 |........

svr-suse|

0010 2d 31 38 00 00 00 00 00-00 00 00 00 00 00 00 00 |-18.....

........|

0020 00 00 00 00 00 00 0b 73-61 00 00 00 00 00 00 00

|.......***.......|

0030 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 |........

........|

0040 00 00 00 00 00 02 72 75-6c 65 72 00 00 00 00 00

|......******.....|

0050 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 |........

........|

0060 00 00 00 00 05 33 37 38-37 36 00 00 00 00 00 00 |.....378

76......|

0070 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 |........

........|

0080 00 00 00 05 03 01 06 0a-09 01 00 00 00 00 02 00 |........

........|

0090 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 |........

........|

00a0 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 |........

........|

00b0 00 00 00 6d 79 70 6f 6f-6c 00 00 00 00 00 00 00 |...mypoo

l.......|

00c0 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 |........

........|

00d0 00 06 72 75 6c 65 72 00-00 00 00 00 00 00 00 00 |..*****.

........|

00e0 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 |........

........|

00f0 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 |........

........|

0100 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 |........

........|

0110 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 |........

........|

0120 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 |........

........|

0130 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 |........

........|

0140 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 |........

........|

0150 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 |........

........|

0160 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 |........

........|

0170 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 |........

........|

0180 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 |........

........|

0190 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 |........

........|

01a0 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 |........

........|

01b0 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 |........

........|

01c0 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 |........

........|

01d0 00 05 04 02 00 00 44 42-2d 4c 69 62 72 61 72 79 |......DB

-Library|

01e0 0a 00 00 00 00 00 0d 11-75 73 5f 65 6e 67 6c 69 |........

us_engli|

01f0 73 68 00 00 00 00 00 00-00 00 00 00 00 00 00 00 |sh......

........|

net.c:650:Sending packet
0000 02 01 00 4c 00 00 00 00-00 00 00 00 00 00 0a 00 |...L....

........|

0010 00 00 00 00 00 00 00 00-00 00 00 00 00 69 73 6f |........

.....iso|

0020 5f 31 00 00 00 00 00 00-00 00 00 00 00 00 00 00 |_1......

........|

0030 00 00 00 00 00 00 00 00-00 00 00 05 01 35 31 32 |........

.....512|

0040 00 00 00 03 00 00 00 00-00 00 00 00 |........ ....|

token.c:317:tds_process_login_tokens()
net.c:437:Received header
0000 04 01 00 dc 00 00 00 00- |........|

net.c:531:Received packet
0000 e3 14 00 01 0b 4c 65 6f-63 61 74 65 54 65 73 74 |.....Leo

cateTest|

0010 06 6d 61 73 74 65 72 ab-3e 00 45 16 00 00 02 0a |.master.

.E.....|

0020 2a 00 43 68 61 6e 67 65-64 20 64 61 74 61 62 61 |*.Change d

databa|

0030 73 65 20 63 6f 6e 74 65-78 74 20 74 6f 20 27 4c |se conte xt to

'L|

0040 65 6f 63 61 74 65 54 65-73 74 27 2e 04 4a 44 42 |eocateTe

st'..JDB|

0050 43 05 5a 5a 5a 5a 5a 01-00 e3 0d 00 02 0a 75 73 |C.ZZZZZ.

......us|

0060 5f 65 6e 67 6c 69 73 68-00 ab 3d 00 47 16 00 00 |_english

..=.G...|

0070 01 0a 29 00 43 68 61 6e-67 65 64 20 6c 61 6e 67 |..).Chan ged

lang|

0080 75 61 67 65 20 73 65 74-74 69 6e 67 20 74 6f 20 |uage set ting

to |

0090 27 75 73 5f 65 6e 67 6c-69 73 68 27 2e 04 4a 44 |'us_engl

ish'..JD|

00a0 42 43 05 5a 5a 5a 5a 5a-01 00 e3 07 00 04 04 31 |BC.ZZZZZ

.......1|

00b0 30 32 34 00 ad 14 00 05-05 00 00 00 0a 73 71 6c |024.....

.....sql|

00c0 20 73 65 72 76 65 72 01-00 00 01 fd 00 00 02 00 | server.

........|

00d0 01 00 00 00 - |....|

token.c:321:looking for login token, got e3(ENVCHANGE)
token.c:106:tds_process_default_tokens() marker is e3(ENVCHANGE)
read.c:178:tds_get_string: reading 11 from wire to give 11 to client.
read.c:178:tds_get_string: reading 6 from wire to give 6 to client.
token.c:321:looking for login token, got ab(INFO)
token.c:106:tds_process_default_tokens() marker is ab(INFO)
token.c:2640:tds_process_msg() reading message from server
read.c:178:tds_get_string: reading 42 from wire to give 42 to client.
read.c:178:tds_get_string: reading 4 from wire to give 4 to client.
read.c:178:tds_get_string: reading 5 from wire to give 5 to client.
token.c:2702:tds_process_msg() calling client msg handler
token.c:2715:tds_process_msg() returning TDS_SUCCEED
token.c:321:looking for login token, got e3(ENVCHANGE)
token.c:106:tds_process_default_tokens() marker is e3(ENVCHANGE)
read.c:178:tds_get_string: reading 10 from wire to give 10 to client.
token.c:321:looking for login token, got ab(INFO)
token.c:106:tds_process_default_tokens() marker is ab(INFO)
token.c:2640:tds_process_msg() reading message from server
read.c:178:tds_get_string: reading 41 from wire to give 41 to client.
read.c:178:tds_get_string: reading 4 from wire to give 4 to client.
read.c:178:tds_get_string: reading 5 from wire to give 5 to client.
token.c:2702:tds_process_msg() calling client msg handler
token.c:2715:tds_process_msg() returning TDS_SUCCEED
token.c:321:looking for login token, got e3(ENVCHANGE)
token.c:106:tds_process_default_tokens() marker is e3(ENVCHANGE)
read.c:178:tds_get_string: reading 4 from wire to give 4 to client.
token.c:2528:increasing block size from to 1024
token.c:321:looking for login token, got ad(LOGINACK)
read.c:178:tds_get_string: reading 10 from wire to give 10 to client.
token.c:321:looking for login token, got fd(DONE)
token.c:106:tds_process_default_tokens() marker is fd(DONE)
token.c:2344:tds_process_end: more_results = 0
was_cancelled = 0
error = 0
done_count_valid = 0
token.c:2360:tds_process_end() state set to TDS_IDLE
util.c:125:Changing query state from TDS_IDLE to TDS_IDLE
token.c:394:leaving tds_process_login_tokens() returning 1
util.c:125:Changing query state from TDS_IDLE to TDS_QUERYING
mem.c:453:tds_free_all_results()
util.c:125:Changing query state from TDS_QUERYING to TDS_PENDING
net.c:650:Sending packet
0000 01 01 00 1b 00 00 00 00-73 65 74 20 74 65 78 74 |........ set

text|

0010 73 69 7a 65 20 36 34 35-31 32 20 |size 645 12 |

util.c:125:Changing query state from TDS_PENDING to TDS_READING
net.c:437:Received header
0000 04 01 00 11 00 00 00 00- |........|

net.c:531:Received packet
0000 fd 00 00 be 00 00 00 00-00 |........ .|

token.c:526:processing result tokens. marker is fd(DONE)
token.c:2344:tds_process_end: more_results = 0
was_cancelled = 0
error = 0
done_count_valid = 0
token.c:2360:tds_process_end() state set to TDS_IDLE
util.c:125:Changing query state from TDS_READING to TDS_IDLE
util.c:125:Changing query state from TDS_IDLE to TDS_PENDING
token.c:514:tds_process_result_tokens() state is COMPLETED
dblib.c:1138:dbuse()
dblib.c:1054:dbcmd() bufsz = 0
dblib.c:1104:in dbsqlexec()
dblib.c:5818:in dbsqlsend()
util.c:125:Changing query state from TDS_IDLE to TDS_QUERYING
mem.c:453:tds_free_all_results()
util.c:125:Changing query state from TDS_QUERYING to TDS_PENDING
net.c:650:Sending packet
0000 01 01 00 13 00 00 00 00-75 73 65 20 4c 65 6f 63 |........ use

Leoc|

0010 61 74 65 - |ate|

dblib.c:4005:in dbsqlok()
net.c:437:Received header
0000 04 01 00 63 00 00 00 00- |...c....|

net.c:531:Received packet
0000 e3 11 00 01 07 4c 65 6f-63 61 74 65 07 4c 65 6f |.....Leo

cate.Leo|

0010 63 61 74 65 ab 3b 00 45-16 00 00 01 00 26 00 43 |cate.;.E

.....&.C|

0020 68 61 6e 67 65 64 20 64-61 74 61 62 61 73 65 20 |hanged d

atabase |

0030 63 6f 6e 74 65 78 74 20-74 6f 20 27 4c 65 6f 63 |context to

'Leoc|

0040 61 74 65 27 2e 09 53 51-4c 53 45 52 56 45 52 00 |ate'..SQ

LSERVER.|

0050 01 00 fd 00 00 e2 00 00-00 00 00 |........ ...|

dblib.c:4024:dbsqlok() marker is e3
util.c:125:Changing query state from TDS_PENDING to TDS_READING
token.c:526:processing result tokens. marker is e3(ENVCHANGE)
token.c:106:tds_process_default_tokens() marker is e3(ENVCHANGE)
read.c:178:tds_get_string: reading 7 from wire to give 7 to client.
read.c:178:tds_get_string: reading 7 from wire to give 7 to client.
token.c:526:processing result tokens. marker is ab(INFO)
token.c:106:tds_process_default_tokens() marker is ab(INFO)
token.c:2640:tds_process_msg() reading message from server
read.c:178:tds_get_string: reading 38 from wire to give 38 to client.
read.c:178:tds_get_string: reading 9 from wire to give 9 to client.
token.c:2702:tds_process_msg() calling client msg handler
token.c:2715:tds_process_msg() returning TDS_SUCCEED
token.c:526:processing result tokens. marker is fd(DONE)
token.c:2344:tds_process_end: more_results = 0
was_cancelled = 0
error = 0
done_count_valid = 0
token.c:2360:tds_process_end() state set to TDS_IDLE
util.c:125:Changing query state from TDS_READING to TDS_IDLE
util.c:125:Changing query state from TDS_IDLE to TDS_PENDING
dblib.c:4070:dbsqlok() end status was success
dblib.c:1288:dbresults()
token.c:897:tds_process_row_tokens() state is COMPLETED
dblib.c:1054:dbcmd() bufsz = 12
dblib.c:5007:in dbfreebuf()
dblib.c:1104:in dbsqlexec()
dblib.c:5818:in dbsqlsend()
util.c:125:Changing query state from TDS_IDLE to TDS_QUERYING
mem.c:453:tds_free_all_results()
util.c:125:Changing query state from TDS_QUERYING to TDS_PENDING
net.c:650:Sending packet
0000 01 01 00 ed 00 00 00 00-49 4e 53 45 52 54 20 49 |........ INSERT

I|

0010 4e 54 4f 20 4a 6f 75 72-6e 65 79 20 28 4a 6f 75 |NTO Jour ney

(Jou|

0020 72 6e 65 79 53 74 61 72-74 54 69 6d 65 2c 53 74 |rneyStar

tTime,St|

0030 61 72 74 4c 61 74 69 74-75 64 65 2c 53 74 61 72 |artLatit

ude,Star|

0040 74 4c 6f 6e 67 69 74 75-64 65 2c 4f 64 6f 6d 65 |tLongitu

de,Odome|

0050 74 65 72 53 74 61 72 74-2c 56 65 68 69 63 6c 65 |terStart

,Vehicle|

0060 49 44 29 20 56 41 4c 55-45 53 20 28 43 4f 4e 56 |ID) VALU ES

(CONV|

0070 45 52 54 28 44 61 74 65-54 69 6d 65 2c 27 32 34 |ERT(Date

Time,'24|

0080 20 41 75 67 75 73 74 20-32 30 30 35 20 31 37 3a | August 2005

17:|

0090 30 34 3a 34 39 27 29 2c-43 4f 4e 56 45 52 54 28 |04:49'),

CONVERT(|

00a0 66 6c 6f 61 74 2c 27 35-33 2e 33 30 39 37 32 30 |float,'5

3.309720|

00b0 27 29 2c 43 4f 4e 56 45-52 54 28 66 6c 6f 61 74 |'),CONVE

RT(float|

00c0 2c 27 2d 32 2e 37 30 33-34 33 39 27 29 2c 43 4f |,'-2.703

439'),CO|

00d0 4e 56 45 52 54 28 69 6e-74 2c 27 31 31 36 32 35 |NVERT(in

t,'11625|

00e0 35 33 34 27 29 2c 27 31-31 31 30 27 29 |534'),'1 110')|

dblib.c:4005:in dbsqlok()
net.c:437:Received header
0000 04 01 00 11 00 00 00 00- |........|

net.c:531:Received packet
0000 fd 10 00 c3 00 01 00 00-00 |........ .|

dblib.c:4024:dbsqlok() marker is fd
util.c:125:Changing query state from TDS_PENDING to TDS_READING
token.c:526:processing result tokens. marker is fd(DONE)
token.c:2344:tds_process_end: more_results = 0
was_cancelled = 0
error = 0
done_count_valid = 1
token.c:2360:tds_process_end() state set to TDS_IDLE
util.c:125:Changing query state from TDS_READING to TDS_IDLE
token.c:2376: rows_affected = 1
util.c:125:Changing query state from TDS_IDLE to TDS_PENDING
dblib.c:4070:dbsqlok() end status was success
dblib.c:1288:dbresults()
dblib.c:1054:dbcmd() bufsz = 230
dblib.c:5007:in dbfreebuf()
dblib.c:1104:in dbsqlexec()
dblib.c:5818:in dbsqlsend()
util.c:125:Changing query state from TDS_IDLE to TDS_QUERYING
mem.c:453:tds_free_all_results()
util.c:125:Changing query state from TDS_QUERYING to TDS_PENDING
net.c:650:Sending packet
0000 01 01 00 25 00 00 00 00-53 45 4c 45 43 54 20 40 |...%.... SELECT

@|

0010 40 49 44 45 4e 54 49 54-59 20 41 53 20 49 6e 73 |@IDENTIT Y AS

Ins|

0020 65 72 74 49 44 - |ertID|

dblib.c:4005:in dbsqlok()
net.c:437:Received header
0000 04 01 00 30 00 00 00 00- |...0....|

net.c:531:Received packet
0000 a0 09 00 08 49 6e 73 65-72 74 49 44 a1 06 00 19 |....Inse

rtID....|

0010 00 01 00 6d 08 d1 08 00-00 00 00 00 80 43 40 fd |...m....

.....C@.|

0020 10 00 c1 00 01 00 00 00- |........|

dblib.c:4024:dbsqlok() marker is a0
dblib.c:4030:dbsqlok() found result token
dblib.c:1288:dbresults()
mem.c:453:tds_free_all_results()
util.c:125:Changing query state from TDS_PENDING to TDS_READING
token.c:526:processing result tokens. marker is a0(COLNAME)
read.c:178:tds_get_string: reading 8 from wire to give 8 to client.
mem.c:453:tds_free_all_results()
token.c:526:processing result tokens. marker is a1(COLFMT)
token.c:1256:processing result. type = 109(float-null), varint_size 1
util.c:125:Changing query state from TDS_READING to TDS_PENDING
dblib.c:1326:dbresults() process_result_tokens returned result_type =



4049 retcode = 1
util.c:125:Changing query state from TDS_PENDING to TDS_READING
token.c:526:processing result tokens. marker is d1(ROW)
util.c:125:Changing query state from TDS_READING to TDS_PENDING
dblib.c:1326:dbresults() process_result_tokens returned result_type =



4040 retcode = 1
dblib.c:2091:dbbind() column = 1 8 0
dblib.c:2123:dbbind() srctype = 62 desttype = 56
convert.c:2887:tds_willconvert()
convert.c:2891:tds_willconvert() 62 56 1
dblib.c:1545:dbnextrow()
dblib.c:1560:dbnextrow() dbresults_state = 2
util.c:125:Changing query state from TDS_PENDING to TDS_READING
token.c:907:processing row tokens. marker is d1(ROW)
token.c:2071:processing row. column is 0 varint size = 1
token.c:2128:processing row. column size is 8
util.c:125:Changing query state from TDS_READING to TDS_PENDING
dblib.c:6244:copy_data_to_host_var(62 [SYBFLT8] len 8 => 56 [SYBINT4]



len 0)
dblib.c:6387:copy_data_to_host_var() called tds_convert returned 4

HTH
_______________________________________________
FreeTDS mailing list
FreeTDS AT lists.ibiblio.org
http://lists.ibiblio.org/mailman/listinfo/freetds



_______________________________________________
FreeTDS mailing list
FreeTDS AT lists.ibiblio.org
http://lists.ibiblio.org/mailman/listinfo/freetds
--------------------------------------------------------

If you are not an intended recipient of this e-mail, please notify the
sender, delete it and do not read, act upon, print, disclose, copy, retain or
redistribute it. Click here for important additional terms relating to this
e-mail. http://www.ml.com/email_terms/
--------------------------------------------------------
_______________________________________________
FreeTDS mailing list
FreeTDS AT lists.ibiblio.org
http://lists.ibiblio.org/mailman/listinfo/freetds






Archive powered by MHonArc 2.6.24.

Top of Page