Locate and replace

  • What I'm trying to do is locate an information on another table and update the temporary table, not sure if this is the correct way, my code is

    DECLARE @areacode char(15)

    DECLARE csrSites CURSOR FOR

    SELECT rtrim(area_code) FROM billable_temp

    Open csrSites

    FETCH NEXT FROM csrSites INTO @areacode

    WHILE @@FETCH_STATUS = 0

    BEGIN

    SELECT DISTINCT areacode, CASE WHEN (SELECT DISTINCT areacode FROM rates1 where areacode = @areacode) IS NULL THEN 'No data ' ELSE areacode END AS areacode

    FROM rates1

    where areacode = @areacode

  • Depends an awful lot on how the updates will take place. If there needs to be specific processing for each row, the CURSOR will work. If you can get away with changing a lot of records at once, the performance is better (unless there are so many that it locks your table).

    Care to supply the update statement as well?

    Guarddata-

  • well , it's not working at all, i'm trying this: and it takes too long, don't know any idea???

    declare @areadesc char(50)

    DECLARE csrSites CURSOR FOR

    SELECT area_code FROM billable_temp

    select @count=(select count(*) from billable_temp)

    Open csrSites

    FETCH NEXT FROM csrSites INTO @areacode

    WHILE @count > 0

    select @areadesc=(select distinct areadesc from rates1 where areacode=@areacode)

    select @count = @count -1

    update billable_temp set country = @areadesc

    CLOSE csrSites

    deallocate csrSites

  • It appears that there are some conditions missing. For example, the update billable_temp statement does not restrict the entries to a specific area code.

    Would it work for you to do something like this?

    UPDATE billable_temp SET country = areadesc

    FROM rates1 WHERE billable_temp.area_code = r1.areacode

    You could then avoid the cursor and update everything in a single statement.

    Guarddata-

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply