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
 Insert into temp results of a Union Query

Author  Topic 

chiman

21 Posts

Posted - 2007-11-20 : 15:25:51
Hi,
I have follwing union query. I want to put this all in a temp table.

select Store_Id,batchnumber
From
Adjustments where updatedDt between '10/30/2007' and '11/20/2007' and Store_id in(8637 ,8641)
group by Store_Id, batchnumber
Union
select DestinationId,b.batchNumber
from
batch b
inner join Carton C on C.Carton_Id = b.General_ID
inner join Document d on d.Document_Id = c.Document_Id
where b.BatchType = 'Warehouse' and b.TranTable = 'Carton'
and (d.DestinationId in (8637 ,8641) ) and c.UpdatedDt Between '10/30/2007' and '11/20/2007'
Union
select d.DestinationId,b.Batchnumber
From
batch b
inner join Document d
on d.Document_Id = b.General_Id
where b.BatchType = 'TransferIn' and b.TranTable = 'Document'
and (d.DestinationId in (8637,8641) ) and d.UpdatedDt Between '10/30/2007' and '11/20/2007'
Union
select d.SourceId,b.batchNumber
From
batch b
inner join Document d
on d.Document_Id = b.General_Id
where b.BatchType = 'TransferOut' and b.TranTable = 'Document'
and (d.SourceId in (8637,8641) ) and d.UpdatedDt Between '10/30/2007' and '11/20/2007'
order by batchnumber

Kindly advice.

Thanks
Renu

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-11-20 : 15:28:42
CREATE TABLE #temp (...)

INSERT INTO #temp
YourQueryGoesHere

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

chiman

21 Posts

Posted - 2007-11-20 : 15:51:35
hey thanks a lot..
I was trying to do it this way..

Select Store_Id, BatchNumber
into #temp from <tablename>
Union..

Thanks again...
Go to Top of Page
   

- Advertisement -