PK Name Collision On Temp Tables

  • So, of course multiple sessions can have temporary tables with the same name at the same time in tempdb because SQL Server "uniquifies" the table names internally. Problem is, I'm using primary keys on a temporary table that may exist on multiple sessions simultaneously, and SQL Server doesn't uniquify my PK constraint names, so only the first session creating the table runs successfully.

    My best shot at this (and someone tell me if I'm wrong) is using the default name for my PK constraint, like CREATE TABLE #Blah (Col1 int PRIMARY KEY). This works just fine, except that I have multi-column PKs, like so:

    CREATE TABLE #Blah (

    Col1 datetime,

    Col2 int,

    CONSTRAINT [constraint_name] PRIMARY KEY (Col1, Col2)

    )

    However, the above code fails without an explicit constraint name. Is there any way to get a default constraint name on each session for my multi-column PKs on temp tables? Or is there just a better way to accomplish what I'm trying to do?

    Thanks!

  • Try this -

    create table #blah

    (col1 int,

    col2 varchar(100),

    primary key (col1, col2)

    )

  • Rajan John (8/25/2008)


    Try this -

    create table #blah

    (col1 int,

    col2 varchar(100),

    primary key (col1, col2)

    )

    Beautiful. Thank you!!

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply