sql >> データベース >  >> RDS >> Oracle

別のテーブルからランダムに選択された一意の値を使用して、テーブルの列を更新します

    これを行うには、table1に挿入するためのトリガーを作成します。次に、table1にまだ設定されていないtable2からすべてのAddressIdを選択し、ランダムに並べ替えて、最初のものを選択します。このようなトリガーの実際の例:

    CREATE TRIGGER TRIGGER1 
    BEFORE INSERT ON TABLE1 
    FOR EACH ROW /* Trigger for each new row inserted into table1 */
    BEGIN
    SELECT addressId INTO :new.ADDRESSID /* Set AddressId for new row in table1 */ FROM 
    (
       SELECT table2.addressId FROM table2
       LEFT JOIN table1 ON table1.CompanyNumber = table2.company AND table1.AddressID = table2.addressId
       WHERE table1.AddressID IS NULL /* Not already in table1 */ AND table2.Company = :new.COMPANYNUMBER /* Select all addressIds matching the company number */
       ORDER BY dbms_random.value /* order them randomly */
     ) hits
     WHERE ROWNUM = 1; /*Only pick the first randomly ordered one*/
        
    EXCEPTION
      WHEN no_data_found THEN /* CompanyNumber not in table2 or no unique AddressId left */
         :new.ADDRESSID := NULL;
    END;
    


    1. jtableセルのwhereパラメータを使用してmysqlテーブルを更新する

    2. MySQLワールドデータベースサブクエリを回避しようとしています

    3. SQLデータベーステーブルに日時を挿入する方法は?

    4. Oracle SQLのreplace()関数の出力からのコンマ区切り文字列を使用したIN句の使用