Groups | Blog | Home
all groups > sql server (alternate) > february 2004 >

sql server (alternate) : insert into table-Primary key error


cliff NO[at]SPAM walkacrossfire.com
2/6/2004 10:03:05 AM
I'm trying to do multiple insert statements. The table looks like
this:

CREATE TABLE $table (CNTY_CNTRY_CD char(3),ST char(2),
CNTY_CNTRY_DESCR varchar(50),CNTY_CNTRY_IND char(1),
HOME_CNTRY_IND char(1),
CONSTRAINT cnty_key PRIMARY KEY (CNTY_CNTRY_CD, ST))
I'm using 2 fields for the primary key constraint

My insert statement looks like this:

INSERT INTO $table
(CNTY_CNTRY_CD,ST,CNTY_CNTRY_DESCR)
VALUES(?,?,?)

I've been through the list of values and none have both the same
CNTY_CNTRY_CD and ST and yet, this is the error message I'm getting:

DBD::ODBC::st execute failed: [Microsoft][ODBC SQL Server Driver][SQL
Server]Vio
lation of PRIMARY KEY constraint 'cnty_key'. Cannot insert duplicate
key in obje
ct 'event_CNTY_CNTRY_CD'. (SQL-23000)
[Microsoft][ODBC SQL Server Driver][SQL Server]The statement has been
terminated
.. (SQL-01000)(DBD: st_execute/SQLExecute err=-1)

Why is it looking for unique in just the one column instead of
Simon Hayes
2/6/2004 7:19:46 PM

[quoted text, click to view]

Why do you think it's only looking one column? The error says the object (ie
the table) is called "event_CNTY_CNTRY_CD", so perhaps you're confusing that
with the column "CNTY_CNTRY_CD"? Or perhaps that wasn't the table name you
intended to use?

In any case, the error certainly indicates duplicate data - you could try
modifying your script to print out each INSERT statement for debugging
purposes, to be sure that you're really sending the values you think you
are. Or use Profiler to view the SQL sent to the server.

Simon

AddThis Social Bookmark Button