Using INSERT INTO with 'SELECT' to supply some values but not others (Access 2010)

后端 未结 2 1779
后悔当初
后悔当初 2020-12-19 16:20

I have two tables that look like this:

(\'MASTER_EOD\' TABLE)
ID  SHA1_HEX    ROLE
----------------------------
1   ff34bb03    2
2   eef0a005    1



(\'ROL         


        
相关标签:
2条回答
  • 2020-12-19 16:35

    You can substitute a DLookup expression for your SELECT role_num ... query.

    SELECT role_num FROM role_index WHERE role_name='Follow Up'
    DLookup("role_num", "role_index", "role_name='Follow Up'")
    

    Use that DLookup in your INSERT.

    INSERT INTO master_eod
        (
            sha1_hex,
            [role]
        )
    VALUES
        (
            'ef03ff03',
            DLookup("role_num", "role_index", "role_name='Follow Up'")
        );
    

    I enclosed role in square brackets because it is a reserved word.

    0 讨论(0)
  • 2020-12-19 16:43

    Try doing it in the most standard way in SQL, by using the SELECT as the whole source in the INSERT instead of just a single column sub-query:

    INSERT INTO master_eod ( sha1_hex, role )
    SELECT 'ef03ff03', role_num FROM role_index WHERE role_name='Follow Up' ;
    

    Note that you must ensure that such a query (the SELECT alone) returns just a single row, otherwise you would end up inserting many rows at once in a single go, one for each role_num found (which may or may not be desirable).

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