MOR_SNOW MOR_SNOW - 3 months ago 10
SQL Question

Inserting table from one table to another, and adding values are specified

I need to insert value in a table, extracted from another table, but some values are specified by me, and therefore not from another table.

INSERT INTO Person (age)
SELECT age FROM AnotherPerson;


Now let's pretend that I want to add another data in another column in the person table, and this data is not extracted from the other table. So what I'm thinking, is something like this:

INSERT INTO Person (age, name)
VALUES (SELECT age FROM AnotherPerson, 'Peter');


The above code is just an example I came up with, so I'm can stay with the relevant aspect.

I was thinking that I could insert the data separately, which would work, since this syntax:

INSERT INTO Person (age, name)
VALUES (SELECT age FROM AnotherPerson, 'Peter');


doesn't work.

But what if all the ecolumns of the Person table must be not null? How can I do this?

Answer
INSERT INTO Person (age, name)
SELECT age, 'Peter' FROM AnotherPerson