Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating Temptable in Stored Procedure

Status
Not open for further replies.

mydisney

Programmer
May 7, 2007
55
US
I have a stored procedure that creates a temp table (see code below).

1. How can I drop the table if it exists?
2. Also I have define a field called "job_date" and I get an error if I specify 'date' as type (so I changes it to 'int')?

create table #jobtemptable
(
job_date int,
job_number char(15),
job_phase char(15),
asphalt_delivered int,
job_received int
)
GO
 
1.)
if exists (
select *
from
tempdb.dbo.sysobjects o
where
o.xtype in ('U') and
o.id = object_id( N'tempdb..'+'#jobtemptable' )

drop #jobtemptable

2.) There is no date type in sql server, it is datetime.
 
I get a syntax error: Incorrect syntax near keyword "drop" ...

CREATE PROCEDURE sp_CreateJobTempTable
as

if exists (
select *
from
tempdb.dbo.sysobjects o
where
o.xtype in ('U') and
o.id = object_id(N'tempdb..'+'#jobtemptable' )
drop #jobtemptable

create table #jobtemptable
(
job_date datetime,
job_number char(15),
job_phase char(15),
asphalt_delivered int,
job_received int
)
GO
 
There's a missing 'close parenthesis'. Can you find it?

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
I tried to place the missing "parenthesis" after the '#jobtemplate' but still get syntax error.
 
There is a parenthesis missing and the drop temp table line isn't right either. Try this...

Code:
if exists (
    select  *
    from
        tempdb.dbo.sysobjects o
    where
        o.xtype in ('U')    and
        o.id = object_id( N'tempdb..'+'#jobtemptable' )
      [!])[/!]

drop [!]table[/!] #jobtemptable

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
You're welcome.

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top