dbTalk Databases Forums  

Combining tables

microsoft.public.sqlserver.dts microsoft.public.sqlserver.dts


Discuss Combining tables in the microsoft.public.sqlserver.dts forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Michael Vardinghus
 
Posts: n/a

Default Combining tables - 06-07-2004 , 01:56 PM






How would one go about doing this:

Table 1:

DimA Type FromDimB ToDimB
123 A 1 2
456 B 1 2
789 C 3 8

Table 2:

DimB Type DimA
1 A 123
2 A 123
2 B 456
3 C 789
4 C 789
5 C 789


For now there is no DimA column in table 2 so I have to make this either by
joining or cursor ? What would be the best approach - making a cursor on
Table 1 and updatering table 2 in steps ?

\Michael Vardinghus



Reply With Quote
  #2  
Old   
John Bell
 
Posts: n/a

Default Re: Combining tables - 06-07-2004 , 02:49 PM






Hi

You are usually better off with set based operations than cursors, therefore
JOIN Table1to table2

It is better to post DDL (CREATE Table statements) and example data as
insert statements rather than pseudo code. Expected output from the example
data is also useful.

John

"Michael Vardinghus" <michaelvardinghus (AT) notexisting (DOT) com> wrote

Quote:
How would one go about doing this:

Table 1:

DimA Type FromDimB ToDimB
123 A 1 2
456 B 1 2
789 C 3 8

Table 2:

DimB Type DimA
1 A 123
2 A 123
2 B 456
3 C 789
4 C 789
5 C 789


For now there is no DimA column in table 2 so I have to make this either
by
joining or cursor ? What would be the best approach - making a cursor on
Table 1 and updatering table 2 in steps ?

\Michael Vardinghus





Reply With Quote
  #3  
Old   
Michael Vardinghus
 
Posts: n/a

Default Re: Combining tables - 06-07-2004 , 03:06 PM



Think i got it solved by this - seems to work okay...i'm not updating in
this example - so the select will be replaced with an update sentence

USE um_dw_finance_staging



DECLARE @delregnskab int, @frasag int , @tilsag int, @titel varchar (40)



DECLARE led1_cursor CURSOR FOR

SELECT delregnskab, frasag, tilsag, titel FROM led1sortering

order by delregnskab, frasag, tilsag, titel



OPEN led1_cursor



FETCH NEXT FROM led1_cursor

into @delregnskab, @frasag, @tilsag, @titel



WHILE @@FETCH_STATUS = 0

BEGIN

select *, @titel from dbo.finansbudgetposter_staging post

where post.delregnskab = @delregnskab and post.sagsbudget >= @frasag and

post.sagsbudget <= @tilsag

FETCH NEXT FROM led1_cursor

into @delregnskab, @frasag, @tilsag, @titel

END



CLOSE authors_cursor

DEALLOCATE authors_cursor

GO



"Michael Vardinghus" <michaelvardinghus (AT) notexisting (DOT) com> skrev i en
meddelelse news:%23RkbWCMTEHA.3608 (AT) TK2MSFTNGP11 (DOT) phx.gbl...
Quote:
How would one go about doing this:

Table 1:

DimA Type FromDimB ToDimB
123 A 1 2
456 B 1 2
789 C 3 8

Table 2:

DimB Type DimA
1 A 123
2 A 123
2 B 456
3 C 789
4 C 789
5 C 789


For now there is no DimA column in table 2 so I have to make this either
by
joining or cursor ? What would be the best approach - making a cursor on
Table 1 and updatering table 2 in steps ?

\Michael Vardinghus





Reply With Quote
  #4  
Old   
John Bell
 
Posts: n/a

Default Re: Combining tables - 06-08-2004 , 04:38 AM



Hi

How about:

select p.*, l.titel
from dbo.finansbudgetposter_staging p JOIN dbo.led1sortering l
ON p.delregnskab = l.delregnskab
and p.sagsbudget >= l.frasag
and p.sagsbudget <= l.tilsag
order by l.delregnskab, l.frasag, l.tilsag, l.titel

A single result set will make the code that processes the results easier!

John

"Michael Vardinghus" <michaelvardinghus (AT) notexisting (DOT) com> wrote

Quote:
Think i got it solved by this - seems to work okay...i'm not updating in
this example - so the select will be replaced with an update sentence

USE um_dw_finance_staging



DECLARE @delregnskab int, @frasag int , @tilsag int, @titel varchar (40)



DECLARE led1_cursor CURSOR FOR

SELECT delregnskab, frasag, tilsag, titel FROM led1sortering

order by delregnskab, frasag, tilsag, titel



OPEN led1_cursor



FETCH NEXT FROM led1_cursor

into @delregnskab, @frasag, @tilsag, @titel



WHILE @@FETCH_STATUS = 0

BEGIN

select *, @titel from dbo.finansbudgetposter_staging post

where post.delregnskab = @delregnskab and post.sagsbudget >= @frasag
and

post.sagsbudget <= @tilsag

FETCH NEXT FROM led1_cursor

into @delregnskab, @frasag, @tilsag, @titel

END



CLOSE authors_cursor

DEALLOCATE authors_cursor

GO



"Michael Vardinghus" <michaelvardinghus (AT) notexisting (DOT) com> skrev i en
meddelelse news:%23RkbWCMTEHA.3608 (AT) TK2MSFTNGP11 (DOT) phx.gbl...
How would one go about doing this:

Table 1:

DimA Type FromDimB ToDimB
123 A 1 2
456 B 1 2
789 C 3 8

Table 2:

DimB Type DimA
1 A 123
2 A 123
2 B 456
3 C 789
4 C 789
5 C 789


For now there is no DimA column in table 2 so I have to make this either
by
joining or cursor ? What would be the best approach - making a cursor on
Table 1 and updatering table 2 in steps ?

\Michael Vardinghus







Reply With Quote
  #5  
Old   
Michael Vardinghus
 
Posts: n/a

Default Re: Combining tables - 06-08-2004 , 04:50 AM



Didn't think i could join like that...wouw...

And I couldn't aggree more....would avoid cursors if possible - didn't think
i could join...

\Michael V

"John Bell" <jbellnewsposts (AT) hotmail (DOT) com> skrev i en meddelelse
news:uQgeIwTTEHA.4048 (AT) TK2MSFTNGP12 (DOT) phx.gbl...
Quote:
Hi

How about:

select p.*, l.titel
from dbo.finansbudgetposter_staging p JOIN dbo.led1sortering l
ON p.delregnskab = l.delregnskab
and p.sagsbudget >= l.frasag
and p.sagsbudget <= l.tilsag
order by l.delregnskab, l.frasag, l.tilsag, l.titel

A single result set will make the code that processes the results easier!

John

"Michael Vardinghus" <michaelvardinghus (AT) notexisting (DOT) com> wrote in message
news:uZLZNpMTEHA.2416 (AT) TK2MSFTNGP09 (DOT) phx.gbl...
Think i got it solved by this - seems to work okay...i'm not updating in
this example - so the select will be replaced with an update sentence

USE um_dw_finance_staging



DECLARE @delregnskab int, @frasag int , @tilsag int, @titel varchar (40)



DECLARE led1_cursor CURSOR FOR

SELECT delregnskab, frasag, tilsag, titel FROM led1sortering

order by delregnskab, frasag, tilsag, titel



OPEN led1_cursor



FETCH NEXT FROM led1_cursor

into @delregnskab, @frasag, @tilsag, @titel



WHILE @@FETCH_STATUS = 0

BEGIN

select *, @titel from dbo.finansbudgetposter_staging post

where post.delregnskab = @delregnskab and post.sagsbudget >= @frasag
and

post.sagsbudget <= @tilsag

FETCH NEXT FROM led1_cursor

into @delregnskab, @frasag, @tilsag, @titel

END



CLOSE authors_cursor

DEALLOCATE authors_cursor

GO



"Michael Vardinghus" <michaelvardinghus (AT) notexisting (DOT) com> skrev i en
meddelelse news:%23RkbWCMTEHA.3608 (AT) TK2MSFTNGP11 (DOT) phx.gbl...
How would one go about doing this:

Table 1:

DimA Type FromDimB ToDimB
123 A 1 2
456 B 1 2
789 C 3 8

Table 2:

DimB Type DimA
1 A 123
2 A 123
2 B 456
3 C 789
4 C 789
5 C 789


For now there is no DimA column in table 2 so I have to make this
either
by
joining or cursor ? What would be the best approach - making a cursor
on
Table 1 and updatering table 2 in steps ?

\Michael Vardinghus









Reply With Quote
  #6  
Old   
John Bell
 
Posts: n/a

Default Re: Combining tables - 06-08-2004 , 05:54 AM



Hi

Time spent reading books online will repay itself ! You may also want to
check out Itzik's articles on http://www.winnetmag.com/SQLServer/ or
subscribe to the printed version!

His (and Tom's) book at:
http://search.barnesandnoble.com/boo...sbn=1893115828
is certainly one for every bookshelf and there are many others! A more
comprehensive list can be found at: http://www.aspfaq.com/show.asp?id=2423

John

"Michael Vardinghus" <michaelvardinghus (AT) notexisting (DOT) com> wrote

Quote:
Didn't think i could join like that...wouw...

And I couldn't aggree more....would avoid cursors if possible - didn't
think
i could join...

\Michael V

"John Bell" <jbellnewsposts (AT) hotmail (DOT) com> skrev i en meddelelse
news:uQgeIwTTEHA.4048 (AT) TK2MSFTNGP12 (DOT) phx.gbl...
Hi

How about:

select p.*, l.titel
from dbo.finansbudgetposter_staging p JOIN dbo.led1sortering l
ON p.delregnskab = l.delregnskab
and p.sagsbudget >= l.frasag
and p.sagsbudget <= l.tilsag
order by l.delregnskab, l.frasag, l.tilsag, l.titel

A single result set will make the code that processes the results
easier!

John

"Michael Vardinghus" <michaelvardinghus (AT) notexisting (DOT) com> wrote in
message
news:uZLZNpMTEHA.2416 (AT) TK2MSFTNGP09 (DOT) phx.gbl...
Think i got it solved by this - seems to work okay...i'm not updating
in
this example - so the select will be replaced with an update sentence

USE um_dw_finance_staging



DECLARE @delregnskab int, @frasag int , @tilsag int, @titel varchar
(40)



DECLARE led1_cursor CURSOR FOR

SELECT delregnskab, frasag, tilsag, titel FROM led1sortering

order by delregnskab, frasag, tilsag, titel



OPEN led1_cursor



FETCH NEXT FROM led1_cursor

into @delregnskab, @frasag, @tilsag, @titel



WHILE @@FETCH_STATUS = 0

BEGIN

select *, @titel from dbo.finansbudgetposter_staging post

where post.delregnskab = @delregnskab and post.sagsbudget >=
@frasag
and

post.sagsbudget <= @tilsag

FETCH NEXT FROM led1_cursor

into @delregnskab, @frasag, @tilsag,
@titel

END



CLOSE authors_cursor

DEALLOCATE authors_cursor

GO



"Michael Vardinghus" <michaelvardinghus (AT) notexisting (DOT) com> skrev i en
meddelelse news:%23RkbWCMTEHA.3608 (AT) TK2MSFTNGP11 (DOT) phx.gbl...
How would one go about doing this:

Table 1:

DimA Type FromDimB ToDimB
123 A 1 2
456 B 1 2
789 C 3 8

Table 2:

DimB Type DimA
1 A 123
2 A 123
2 B 456
3 C 789
4 C 789
5 C 789


For now there is no DimA column in table 2 so I have to make this
either
by
joining or cursor ? What would be the best approach - making a
cursor
on
Table 1 and updatering table 2 in steps ?

\Michael Vardinghus











Reply With Quote
  #7  
Old   
Michael Vardinghus
 
Posts: n/a

Default Re: Combining tables - 06-08-2004 , 06:22 AM



Actually bougth sql for gurus....perhaps i should try to read it...

"John Bell" <jbellnewsposts (AT) hotmail (DOT) com> skrev i en meddelelse
news:uIHEkaUTEHA.1472 (AT) TK2MSFTNGP09 (DOT) phx.gbl...
Quote:
Hi

Time spent reading books online will repay itself ! You may also want to
check out Itzik's articles on http://www.winnetmag.com/SQLServer/ or
subscribe to the printed version!

His (and Tom's) book at:

http://search.barnesandnoble.com/boo...sbn=1893115828
is certainly one for every bookshelf and there are many others! A more
comprehensive list can be found at:
http://www.aspfaq.com/show.asp?id=2423

John

"Michael Vardinghus" <michaelvardinghus (AT) notexisting (DOT) com> wrote in message
news:uWsJ41TTEHA.3548 (AT) TK2MSFTNGP09 (DOT) phx.gbl...
Didn't think i could join like that...wouw...

And I couldn't aggree more....would avoid cursors if possible - didn't
think
i could join...

\Michael V

"John Bell" <jbellnewsposts (AT) hotmail (DOT) com> skrev i en meddelelse
news:uQgeIwTTEHA.4048 (AT) TK2MSFTNGP12 (DOT) phx.gbl...
Hi

How about:

select p.*, l.titel
from dbo.finansbudgetposter_staging p JOIN dbo.led1sortering l
ON p.delregnskab = l.delregnskab
and p.sagsbudget >= l.frasag
and p.sagsbudget <= l.tilsag
order by l.delregnskab, l.frasag, l.tilsag, l.titel

A single result set will make the code that processes the results
easier!

John

"Michael Vardinghus" <michaelvardinghus (AT) notexisting (DOT) com> wrote in
message
news:uZLZNpMTEHA.2416 (AT) TK2MSFTNGP09 (DOT) phx.gbl...
Think i got it solved by this - seems to work okay...i'm not
updating
in
this example - so the select will be replaced with an update
sentence

USE um_dw_finance_staging



DECLARE @delregnskab int, @frasag int , @tilsag int, @titel varchar
(40)



DECLARE led1_cursor CURSOR FOR

SELECT delregnskab, frasag, tilsag, titel FROM led1sortering

order by delregnskab, frasag, tilsag, titel



OPEN led1_cursor



FETCH NEXT FROM led1_cursor

into @delregnskab, @frasag, @tilsag, @titel



WHILE @@FETCH_STATUS = 0

BEGIN

select *, @titel from dbo.finansbudgetposter_staging post

where post.delregnskab = @delregnskab and post.sagsbudget >=
@frasag
and

post.sagsbudget <= @tilsag

FETCH NEXT FROM led1_cursor

into @delregnskab, @frasag, @tilsag,
@titel

END



CLOSE authors_cursor

DEALLOCATE authors_cursor

GO



"Michael Vardinghus" <michaelvardinghus (AT) notexisting (DOT) com> skrev i en
meddelelse news:%23RkbWCMTEHA.3608 (AT) TK2MSFTNGP11 (DOT) phx.gbl...
How would one go about doing this:

Table 1:

DimA Type FromDimB ToDimB
123 A 1 2
456 B 1 2
789 C 3 8

Table 2:

DimB Type DimA
1 A 123
2 A 123
2 B 456
3 C 789
4 C 789
5 C 789


For now there is no DimA column in table 2 so I have to make this
either
by
joining or cursor ? What would be the best approach - making a
cursor
on
Table 1 and updatering table 2 in steps ?

\Michael Vardinghus













Reply With Quote
  #8  
Old   
John Bell
 
Posts: n/a

Default Re: Combining tables - 06-08-2004 , 08:00 AM



Hi

I assume that is the Ken Henderson book? I have it, but haven't read it
either!!!! He is apparently producing a boxed set
http://search.barnesandnoble.com/boo... 287509&itm=8,
but I may wait for the directors cut DVD with extra bits

John

"Michael Vardinghus" <michaelvardinghus (AT) notexisting (DOT) com> wrote

Quote:
Actually bougth sql for gurus....perhaps i should try to read it...

"John Bell" <jbellnewsposts (AT) hotmail (DOT) com> skrev i en meddelelse
news:uIHEkaUTEHA.1472 (AT) TK2MSFTNGP09 (DOT) phx.gbl...
Hi

Time spent reading books online will repay itself ! You may also want to
check out Itzik's articles on http://www.winnetmag.com/SQLServer/ or
subscribe to the printed version!

His (and Tom's) book at:


http://search.barnesandnoble.com/boo...sbn=1893115828
is certainly one for every bookshelf and there are many others! A more
comprehensive list can be found at:
http://www.aspfaq.com/show.asp?id=2423

John

"Michael Vardinghus" <michaelvardinghus (AT) notexisting (DOT) com> wrote in
message
news:uWsJ41TTEHA.3548 (AT) TK2MSFTNGP09 (DOT) phx.gbl...
Didn't think i could join like that...wouw...

And I couldn't aggree more....would avoid cursors if possible - didn't
think
i could join...

\Michael V

"John Bell" <jbellnewsposts (AT) hotmail (DOT) com> skrev i en meddelelse
news:uQgeIwTTEHA.4048 (AT) TK2MSFTNGP12 (DOT) phx.gbl...
Hi

How about:

select p.*, l.titel
from dbo.finansbudgetposter_staging p JOIN dbo.led1sortering l
ON p.delregnskab = l.delregnskab
and p.sagsbudget >= l.frasag
and p.sagsbudget <= l.tilsag
order by l.delregnskab, l.frasag, l.tilsag, l.titel

A single result set will make the code that processes the results
easier!

John

"Michael Vardinghus" <michaelvardinghus (AT) notexisting (DOT) com> wrote in
message
news:uZLZNpMTEHA.2416 (AT) TK2MSFTNGP09 (DOT) phx.gbl...
Think i got it solved by this - seems to work okay...i'm not
updating
in
this example - so the select will be replaced with an update
sentence

USE um_dw_finance_staging



DECLARE @delregnskab int, @frasag int , @tilsag int, @titel
varchar
(40)



DECLARE led1_cursor CURSOR FOR

SELECT delregnskab, frasag, tilsag, titel FROM led1sortering

order by delregnskab, frasag, tilsag, titel



OPEN led1_cursor



FETCH NEXT FROM led1_cursor

into @delregnskab, @frasag, @tilsag, @titel



WHILE @@FETCH_STATUS = 0

BEGIN

select *, @titel from dbo.finansbudgetposter_staging post

where post.delregnskab = @delregnskab and post.sagsbudget >=
@frasag
and

post.sagsbudget <= @tilsag

FETCH NEXT FROM led1_cursor

into @delregnskab, @frasag, @tilsag,
@titel

END



CLOSE authors_cursor

DEALLOCATE authors_cursor

GO



"Michael Vardinghus" <michaelvardinghus (AT) notexisting (DOT) com> skrev i
en
meddelelse news:%23RkbWCMTEHA.3608 (AT) TK2MSFTNGP11 (DOT) phx.gbl...
How would one go about doing this:

Table 1:

DimA Type FromDimB ToDimB
123 A 1 2
456 B 1 2
789 C 3 8

Table 2:

DimB Type DimA
1 A 123
2 A 123
2 B 456
3 C 789
4 C 789
5 C 789


For now there is no DimA column in table 2 so I have to make
this
either
by
joining or cursor ? What would be the best approach - making a
cursor
on
Table 1 and updatering table 2 in steps ?

\Michael Vardinghus















Reply With Quote
  #9  
Old   
Ken Henderson
 
Posts: n/a

Default Re: Combining tables - 06-10-2004 , 01:27 PM



Actually, the director's cut won't be out until Yukon, so you should go
ahead and buy the boxed set now while you can :-)

Peace,

-kh

"John Bell" <jbellnewsposts (AT) hotmail (DOT) com> wrote

Quote:
Hi

I assume that is the Ken Henderson book? I have it, but haven't read it
either!!!! He is apparently producing a boxed set
http://search.barnesandnoble.com/boo... 287509&itm=8,
but I may wait for the directors cut DVD with extra bits

John

"Michael Vardinghus" <michaelvardinghus (AT) notexisting (DOT) com> wrote in message
news:eX6wPpUTEHA.2580 (AT) TK2MSFTNGP12 (DOT) phx.gbl...
Actually bougth sql for gurus....perhaps i should try to read it...

"John Bell" <jbellnewsposts (AT) hotmail (DOT) com> skrev i en meddelelse
news:uIHEkaUTEHA.1472 (AT) TK2MSFTNGP09 (DOT) phx.gbl...
Hi

Time spent reading books online will repay itself ! You may also want
to
check out Itzik's articles on http://www.winnetmag.com/SQLServer/ or
subscribe to the printed version!

His (and Tom's) book at:


http://search.barnesandnoble.com/boo...sbn=1893115828
is certainly one for every bookshelf and there are many others! A more
comprehensive list can be found at:
http://www.aspfaq.com/show.asp?id=2423

John

"Michael Vardinghus" <michaelvardinghus (AT) notexisting (DOT) com> wrote in
message
news:uWsJ41TTEHA.3548 (AT) TK2MSFTNGP09 (DOT) phx.gbl...
Didn't think i could join like that...wouw...

And I couldn't aggree more....would avoid cursors if possible -
didn't
think
i could join...

\Michael V

"John Bell" <jbellnewsposts (AT) hotmail (DOT) com> skrev i en meddelelse
news:uQgeIwTTEHA.4048 (AT) TK2MSFTNGP12 (DOT) phx.gbl...
Hi

How about:

select p.*, l.titel
from dbo.finansbudgetposter_staging p JOIN dbo.led1sortering l
ON p.delregnskab = l.delregnskab
and p.sagsbudget >= l.frasag
and p.sagsbudget <= l.tilsag
order by l.delregnskab, l.frasag, l.tilsag, l.titel

A single result set will make the code that processes the results
easier!

John

"Michael Vardinghus" <michaelvardinghus (AT) notexisting (DOT) com> wrote in
message
news:uZLZNpMTEHA.2416 (AT) TK2MSFTNGP09 (DOT) phx.gbl...
Think i got it solved by this - seems to work okay...i'm not
updating
in
this example - so the select will be replaced with an update
sentence

USE um_dw_finance_staging



DECLARE @delregnskab int, @frasag int , @tilsag int, @titel
varchar
(40)



DECLARE led1_cursor CURSOR FOR

SELECT delregnskab, frasag, tilsag, titel FROM led1sortering

order by delregnskab, frasag, tilsag, titel



OPEN led1_cursor



FETCH NEXT FROM led1_cursor

into @delregnskab, @frasag, @tilsag, @titel



WHILE @@FETCH_STATUS = 0

BEGIN

select *, @titel from dbo.finansbudgetposter_staging post

where post.delregnskab = @delregnskab and post.sagsbudget >=
@frasag
and

post.sagsbudget <= @tilsag

FETCH NEXT FROM led1_cursor

into @delregnskab, @frasag, @tilsag,
@titel

END



CLOSE authors_cursor

DEALLOCATE authors_cursor

GO



"Michael Vardinghus" <michaelvardinghus (AT) notexisting (DOT) com> skrev i
en
meddelelse news:%23RkbWCMTEHA.3608 (AT) TK2MSFTNGP11 (DOT) phx.gbl...
How would one go about doing this:

Table 1:

DimA Type FromDimB ToDimB
123 A 1 2
456 B 1 2
789 C 3 8

Table 2:

DimB Type DimA
1 A 123
2 A 123
2 B 456
3 C 789
4 C 789
5 C 789


For now there is no DimA column in table 2 so I have to make
this
either
by
joining or cursor ? What would be the best approach - making a
cursor
on
Table 1 and updatering table 2 in steps ?

\Michael Vardinghus

















Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.