INSERT with SELECT returning multiple values throws error "subquery returned more than 1 value" - sql

Trying to insert multiple values in a table (as many as ID's from the SELECT result), but the select Id query returns more than 1 value (expected), hence the error.
INSERT INTO table1 (UserId, Date, ...)
VALUES
((SELECT Id from table2 WHERE ClientId = 26), GETDATE(), ...)
what would be the best approach for this?

I'm not sure how your columns line up. It looks like GETDATE() is inserting into Name. But if that's what you need, what about this? You could just do an INSERT SELECT without the VALUES.
INSERT INTO table1 (UserId, Name, ...)
SELECT Id, GETDATE(), ...
from table2
WHERE ClientId = 26

Related

How to use a WITH query to insert into a table and insert the returning id into another table?

I'm trying to insert a row into a table named table1 if only 'value' exists in table2. The newly created row returns an id and I want to use this id through a WITH query and insert it into a third table named table3.
Here is the sql statement I used.
WITH new_key AS (
INSERT INTO table1(name, value) 
SELECT 'test', value
FROM table2 
WHERE value = 'some_value'
RETURNING id
)
INSERT INTO table3(table1_id)
VALUES (new_key);
The INSERT query in the WITH query works and returns an id. Unfortunately the whole statement returns "new_key column does not exist".
You need to select the value from the CTE:
WITH new_key AS (
INSERT INTO table1(name, value)
SELECT 'test', value
FROM table2
WHERE value = 'some_value'
RETURNING id
)
INSERT INTO table3(table1_id)
SELECT id
FROM new_key;

Condition before insert SQL

In DB2 SQL, I want to write something like
insert into employees
(id, name) values (1, "emp1")
where (select count(*) from employee_registry) <= 10
Can't figure out the correct syntax though.
post edit.
i wanted to insert records only if some condition is met.
You need to have a SELECT statement somewhere in order to use the WHERE clause, so something like this might work:
insert into employees (id, name)
select 1, 'emp1' from sysibm.sysdummy1
where (select count(*) from employee_registry) <= 10
sysibm.sysdummy1 is a special system table that always has only one row.
Other method, group by Nothing + having :
insert into employees (id, name)
select '1', 'emp1' from employee_registry
group by 1 having count(*)<=10

insert into value in select statement mutliple values

I try to insert into table with 1 column is (select from table).
I should copy all the id to table1 with 1 column is (select from table )
This not working:
insert into table1 (id,resoucrce,rate) values ((select id from table2),0,0)
I want to do something like that insert all the id from table to another table with default values.
Use insert . . . select:
insert into table1 (id, resource, rate)
select id, 0, 0
from table2;
For copy the content of one table to another table within the same database use this :-
INSERT INTO TARGET_TABLE (`col1`,`col2`) SELECT `col1`,`col2` FROM SOURCE_TABLE;
or your query like that :-
Insert into table1 (id, resource, rate) select id, 0, 0 from table2;
You should focus "values" keyword;
values ((select id from table2),0,0)
When you use values(, , ,) you have to specify value columns. But you're trying to pass both resultset and single values together. That's why you get an error. You should only pass single values...
If its clear for you; you can easily find the correct sql syntax.

SELECT values of an INSERT INTO statement

I need to select a few columns from a table1. I need to insert only one of these columns as well as some arbitrary hard coded data and insert it into table2 while also getting the original select statement back.
Basically I would like to get the results of my INSERT INTO statement instead of the "(1 row(s) affected)" that I get in SSMS.
Is there a way to do this?
Here is a SQLFiddle:
http://sqlfiddle.com/#!3/e9beb/3
Those records will insert just fine. However, I want the results of my SELECT statement to come back to me so that I can do it all at once without multiple reads or trips. Is this possible?
You can use the OUTPUT clause:
INSERT INTO Table2
OUTPUT inserted.*
SELECT Phrase, 'This is an automatic note by the system', GETDATE(), 1
FROM Table1
Use a batch statement and store the intermediate results in a table variable:
DECLARE #intermediate TABLE (
col1 type,
col2 type,
col3 type
);
INSERT INTO #intermediate VALUES ( ... );
INSERT INTO destinationTable SELECT * FROM #intermediate;
SELECT #intermediate;
If using this from code you can have all of this in a single command-text string.
Have you tried something like this:
INSERT INTO Table1 (
SELECT Phrase, Notes, GetDate, 1
FROM Table2
UNION
SELECT Phrase, 'This is an automatic note by the system', GETDATE(), 1
UNION
)

Is this possible with sql?

Is it possible to do something like this:
INSERT INTO table(col1, col2) VALUES(something_from_another_table, value);
With something_from_another_table being a SQL command? Like, is there something I can do that's equivelant to:
INSERT INTO table(col1, col2) VALUES((SELECT value FROM table2 WHERE id = 3), value);
Yes
INSERT INTO table(col1, col2)
SELECT value1, 'value2' FROM table2 WHERE id = 3
Where value1 is the value from the 'other table' and value2 is a constant that you've included in that select statement.
Try this:
INSERT INTO table(col1, col2)
SELECT table2.value1, value2 FROM table2 WHERE table2.id = 3;
http://dev.mysql.com/doc/refman/5.1/en/insert-select.html
take a look especially in the examples.
I would recommend reading full syntax of SELECT, UPDATE, DELETE and INSERT SQL commands to begin with. Then expand to subqueries and DDL.
Go slowly and work out examples.
You definately can. It should work similar as the example below;
INSERT INTO Store_Information (store_name, Sales, Date)
(SELECT store_name, Sales, Date FROM Sales_Information WHERE Year(Date) = 2010)
when you specify the keyword "Values" on the insert statement you are trying to insert just a value. the current way to do that is assigning the value of the "something_from_another_table" into a variable and then, make your insert
DECLARE #ANYVALUE AS VARCHAR(40)
SELECT #ANYVALUE = ANYFIELD FROM table2 WHERE id = 3
INSERT INTO table1 (FIELD1, FIELD2) VALUES(#ANYVALUE, VALUE2)
On this way always will insert one record. the other correct way will insert n record as the where statement can filter.
INSERT INTO Store_Information (store_name, Sales, Date)
(SELECT store_name, Sales, Date FROM Sales_Information WHERE Year(Date) = 2010)