How to UPDATE all columns of a record without having to list every column

后端 未结 8 1827
情歌与酒
情歌与酒 2020-12-29 03:20

I\'m trying to figure out a way to update a record without having to list every column name that needs to be updated.

For instance, it would be nice if I could use so

相关标签:
8条回答
  • 2020-12-29 03:27

    How about using Merge?

    https://technet.microsoft.com/en-us/library/bb522522(v=sql.105).aspx

    It gives you the ability to run Insert, Update, and Delete. One other piece of advice is if you're going to be updating a large data set with indexes, and the source subset is smaller than your target but both tables are very large, move the changes to a temporary table first. I tried to merge two tables that were nearly two million rows each and 20 records took 22 minutes. Once I moved the deltas over to a temp table, it took seconds.

    0 讨论(0)
  • 2020-12-29 03:28

    you could do it by deleting the column in the table and adding the column back in and adding a default value of whatever you needed it to be. then saving this will require to rebuild the table

    0 讨论(0)
  • 2020-12-29 03:30

    In Oracle PL/SQL, you can use the following syntax:

    DECLARE
      r my_table%ROWTYPE;
    BEGIN
      r.a := 1;
      r.b := 2;
      ...
    
      UPDATE my_table
      SET ROW = r
      WHERE id = r.id;
    END;
    

    Of course that just moves the burden from the UPDATE statement to the record construction, but you might already have fetched the record from somewhere.

    0 讨论(0)
  • 2020-12-29 03:44

    If you are using Oracle, you can use rowtype

    declare 
        var_x  TABLE_A%ROWTYPE;
    Begin
        select * into var_x
        from TABLE_B where rownum = 1;
    
        update TABLE_A set row = var_x
        where ID = var_x.ID;
    end;
    /
    

    given that TABLE_A and TABLE_B are of same schema

    0 讨论(0)
  • 2020-12-29 03:45

    It's not possible.

    What you're trying to do is not part of SQL specification and is not supported by any database vendor. See the specifications of SQL UPDATE statements for MySQL, Postgresql, MSSQL, Oracle, Firebird, Teradata. Every one of those supports only below syntax:

    UPDATE table_reference
       SET column1 = {expression} [, column2 = {expression}] ...
    [WHERE ...]
    
    0 讨论(0)
  • 2020-12-29 03:47

    This is not posible, but..

    you can doit:

    begin tran
    delete from table where CONDITION
    insert into table select * from EqualDesingTabletoTable where CONDITION
    commit tran
    

    be carefoul with identity fields.

    0 讨论(0)
提交回复
热议问题