SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Union query to temp table


Union query to temp table

Author
Message
s.green
s.green
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 18
Hi all,

I've written a union query to extact data from our ERP solution but want to write this into a temporary table for further manipulation. What's the best way of doing this?

My query thus far is;


Use Evision_Latest

Declare @Contract varchar(50)
Declare @EndDate varchar(50)

Select @Contract = 'CW0455'
Select @EndDate = '2010-10-31'

Select
j.[Job No_] 'ContractNumber',
j.[Posting Date] as 'PostingDate',
j.[Total Price]+j.[Total Cost] as 'Amount',
j.[Global Dimension 2 Code] as 'CostCode',
'JNL01' as 'SourceCode',
d.[Name] as 'CostCodeName',
'Journal' as 'SourceName',
j.[Shortcut Dimension 6 Code] as 'ResourceCode',
j.[Global Dimension 2 Code] + j.[Shortcut Dimension 6 Code] as 'Key'
from
[Swift horsman Ltd$Job Ledger Entry] as j
inner join
[Swift horsman Ltd$Dimension Value] as d
on j.[Global Dimension 2 code] = d.[code]
where
j.[Job No_] = @Contract
and
j.[Posting Date] >= @EndDate
and
j.[Source Type] = '4'
and
j.[Global Dimension 2 Code] >= '10000'
and
j.[Global Dimension 2 Code] <= '99999'
UNION ALL
Select
j.[Job No_] 'ContractNumber',
j.[Posting Date] as 'PostingDate',
j.[Total Price]+j.[Total Cost] as 'Amount',
j.[Global Dimension 2 Code] as 'CostCode',
'LAB01' as 'SourceCode',
d.[Name] as 'CostCodeName',
'Direct Labour' as 'SourceName',
'' as 'ResourceCode',
j.[Global Dimension 2 Code] + 'LAB01' as 'Key'
from
[Swift horsman Ltd$Job Ledger Entry] as j
inner join
[Swift horsman Ltd$Dimension Value] as d
on j.[Global Dimension 2 code] = d.[code]
where
j.[Job No_] = @Contract
and
j.[Posting Date] >= @EndDate
and
j.[Source Type] = '0'
and
j.[Global Dimension 2 Code] >= '10000'
and
j.[Global Dimension 2 Code] <= '99999'
UNION ALL
Select
j.[Job No_] 'ContractNumber',
j.[Posting Date] as 'PostingDate',
j.[Total Price]+j.[Total Cost] as 'Amount',
j.[Global Dimension 2 Code] as 'CostCode',
j.[Source no_] as 'SourceCode',
d.[Name] as 'CostCodeName',
s.[Name] as 'SourceName',
'' as 'ResourceCode',
j.[Global Dimension 2 Code] + j.[Source no_] as 'Key'
from
[Swift horsman Ltd$Job Ledger Entry] as j
inner join
[Swift horsman Ltd$Dimension Value] as d
on j.[Global Dimension 2 code] = d.[code]
inner join
[Swift Horsman Ltd$Subcontractor] as s
on s.[No_] =j.[Source No_]
where
j.[Job No_] = @Contract
and
j.[Posting Date] >= @EndDate
and
j.[Source Type] = '3'
and
j.[Global Dimension 2 Code] >= '10000'
and
j.[Global Dimension 2 Code] <= '99999'
UNION ALL
Select
j.[Job No_] 'ContractNumber',
j.[Posting Date] as 'PostingDate',
j.[Total Price]+j.[Total Cost] as 'Amount',
j.[Global Dimension 2 Code] as 'CostCode',
j.[Source no_] as 'SourceCode',
d.[Name] as 'CostCodeName',
v.[Name] as 'SourceName',
'' as 'ResourceCode',
j.[Global Dimension 2 Code] + j.[Source no_] as 'Key'
from
[Swift horsman Ltd$Job Ledger Entry] as j
inner join
[Swift horsman Ltd$Dimension Value] as d
on j.[Global Dimension 2 code] = d.[code]
inner join
[Swift Horsman Ltd$Vendor] as v
on v.[No_] =j.[Source No_]
where
j.[Job No_] = @Contract
and
j.[Posting Date] >= @EndDate
and
j.[Source Type] = '2'
and
j.[Global Dimension 2 Code] >= '10000'
and
j.[Global Dimension 2 Code] <= '99999'
Order by
j.[Global Dimension 2 Code] asc

Appreciate any advice.

Thanks,

Simon
ALZDBA
ALZDBA
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12442 Visits: 8930
Best is to create your temp table and then perform a regular insert into #temptb select .... yourunionquery

Johan


Dont drive faster than your guardian angel can fly ...
but keeping both feet on the ground wont get you anywhere w00t

- How to post Performance Problems
- How to post data/code to get the best help


- How to prevent a sore throat after hours of presenting ppt ?


press F1 for solution, press shift+F1 for urgent solution :-D


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me Alien but most of the time this is me Hehe
s.green
s.green
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 18
Thanks for the post.
Lowell
Lowell
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28624 Visits: 39977
for reference though, the format is not too difficult: it's INTO #Tablename just before the first from

SELECT
ColumnList
INTO #TMP
FROM ATable
JOIN OtherTables ON ...
UNION
SELECT columnList
FromBTable



Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

ALZDBA
ALZDBA
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12442 Visits: 8930
Lowell (3/14/2011)
for reference though, the format is not too difficult: it's INTO #Tablename just before the first from

SELECT
ColumnList
INTO #TMP
FROM ATable
JOIN OtherTables ON ...
UNION
SELECT columnList
FromBTable



Keep in mind, doing it this way may cause a lock on tempdb system tables for the whole duration of the query.
That's why I suggested to first create the temp table.

Tempdb is used by everyone that uses your instance !
(order by / group by / temp tb/ tb var / query set materialisation, ....)

Johan


Dont drive faster than your guardian angel can fly ...
but keeping both feet on the ground wont get you anywhere w00t

- How to post Performance Problems
- How to post data/code to get the best help


- How to prevent a sore throat after hours of presenting ppt ?


press F1 for solution, press shift+F1 for urgent solution :-D


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me Alien but most of the time this is me Hehe
samjoseph_p
samjoseph_p
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 8
ya... Good Answer... Lowell
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search