带有子查询的Sybase插入语句

时间:2014-08-13 22:13:24

标签: sql insert subquery sybase

我正在尝试使用子查询执行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);

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

1 个答案:

答案 0 :(得分: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;

如果您愿意,也可以将其写为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