in reply to Sql problem in Win32::ODBC
But if the ID column is an identity column then all you have to do is this (assuming type and name together also uniquely identify a row (they're an alternate or candidate key)):IF NOT EXISTS (SELECT * FROM ENTITIES WHERE ID = $MaxEntityId) INSERT INTO ENTITIES (ID,TYPE,NAME) VALUES ($MaxEntityID, '$type', '$name')
Making the ID column have the identity property you can reduce your perl code to this:IF NOT EXISTS (SELECT * FROM ENTITIES WHERE TYPE = $type AND NAME = $name) INSERT INTO ENTITIES (TYPE, NAME) VALUES ('$type', '$name')
Another problem can be that your script doesn't have permission to modify the table. Set the permission on the ENTITIES table to public and try your script again. If it works then you can decide what the correct table permissions should be.################ NOTE: UNTESTED CODE use strict; use warnings; use Win32::ODBC; my $dB = new Win32::ODBC("DSN=name;UID=user;PWD=password;"); die 'Cannot connect to dB.' if !$dB; my %tests = ( COUNTRY => 'UNITED_STATES', PERSON => 'FRED', PERSON => 'JOE' ); my ($type, $name); while (($type, $name) = each %tests) { my $query = qq( IF NOT EXISTS ( SELECT * FROM ENTITIES WHERE TYPE = '$type' AND NAME = '$name' ) INSERT INTO ENTITIES (TYPE, NAME) VALUES ('$type', '$name') ); my $rc = $dB->Sql($query); die "Query failed: $query ".Win32::ODBC::Error() if $rc; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Sql problem in Win32::ODBC
by fletcher_the_dog (Friar) on Aug 07, 2003 at 18:05 UTC | |
by monktim (Friar) on Aug 07, 2003 at 18:33 UTC |