Macin
Macin

Reputation: 391

SQL Insert combine a value and columns from another table

I am trying to create a row in an existing table which consists of current timestamp, and some other data from other table.

What would be a correct syntax to achieve it? Below is a pseudo code for what I am trying to achieve.

INSERT INTO logTable
    VALUES( currentTimeStamp, select data1, data2, data3 FROM datatable WHERE data1 = 123);

Upvotes: 1

Views: 1041

Answers (2)

Rameez
Rameez

Reputation: 1712

INSERT INTO logTable (column1, column2, column3,column4)
  select CURRENT_TIMESTAMP,data1, data2, data3 FROM datatable WHERE data1 = 123

You can use something else instead of CURRENT_TIMESTAMP, but make sure column types matches.

Upvotes: 2

Gordon Linoff
Gordon Linoff

Reputation: 1269873

You cannot mix insert . . . values and insert . . . select. Just use:

INSERT INTO logTable
    select currentTimeStamp, data1, data2, data3
    FROM datatable
    WHERE data1 = 123;

Or, better yet, set up the column to have a default value of the current time stamp so it gets set automatically.

Upvotes: 2

Related Questions