Groups | Blog | Home
all groups > sql server programming > january 2007 >

sql server programming : Temp table problem


jack
1/6/2007 10:17:26 PM
Hi All,
Im creating a temporary table in a procedure in an if condition but it
is giving a error like table already created.
What i want is that depending upon the condition the result in the
table data should be displayed. below is the procedure but gives me
error.

-------------------------------------------
create procedure usps_getTrans
txn_yyyymm int
as

if (txn_yyyymm =200607)
begin
select txn_yyyymm, UnitCode, Amount
into #temp from Transaction
where txn_yyyymm = 200607
end

if (txn_yyyymm =200608)
begin
select txn_yyyymm, UnitCode, GLCode Amount
into #temp from Transaction
where txn_yyyymm = 200608
end

select txn_yyyymm, UnitCode, Amount from #temp
-------------------------------------------
Server: Msg 2714, Level 16, State 1, Procedure usps_getTrans, Line 15
There is already an object named '#temp' in the database.
Server: Msg 170, Level 15, State 1, Procedure usps_getTrans, Line 2
Line 2: Incorrect syntax near 'txn_yyyymm'.
Server: Msg 156, Level 15, State 1, Procedure usps_getTrans, Line 8
Incorrect syntax near the keyword 'Transaction'.


The error shows that the table is already created. Im not able to find
the solution for this as i want to create only one temp table rather
than creating two tables.

Some one suggested me to do truncate table before filling the second
table in if condition. will this help.



Thanks for replying me..
jack
1/7/2007 1:33:01 AM
I tried using below statement but couldnt make it

if object_idn('#temp') is not null
drop table #temp

still giving me the same error.

Could you please explain me more about. How can i implement into
statement in this situation

Thanks for replying me

Uri Dimant
Uri Dimant
1/7/2007 9:50:11 AM
jack

One method is create a temporary table at the begining of stored procedure
and using
INSERT INTO .....

But if the load is huge SELECT INTO... will defenitely improve performance ,
so you can use

if object_idn('#temp') is not null
drop table #temp

if (txn_yyyymm =200607)
begin
select txn_yyyymm, UnitCode, Amount
into #temp from Transaction
where txn_yyyymm = 200607
end




[quoted text, click to view]

Erland Sommarskog
1/7/2007 11:14:38 AM
jack (gautams.mail@gmail.com) writes:
[quoted text, click to view]

It should be object_id('tempdb..#temp') IS NOT NULL

However, I strongly recommend that you create the table with CREATE TABLE,
and stick with one definition within the procedure. If create the table
with different columns depending on conditions, you are walking a very
difficult path, and you are likely to run into compilation errors, as the
procedure is recompiled, particularly on SQL 2000.



--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
Uri Dimant
1/7/2007 1:29:06 PM
[quoted text, click to view]

Oops, thanks Erland


[quoted text, click to view]

jack
1/23/2007 9:57:29 AM
Thanks i have done it

Used a table variable

Created a temporaty table in the procedure and implements insert into
select statement which worked..

and even in this case the scope of the table is within the procedure so
there is no need to drop the table or check whether it exist in the
database..

Thanks to all..
AddThis Social Bookmark Button