Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Sql Server 2005, Primary Key on Temp. table

Posted on 2008-10-02
4
Medium Priority
?
1,903 Views
Last Modified: 2012-05-05
I get an error "There is already an object named 'PK_#temp' in the database". because i have created a primary key on temp. table in a stored proc and 2 or multiple users run it at the same time i get above error message. If only one user would run the SP then there is no problem.
Challenge is how to fix this.
Question, can't there be Primary key on the temp table. If so can Sql server 2005 manage if i dont use primary key name or how does it work. Let me know. See the below code from the SP.


--this temp table created on the fly in the SP. 2 users run the SP 
--sametime is causing it to fail or should i do check if table(&PK) 
--exists at first place and then create table.. Let me know.
Create table #tempMain
(
SrOfferID Int NOT NULL CONSTRAINT [DF_#tempMain_SrOfferID]  DEFAULT (0), 
SrOfferScheduleID  Int NOT NULL CONSTRAINT [DF_#tempMain_SrOfferScheduleID]  DEFAULT (0), 
FacultyID Int NOT NULL CONSTRAINT [DF_#tempMain_FacultyID]  DEFAULT (0), 
StartDate  Datetime,
EndDate  Datetime,
OfferDays char(10),
ShowDay  char(3000),
ShowMonth char(3000)
CONSTRAINT [PK_#tempMain] PRIMARY KEY CLUSTERED 
(
[SrOfferID] ASC,
[SrOfferScheduleID] ASC,
[FacultyID] ASC
) ON [PRIMARY]
) ON [PRIMARY]

Open in new window

0
Comment
Question by:onebite2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 

Author Comment

by:onebite2
ID: 22624462
Forgot to tell this.. I already do the checking before creating temp table..
--Drop #temp table if exists
 
IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[PK_#tempMain]') AND type = 'PK')
	Begin
	ALTER TABLE [dbo].[#tempMain] DROP CONSTRAINT [PK_#tempMain]
	End 
 
If exists (select  * from tempdb.dbo.sysobjects o where o.xtype in ('U')  and o.id = object_id(N'tempdb..#tempMain'))
	Begin
	Drop table #tempMain	
	End 
--After this i create the table as in above code snippet

Open in new window

0
 
LVL 60

Accepted Solution

by:
chapmandew earned 2000 total points
ID: 22624501
you shouldn't need to drop the constraint first...try this:

if object_id('tempdb..#tempMain') is not null
drop table #tempmain

create table...
0
 

Author Comment

by:onebite2
ID: 22624709
That works chapmandew, so that means when i drop a temp table. Primary Key constraint on the temp table will be dropped automatically??? or explain me what's my mistake.
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 22624735
yep, when you drop a table, the constraints are dropped w/ it.
0

Featured Post

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question