76

Is there a way to insert pre-set values and values I get from a select-query? For example:

INSERT INTO table1 VALUES ("A string", 5, [int]).

I have the value of "A string" and the number 5, but I've to find the [int] value from a select like this:

SELECT idTable2
FROM table2
WHERE ...

that gives me that id to put inside table1.

How to merge this into one statement?

10 Answers 10

117

Use an insert ... select query, and put the known values in the select:

insert into table1
select 'A string', 5, idTable2
from table2
where ...
Sign up to request clarification or add additional context in comments.

2 Comments

A problem I've found with this approach is that if the SELECT returns zero records then the statement succeeds but no data is INSERT.
@NeilC.Obremski: Yes, but that is only a problem if you expect the result to be something else. If you want an error in that case, you can use a subquery instead.
103

just use a subquery right there like:

INSERT INTO table1 VALUES ("A string", 5, (SELECT ...)).

3 Comments

this method may give an error like "more than 1 row"
It should be "INSERT INTO table1 VALUES ("A string", 5, (SELECT ... LIMIT 1))."
This solution is very helpful when you need to copy only few values from a row in one table, into a new row in another table.
23
INSERT INTO table_name1
            (id,
             name,
             address,
             contact_number) 
SELECT id, name, address, contact_number FROM table_name2;   

Comments

12

try this

INSERT INTO TABLE1 (COL1 , COL2,COL3) values
('A STRING' , 5 , (select idTable2 from Table2) )
where ...

2 Comments

What about multiple columns from the SELECT table? If I try more than one column coming from there, I get Operand should contain 1 column(s).
that's it . yes!
8

All other answers solves the problem and my answer works the same way as the others, but just on a more didactically way (this works on MySQL... don't know other SQL servers):

INSERT INTO table1 SET 
  stringColumn  = 'A String', 
  numericColumn = 5, 
  selectColumn  = (SELECT idTable2 FROM table2 WHERE ...);

You can refer the MySQL documentation: INSERT Syntax

Comments

3
INSERT INTO table1 
SELECT "A string", 5, idTable2
FROM table2
WHERE ...

See: http://dev.mysql.com/doc/refman/5.6/en/insert-select.html

Comments

2
INSERT INTO table1(Table2Id, KeyTypeEnumId, SortOrder, TenantId)
    SELECT Id, 1, 1, TenantId
    FROM table2
    WHERE WebsitePageTypeEnumId = 10 AND ElementTypeEnumId = 16

Comments

1
INSERT INTO table1 (col1, col2)
SELECT "a string", 5, TheNameOfTheFieldInTable2
FROM table2 where ...

Comments

1

Try the following:

INSERT INTO table1 
SELECT 'A string', 5, idTable2 idTable2 FROM table2 WHERE ...

Comments

0

Try this:

INSERT INTO table1 SELECT "A string", 5, idTable2 FROM table2 WHERE ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.