带有子查询的Sybase插入语句

thi*_*man 1 sql sybase insert subquery

我正在尝试insert使用子查询,但这insert失败了:

insert into 
    TABLE_A(COL_A, COL_B, COL_C, COL_D, COL_E, COL_F)
    values (
        1, 
        (select COL_B from TABLE_B where user_name = 'foo'),
        (select COL_C from TABLE_C where age = 25),
        2,3,4);
Run Code Online (Sandbox Code Playgroud)

我试着写不同但它仍然失败.

Gor*_*off 6

你想要insert . . . select语法.在values不需要:

insert into TABLE_A(COL_A, COL_B, COL_C, COL_D, COL_E, COL_F) 
    select 1, (select COL_B from TABLE_B where user_name = 'foo'),
           (select COL_C from TABLE_C where age = 25),
           2, 3, 4;
Run Code Online (Sandbox Code Playgroud)

cross join如果您愿意,也可以将其写为:

insert into TABLE_A(COL_A, COL_B, COL_C, COL_D, COL_E, COL_F) 
    select 1, b.COL_B, c.COL_C,
           2, 3, 4;
    from (select COL_B from TABLE_B where user_name = 'foo') b cross join
         (select COL_C from TABLE_C where age = 25) c
Run Code Online (Sandbox Code Playgroud)