简体   繁体   中英

SQL Error in a insert query where there is a select * from

I'm trying to run this query but the sql server show this error:

Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

This is my query:

insert into gdo_funcionario (TIPDOC, CEDULA, NOMFUN, CODFUN, CODDEP,
                             EMAIL, CODCARGO, ACTIVO, ID_TER) 
VALUES (4, 123456789, 'Name Name Name Name', 'Usuario',
        (select coddep from gdo_grl_dependencias where codigo='11'),
        'xxxxx@xxxx.com',
        (select idcargo from mp_grl_cargo where nombre ='Cargo Cargo Cargo'),
        1, (SELECT * FROM GDO_TERCERO WHERE NUMTER =1234566778899));

I'm not an expert in DB so I hope someone can help me. Thanks

You have subqueries in your INSERT . That's allowed if they return only a single value. So you either have to fix them to return only one or force it by using TOP 1 :

INSERT INTO gdo_funcionario 
            (TIPDOC, 
             CEDULA, 
             NOMFUN, 
             CODFUN, 
             CODDEP, 
             EMAIL, 
             CODCARGO, 
             ACTIVO, 
             ID_TER) 
VALUES     (4, 
            123456789, 
            'Name Name Name Name', 
            'Usuario', 
            (SELECT TOP 1 coddep 
             FROM   gdo_grl_dependencias 
             WHERE  codigo = '11'), 
            'xxxxx@xxxx.com', 
            (SELECT TOP 1 idcargo 
             FROM   mp_grl_cargo 
             WHERE  nombre = 'Cargo Cargo Cargo'), 
            1, 
            (SELECT TOP 1 ID_TER
             FROM   GDO_TERCERO 
             WHERE  NUMTER = 1234566778899)); 

One of those SELECT statements is returning more than one value. If you add a TOP 1 with an ORDER BY , you should be fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM