Hello, when writing code in TSQL, a problem arose, I could not find the answer in Google :(

There are 2 tables:

  • AA (id int, somestring char) and
  • BB (id int, somestring char, somedate datetime)

There is also an initialized @thedate variable of type datetime.

How do you write Insert so that as a result of its execution all the records from AA are copied to BB: the ID and somestring fields, respectively, and the somedate field to be filled with the value of the @thedate variable?

Thank you in advance!

    2 answers 2

    Insert into BB select *, @thedate from AA 

      Just in case, it is better to explicitly list what you choose from AA and where to insert it into BB, otherwise the query will fall when the table structure changes.

      I advise you:

       Insert into BB (ID, somestring, somedate) select ID, somestring, @thedate from AA