Solved

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

Posted on 2011-02-15
6
575 Views
Last Modified: 2012-05-11
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.



0
Comment
Question by:chokka
6 Comments
 
LVL 2

Expert Comment

by:eljakim
ID: 34901217
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;
0
 

Author Comment

by:chokka
ID: 34901234
I need to insert two Tables to one Temp Table
0
 
LVL 2

Accepted Solution

by:
eljakim earned 167 total points
ID: 34901273
do they have the same structure?

create table tmptable select * from table1 union select * from table2;

If they don't have the same structure you have to specify the field names.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 23

Assisted Solution

by:wdosanjos
wdosanjos earned 167 total points
ID: 34901309
You can also prefix the table name with a # sign to create a local temporary table (for example, #TempTable).  They have better performance than regular tables.

More on temporary tables below:

http://msdn.microsoft.com/en-us/library/ms177399.aspx

Temporary tables are similar to permanent tables, except temporary tables are stored in tempdb and are deleted automatically when they are no longer used.

There are two types of temporary tables: local and global. They differ from each other in their names, their visibility, and their availability. Local temporary tables have a single number sign (#) as the first character of their names; they are visible only to the current connection for the user, and they are deleted when the user disconnects from the instance of SQL Server. Global temporary tables have two number signs (##) as the first characters of their names; they are visible to any user after they are created, and they are deleted when all users referencing the table disconnect from the instance of SQL Server.
0
 
LVL 51

Assisted Solution

by:HainKurt
HainKurt earned 166 total points
ID: 34901324
you can put them into two seperate temp table

and why are you using this? is the calculation too complicated? maybe we can do it one query without need of temp table/cursors...

what are you trying to do actually?
0
 

Author Comment

by:chokka
ID: 34901341
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

0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question