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

Asked

Viewed 9,412 times

6

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

I know 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, besides the SELECT (that is already getting some data from another table)?

  • Can’t you just add a GETDATE()? INSERT INTO TABLE(ID, NAME, ADDRESS, DATA) SELECT ID, NAME, ADDRESS, GETDATE() AS 'DATA' FROM...

  • User is a table or system user? You want to enter the time of action?

  • Which database?

  • Thank you, @Leandroangelo!

  • It’s an Access database, @David. Thanks! Abs!

2 answers

8


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
  • Thank you, @Onosendai !

  • @Leandro Not so - it is always a pleasure to help. =)

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
  • Thank you, @Guilherme Lautert !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.