2

I have two tables, tab1 and tab2.

tab2 has all of the columns of tab1 but with an extra column for a timestamp. What I want to do is copy all of the rows from tab1 into tab2 and input the same time thae timestamp column for all rows that I insert. I can get and input the time fine, but I'm confused how to copy and insert data and fill in the last column with the timestamp for all of the rows that I inserted.

So I want to do:

Insert into tab2 select * from tab1

but I also want to add data for that final column in tab2, how can I do this?

Mureinik
  • 297,002
  • 52
  • 306
  • 350

1 Answers1

2

You could add the timestamp to the select list so the column lists of both tables would match:

INSERT INTO tab2
SELECT *, CURRENT_TIMESTAMP()
FROM   tab1

EDIT
To answer the question in the comment - you don't have to use CURRENT_TIMESTAMP(). Any expression returning a timestamp would do. You could use a hard-coded timestamp:

INSERT INTO tab2
SELECT *, TIMESTAMP('2017-07-07 19:43:00')
FROM   tab1
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • Great idea, but I don't necessarily want to fetch the current time and use that. If i wanted to put in a static value, how would i do that? Could i replace "CURRENT_TIMESTAMP()" with something like "2017-07-07 19:43:00"? – Cristian Navarrete Feb 23 '17 at 21:07
  • I settled on INSERT INTO tab1 SELECT * , '07/07/07 19:43:00' FROM tab1. Thanks – Cristian Navarrete Feb 24 '17 at 21:14