create procedure proc1 (@a int)
as
begin
if @a=1
create table #temp(a1 int)
else
create table #temp(a1 int)
end

while executeing the above code it shows error
like '#temp already exist' .why it shows an error?

Answers were Sorted based on User's Feedback



create procedure proc1 (@a int) as begin if @a=1 create table #temp(a1 int) else ..

Answer / saravanan p

Since the stored procedure precompiled, we cannot give same
table name in both the If and else part.

It will work if give some other name in else part.

Is This Answer Correct ?    11 Yes 2 No

create procedure proc1 (@a int) as begin if @a=1 create table #temp(a1 int) else ..

Answer / soorai ganesh

Problem in #temp table. Because it comes twice in SP.
(U can say it is in else part. But compiler will not accept
this.First it will compile everthing then running the SP. )

Its better to keep another name for Else part table.......

Is This Answer Correct ?    8 Yes 6 No

create procedure proc1 (@a int) as begin if @a=1 create table #temp(a1 int) else ..

Answer / lee

yep problem is with the compiler.. I had to think of
another way to solve the problem, eg, I created the #temp
without the IF statement and then used an IF ELSE statement
to modify or update #temp, make sense?

Is This Answer Correct ?    0 Yes 0 No

create procedure proc1 (@a int) as begin if @a=1 create table #temp(a1 int) else ..

Answer / madhu sudhan g

Hiiii


in Stored Procedures we cannot create same temp table twice if it is in IF else conditions also

Is This Answer Correct ?    0 Yes 0 No

create procedure proc1 (@a int) as begin if @a=1 create table #temp(a1 int) else ..

Answer / murthy

The #Temp table created in this stores procedure exists in
memory as long as the Particular session is closed. Hence
drop the table at the end of the procedure.

Note: In case if you exec the SP using Management studio,
close the connection and re-open it, it will work without
modifying anything.

Is This Answer Correct ?    1 Yes 3 No

create procedure proc1 (@a int) as begin if @a=1 create table #temp(a1 int) else ..

Answer / utpal chakraboarty

Because, Stored procedure is pre-compiled language.

Is This Answer Correct ?    4 Yes 12 No

create procedure proc1 (@a int) as begin if @a=1 create table #temp(a1 int) else ..

Answer / david

it indicates that some one had already executed the stored
proc but forgotten to delete the temp table they have
created.

normally before closing that particular transaction within
the SP we need to use drop table statement.

Is This Answer Correct ?    1 Yes 10 No

create procedure proc1 (@a int) as begin if @a=1 create table #temp(a1 int) else ..

Answer / gaurav

Because temp is being used by database internally so that it
is same as a system table so this table can't create by user...

Is This Answer Correct ?    1 Yes 14 No

Post New Answer

More SQL Server Interview Questions

How can I create a report based on a query? : sql server management studio

0 Answers  


What are the new features introduced in SQL Server 2000 (or the latest release of SQL Server at the time of your interview)? What changed between the previous version of SQL Server and the current version?

0 Answers  


what is dbcc? : Sql server database administration

0 Answers  


how can you check the level of fragmentation on a table? : Sql server administration

0 Answers  


What is the difference between value type and reference type?

0 Answers  


How to change the ownership of a schema in ms sql server?

0 Answers  


What do you mean by a Composite primary key?

0 Answers   Genpact,


How to restart SQL Server in single user mode? How to start SQL Server in minimal configuration mode?

3 Answers   HCL,


How many types of relations are there between dimension and measure group? : sql server analysis services, ssas

0 Answers  


What is the role that is required for killing a process What is the role that is required for creating a linked server

1 Answers   CarrizalSoft Technologies, IBM,


Which are ddl commands?

0 Answers  


What is Report Server,Report Manager and Report Builder in SSRS 2005?

1 Answers  


Categories