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.
| Author |
Topic |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2003-01-09 : 07:15:29
|
| Adrian writes "I have two tables My_Phones ( PhoneNo varchar(32), Date varchar(32), Duration varchar(32) ) imported from a text file ( that's why all the columns are varchars ) and a table My_Prefix ( Prefix varchar(16) PRIMARY KEY, Country varchar(32) )SELECT * FROM My_Phones 0040724215950 Jan-01-2003 670040216845157 Jan-02-2003 340040245614904 Jan-03-2003 56SELECT * FROM My_Prefix004 Romania004021 Romania_Bucharest0040723 Romania_ConnexI want to find for every phone in My_Phones it's coresponding Prefix and Country having in mind that I have to choose prefix with the longest length .I tried to create a cursor FOR SELECT Prefix, Country FROM My_Prefix ORDER BY LEN(Prefix) into @prefix, @country ( because I may have prefixes that start with the same numbers ( ex: 0042021 must be selected first than 0042 ) ) and then DELETE FROM My_Phones WHERE PhoneNo LIKE @prefix+'%' after I previous created a trigger when delete from My_Phones that inserted into another table the PhoneNo, Prefix ... But this is very slow ! - because I don't have any key in My_Phones.Anyone have a better idea ?Something like a cursor for every row in My_Phones and then select Prefix, Country from MyPrefix where LEFT(@phone,LEN(PREFIX)) = PREFIX AND LEN(PREFIX) = MAX(LEN(PREFIX))But this will be also slow - to may calculations - and not using the primary key because I need LEN(PREFIX) not PREFIX ..." |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2003-01-09 : 07:40:30
|
| This wouldn't be a telecoms rating system would it?You have the dialling codes in a table?Include the code length on this and loop through and set the dialling code ID on the table.declare @i intselect @i = max(codelen) from DiallingCodeswhile @i > 2beginupdate myphones set codeid = diallingcodes.idwhere left(code,@i) = DiallingCodes.codeand DiallingCodes.codelen = @iand codeid is nullset @i = @i - 1endmake sure you include 00 as a default codeTo be scalable you should do this in batches of records.I implement this using a table which holds the IDs of the calls to work on. The workflow processes these records to completion then gets the next. The number of recs that are selected is in a variable so can be changed from system to system depending on memory.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
|
|
|