Change primary key

krithikakrithika Member Posts: 56
edited 2012-02-17 in NAV Three Tier
How to change primary key for a set of records without deleting the records and without using temp table ](*,)

Comments

  • mohana_cse06mohana_cse06 Member Posts: 5,504
    Where do you need?
    Did you try with RENAME?
  • ClausHamannClausHamann Member Posts: 80
    Loop though the records and use the RENAME function to rename the individual records.

    Regards
  • krithikakrithika Member Posts: 56
    Thanks for the response. I have a table which has three fields as primary key. In that i am trying to modify a field which is of integer data type. When i loop it and modify the first record it goes and saves in the table as last record hence the loop terminates abruptly. Can u give me a solution for this?
  • mohana_cse06mohana_cse06 Member Posts: 5,504
    Temporarily you can create a field like PKChanged and Set it immediatly after changing and Put filter on PMChanged False..
  • krithikakrithika Member Posts: 56
    Is there any other options apart from the above provided scenario/solution
  • vijay_gvijay_g Member Posts: 884
    edited 2012-02-16
    Why don't you use above solution even it's simple?

    if you are not able to add a field then you can use any other field that is not being used in your table.
  • ajageaviajageavi Member Posts: 15
    yes u can use like

    rec.setrang(fields);
    re. findfirst then
    repeat
    rec1.get(rec);
    rec1.renbame(fields);
    unitl rec.next=0

    like above
    Nav & LS Retail Technical Consultant
    Ajageavi Nath Keshari
  • ClausHamannClausHamann Member Posts: 80
    Hi

    To get this to work you need to use another sorting key than the Primary Key. It is also good practice to use another variable to do the changes in.

    E.g. like this
    MyVar.SETCURRENTKEY(NotPK);
    REPEAT
      MyVar2 := MyVar;
      MyVar2.RENAME(Value1 [, Value2] ,...);
    UNTIL MyVar.next = 0;
    

    Regards
  • krithikakrithika Member Posts: 56
    Thanks for ur replies,i have changed the primary key,but the system provides default message As do u want to change the primary key. Yes/No.. for each record Rather than providing YES for each key can i be able change as YES by default for only once which changes entire set of PK to be changed.

    Any soultions ..
  • krikikriki Member, Moderator Posts: 9,112
    ajageavi wrote:
    yes u can use like

    rec.setrang(fields);
    re. findfirst then
    repeat
    rec1.get(rec);
    rec1.renbame(fields);
    unitl rec.next=0

    like above
    NEVER use FINDFIRST in a loop! you need to use the FINDSET-statement. But the idea is correct. The correct code to rename is this:
    rec.RESET;
    rec.SETCURRENTKEY(...);
    rec.SETRANGE(...);
    IF rec.FINDSET(TRUE,TRUE) THEN // check the help for info on the parameters of FINDSET
      REPEAT
        rec2 := rec;
        rec2.RENAME(new key fields);
      UNTIL rec.NEXT = 0;
    
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


Sign In or Register to comment.