Is it possible to do an INSERT INTO SELECT + other values outside of SELECT?

5

I need to insert into a table ACCESS some values from another table plus the time and the user who registered.

I know that to get the data from the other table just do something like:

INSERT INTO TABELA(ID, NOME, ENDERECO)
SELECT ID, NOME, ENDERECO FROM MEUBANCO2..MINHATABELA
WHERE ..

How do I also enter the time and user data in the same command INSERT INTO, in addition to SELECT (which is already getting some data from another table)?

    
asked by anonymous 13.09.2016 / 16:52

2 answers

6

You can use static values. Assuming, for example, that the user has ID 1024:

INSERT INTO TABELA (ID, NOME, ENDERECO, USUARIO, DATAHORA)
SELECT      A.ID, 
            A.NOME, 
            A.ENDERECO, 
            1024, 
            Now()
FROM        MEUBANCO2..MINHATABELA A
    
13.09.2016 / 16:56
4

You can create the commands to be executed:

SELECT
    'INSERT INTO ACCESSO (user, hora, url) VALUES ('||U.nome||','||P.hora||','||P.url||');'
FROM
    usuario U
    LEFT JOIN pagina P ON P.user_id = U.user_id
    
13.09.2016 / 17:31