Solved

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

Posted on 2011-02-15
6
570 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now