Link to home
Start Free TrialLog in
Avatar of chokka
chokkaFlag for United States of America

asked on

SQL : How to insert couple of Table records to a TempTable ?

SQL 2008

How to insert couple of Table records to a TempTable ?

I have Table1, Table2.

I need to move both Tables to a Temp Table, in which i can do some calculation.



Avatar of eljakim
eljakim

You can create a table as follows:

create tmptable select * from table1;

If you need to have several extra fields, an easy way to do this would be:

create tmptable select *, 1 as extra_field from table1;

When you want to get rid of the table:

drop table tmptable;
Avatar of chokka

ASKER

I need to insert two Tables to one Temp Table
ASKER CERTIFIED SOLUTION
Avatar of eljakim
eljakim

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chokka

ASKER

I did it in this way
declare @TempTable table
( 
	NDC nvarchar(1000), 
	Qty nvarchar(1000) 
)

Insert Into @TempTable (NDC,Qty)
select		
			
			TR.NDC,   
			Sum(TR.[Dispensed Qty]) as Qty
from		TransactionReport TR    
Group By    TR.NDC

Insert Into @TempTable (NDC,Qty)
select BR.NDC,BR.Qty  from BalanceReport BR


select NDC,Sum(CONVERT(INT, Qty)) as Qty from  @TempTable
Group By NDC

Open in new window