| Author |
Topic |
|
reenz
Starting Member
29 Posts |
Posted - 2006-04-19 : 07:29:24
|
| Suppose i have 2 tablesTable Acol1 int id bigintTable Bcol1 intcol2 varchar(100)i want to match table A with table B based on col 1 and insert the id of table A with the cols in table B into table C as well as break up delimited data col of table BEgTable A123 ID1234 ID2Table B123 a>b>c234 d>e>fTable C123 ID1 a b c234 ID2 d e fHow do i go about doing it? I currently using cursors but the performance sux. |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-04-19 : 08:00:01
|
[code]create function dbo.CSVStr(@str varchar(8000), @delimiter varchar(10), @word_no int)returns varchar(100)asbegin declare @word varchar(100), @s int, @c int, @i int select @i = 1 select @s = 1 select @c = -1 while (@i <= @word_no and @c <> 0) begin select @s = @c + 1 select @c = charindex(@delimiter, @str + @delimiter, @s) select @i = @i + 1 end select @word = substring(@str, @s, @c - @s) where @c <> 0 return @wordendgodeclare @TableA table( col1 int, id varchar(10))declare @TableB table( col1 int, col2 varchar(100))insert into @TableAselect 123, 'ID1' union allselect 234, 'ID2'insert into @TableBselect 123, 'a>b>c' union allselect 234, 'd>e>f'select a.col1, a.id, dbo.CSVStr(b.col2, '>', 1), dbo.CSVStr(b.col2, '>', 2), dbo.CSVStr(b.col2, '>', 3)from @TableA a inner join @TableB b on a.col1 = b.col1[/code] KH |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
reenz
Starting Member
29 Posts |
Posted - 2006-04-19 : 20:28:20
|
| the databases are created donkey years ago and its not up to me to say normalize them and i can do it.. data are coming in at 5 million records per table per day and i'm told to work with at least 5 such tables... it had been proposed to changed the schema but it will affect like dozens of other applications so no one is willing to bite the bullet yet. |
 |
|
|
reenz
Starting Member
29 Posts |
Posted - 2006-04-19 : 20:44:09
|
| Hi KH,suppose it gets a little messier... Table B now have a gid col which affect how data is col2 is likeTable Bcol1 intcol2 varchar(100)gid int There is like 20 different gids. And i am interested in data a b c d efrom a few gids...Eggid = 40 OR 18col2 =1234567dgid = 42 and consist of dcol 2 = 1234567d890>…>a:b:celse col 2 = 1234567d890>…>a>b>cgid = 18col2 = e123456..and i would like to write data from table B with table A's id and data a b c d into table Cand table B with table A's id and data d e into another table D and data from the rest of Table A's id and table B (other gids) into table E so that my asp.net application can read from table C D and E rather then the chunky table A n Bis it possible/feasible? |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-04-19 : 21:40:44
|
yes. you can use CASE WHEN .. THEN to do this. KH |
 |
|
|
reenz
Starting Member
29 Posts |
Posted - 2006-04-20 : 04:08:16
|
| i tried something in the line ofcase when gid = '42' then dbo.CSVStr(b.col2, '>', 1)end as aand it work.. im thinking of case when gid = '42' then dbo.CSVStr(b.col2, '>', 1) as a, dbo.CSVStr(b.col2, '>', 2) as b, dbo.CSVStr(b.col2, '>', 3) as c endbut it gave an error... is it possible to do something like that since i need 3 data from the same case condition instead of doing 3 cases that are the same?case when gid = '42' then dbo.CSVStr(b.col2, '>', 1)end as a,case when gid = '42' then dbo.CSVStr(b.col2, '>', 2)end as b,case when gid = '42' then dbo.CSVStr(b.col2, '>', 3)end as c |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-04-20 : 06:51:57
|
can you post some sample data of TableB for various different format of col2 that you might have KH |
 |
|
|
reenz
Starting Member
29 Posts |
Posted - 2006-04-20 : 22:11:23
|
| table Bgid intckpt varchar(100)gstngsubSample Data 142HKGBKKDF 1>HKG>KWS2356>0103>H143144912 >HHP>HKGKCCSample Data 242SINLHRDF 1>:BRU-HUB> BCS2411:200406:AAA5692:STNBRUHUBSample Data 318STAMP SGN00010307HKGKCCSample Data 420HKG OHrtn to shipper 0000HKGKCCSample Data 540HKGHKGRTCNOR STAMP 0000HKGKCCWhat i need is gid = 42 & substring(7,2)= DF Sample Data 1DF NULL KWS23560103H143144912HKGKCCgid = 42 & substring(7,2)= DF & gstn NOT IN (BKK, HHP,SSC ) & gsub <> HUBSample Data 2DF NULLBCS2411200406AAA5692BRUHUBgid = 18 Sample Data 318OK Data retrieved from select c1 from tablec1 where gid = 18STAMP LEFT(12)NULLNULLNULLHKGKCCgid = 20Sample Data 420OHNULLNULLNULLNULLHKGKCCGid = 40Sample Data 540RTNULLNULLNULLNULLHKGKCCWhat i hope to achieve is to insert data with gid 42 into a table42,data with gid 18 or 40 into another table1840and all the data(includiing gid 42,18,40) into tableALL |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-04-21 : 01:15:58
|
looks like you have quite a complicated scenario.Modify the CSVStr() function and use IF .. ELSE or CASE WHEN .. ELSE .. END to check for your various condition and process accordingly KH |
 |
|
|
reenz
Starting Member
29 Posts |
Posted - 2006-04-21 : 05:55:00
|
| but even if i were to edit the function to cater for various gid, it wouldnt be able to return multiple values right?what came to mind is to pass in gid and col2 into the function and hoping to return like 7 values from it....... |
 |
|
|
|