Change data procedure with cursor current of

后端 未结 1 334
名媛妹妹
名媛妹妹 2021-01-26 04:38

I need to make a procedure in SQL Developer that can move data from one column to another. These columns are in different tables so i don\'t know how to write the code.

相关标签:
1条回答
  • 2021-01-26 05:21

    UPDATE: Since you're using Oracle, the accepted answer on this link may help clear things up for you...

    When you create your cursor, you'll join between the two tables you have and specify the for update of (column you want to update). From there, the WHERE CURRENT OF c_newpass just refers to the latest row processed by the FETCH statement associated with c_newpass.

    This is just a rough idea of how I think it would work.

    Cursor c_newpass IS 
    select customersecurity.password, customersecurity.cnumbr, table1.cnumbr, table1.password
    from customersecurity, table1
    for update of table1.password
    

    You should then be able to loop through c_newpass, fetching the next row and update table1

    Update table1
    Set password = password
    WHERE CURRENT OF c_newpass
    

    SQL SERVER example: It may not be what you need, but it can show you how cursors work and how they can accomplish what you need. I've got 2 tables, and I need to transfer/copy the names in Foo to the names in Bar because the Bar table has NULL names to start out with. I create 2 variables, one for ID and one for name which will be used to hold the contents of where the cursor (someCursor) currently is. Once you have the cursor set, you need to get items from it which is the FETCH NEXT statement and using the INTO to set the variables for @ID and @Name. I start a while loop by checking @@Fetch_Status to make sure that the previous statement was successful. If so, I use the variable that were just set to update the Bar table, matching the IDs and updating the Name column with the contents of @Name. Once that is done I get the next item in the cursor using FETCH NEXT again. Assuming that there is another item in the cursor, and it was successful, it will do it all over again.

    I think you're using a different DBMS than SQL Server, but the concept should be similar. You'll create the cursor based off the customersecurity table, selecting the ID and Password, and then update the new table based off those columns.

    Create Table Foo(
    ID int identity primary key,
    Name varchar(20)
    )
    
    Create Table Bar(
    ID int identity primary key,
    Name varchar(20)
    )
    
    Insert Into Foo Values('ABC')
    Insert Into Foo Values('XYZ')
    Insert Into Foo Values('JMK')
    
    Insert Into Bar Values(NULL)
    Insert Into Bar Values(NULL)
    Insert Into Bar Values(NULL)
    
    declare @ID int, @name varchar(20)
    
    Declare someCursor CURSOR FOR 
    Select ID, Name From Foo order by ID
    
    OPEN someCursor
    
    FETCH NEXT FROM someCursor
    INTO @ID, @name
    
    WHILE @@Fetch_Status = 0
    BEGIN
        Update Bar
        Set Name = @Name
        Where ID = @ID
    
        FETCH NEXT FROM someCursor
        INTO @ID, @name
    END
    
    Close someCursor
    Deallocate someCursor
    
    select * from Foo
    select * from Bar
    
    0 讨论(0)
提交回复
热议问题