Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 help me :

Author  Topic 

ducletan
Starting Member

25 Posts

Posted - 2010-12-15 : 04:13:17
if @id=1 then select * into #test from table1
else select * into #test from table2

There is already an object #test in the database

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-12-15 : 05:45:18
drop table #test


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2010-12-15 : 06:15:25
That IF structure will never work, the parser sees the first select into and throws an error on the second believing the table exists.

Is the structure of Table1 and Table2 the same?

--
Gail Shaw
SQL Server MVP
Go to Top of Page

SparkByte
Yak Posting Veteran

60 Posts

Posted - 2010-12-15 : 10:41:41
This should give you a good start.

Hope it helps.



Create Table #test
(
PK Int,
ID Int,
Col1 Varchar(15),
col2 Varchar(15),
col3 DateTime
)

Create Table #Table1
(
PK Int,
ID Int,
Col1 Varchar(15),
col2 Varchar(15),
col3 DateTime
)

Insert Into #Table1 Values (1,11,'Test data','More Data',N'01/01/2010')
Insert Into #Table1 Values (2,12,'Test data','More Data',N'01/02/2010')
Insert Into #Table1 Values (3,13,'Test data','More Data',N'01/03/2010')
Insert Into #Table1 Values (4,14,'Test data','More Data',N'01/04/2010')
Insert Into #Table1 Values (5,15,'Test data','More Data',N'01/05/2010')
Insert Into #Table1 Values (6,16,'Test data','More Data',N'01/06/2010')
Insert Into #Table1 Values (7,17,'Test data','More Data',N'01/07/2010')
Insert Into #Table1 Values (8,18,'Test data','More Data',N'01/08/2010')
Insert Into #Table1 Values (9,19,'Test data','More Data',N'01/09/2010')

Create Table #Table2
(
PK Int,
ID Int,
Col1 Varchar(15),
col2 Varchar(15),
col3 DateTime
)

Insert Into #Table2 Values (1,21,'Test data','More Data',N'01/01/2010')
Insert Into #Table2 Values (2,22,'Test data','More Data',N'01/02/2010')
Insert Into #Table2 Values (3,23,'Test data','More Data',N'01/03/2010')
Insert Into #Table2 Values (4,24,'Test data','More Data',N'01/04/2010')
Insert Into #Table2 Values (5,25,'Test data','More Data',N'01/05/2010')
Insert Into #Table2 Values (6,26,'Test data','More Data',N'01/06/2010')
Insert Into #Table2 Values (7,27,'Test data','More Data',N'01/07/2010')
Insert Into #Table2 Values (8,28,'Test data','More Data',N'01/08/2010')
Insert Into #Table2 Values (9,29,'Test data','More Data',N'01/09/2010')


Declare @id int
Set @id=0


If @id=1
Begin
Insert into #test (PK, ID, Col1, col2, col3)
Select PK, ID, Col1, col2, col3 from #Table1
End
Else
Begin
Insert into #test (PK, ID, Col1, col2, col3)
Select PK, ID, Col1, col2, col3 from #Table2
End


Select * from #test

Drop Table #test
Drop Table #Table1
Drop Table #Table2


Thank You,

John
Go to Top of Page
   

- Advertisement -