An insert query inserts only one row - sql

When I try to use this :
insert into mytable1
(whatever)
values
(select whatever from mytable2 where accomplished=True);
Only one row gets inserted though there should be 3.
Why is this ?

Depending on the implementation, the statement you have above inserts into one row as many values as you have fields in the (whatever) part.
You should try this instead:
INSERT INTO table2
SELECT * FROM table1;
Check this site out for more information:
http://www.w3schools.com/sql/sql_insert_into_select.asp

Related

Could insert statements match data from another table

I am trying to do an insert on a table from another table with matched values and IDs.
lets say on table1 there are values of names and IDS. Like John and 55.
I am trying to only insert the 55 into table2 since John is already on table2 but just missing his ID.
I know I can do update statement to update the value for John to 55, but my tables have over 3000 values, and it will be hard to do one at a time:
Anyway I can write a query to enter a value into the other table as long as the names match together?
what I tried so far:
insert into desired_table (id,version,source_id,description,r_id)
SELECT HI_SEQUENCE.nextval,'0', select min (id)
from table
where name in (select name from table2 where table2_name is not null),
table2_name,
table2.r_id from table2 where name is not null;
Issue with this statement is it inserts multiple values, but it only inserts it into where the min ID is.
Anyway I can adjust this and have it pull more than one ID?
Use Merge statement (https://learn.microsoft.com/en-us/sql/t-sql/statements/merge-transact-sql?view=sql-server-ver15)
Merge into Table1 d
Using Table 2 s
on d.name=s.name
when matching then update
age=s.age
when not matching then insert
(col1, col2)
values (s.col1, s.col2);
You might want a trigger to automate the above task.
Create Trigger sample after insert on
Table1 for each row
Begin
Update table2 set table2.age = :NEW.AGE where
table2.id=:NEW.Id
END;
Got this working by generating insert statements and running them with insert all

Why is 'insert into select' statement in SQL inserting as a new row and not inserting correctly?

I have table 1.
I have another empty table 2 with the following columns.
I want to insert into table 2 by selecting from table 1 - so I write the query as:
insert into table2(employee,id,zone,url)
select employee, id, zone, concat('https://',employee,'.com/',id,'?',zone)
from table1
Now my table 2 looks like this,
Now for the authcode column, I do the following and insert it into the table2.
insert into table2(authcode)
SELECT CONVERT(VARCHAR(10),HASHBYTES('MD5', substring(URL,8,100)),2)
from table2.
But the insert happens differently like this AS AN ENTIRE NEW SET OF ROWS.
Can someone help me to insert the last column to the corresponding rows instead of it creating a new one?
What you should be doing is UPDATE the table to fill the column authcode, but you could do it all in 1 step while you are inserting the rows:
insert into table2(employee,id,zone,url, authcode)
select
employee,
id,
zone,
concat('https://',employee,'.com/',id,'?',zone),
CONVERT(VARCHAR(10),HASHBYTES('MD5', substring(concat('https://',employee,'.com/',id,'?',zone),8,100)),2)
from table1
or if you want to update:
update table2
set authcode = CONVERT(VARCHAR(10),HASHBYTES('MD5', substring(URL,8,100)),2)
where authcode is null
The result you are seeing is the intended behavior for an INSERT statement. It will always insert new rows.
If you want to modify existing rows your need to use an UPDATE statement.
You can either modify your INSERT to look like what #forpas has posted to get all this work done in one step. Another option is to modify the second INSERT to be an UPDATE like the following:
update table2
set authcode = CONVERT(VARCHAR(10),HASHBYTES('MD5', substring(URL,8,100)),2)

sql server add a row with same records except for ID

My code is as below"
select * into tbltemp
from table1 where ID='12345'
update tbltemp set ID='54321'where ID='12345'
insert into table1
select * from tbltemp where ID='54321'
drop table tbltemp
When executing insert into query, I got error saying 'Insert Error: Column name or number of supplied values does not match table definition.'
I wonder how I can deal with that?
My table1 has 50 columns with three computed columns.
Thanks for advice!
table1 and tbltemp must match by number of columns. You must explicitly name the columns do not use the * sign in insert into from select, if number of columns do not match.
I just realized that when I have computed columns, the query doesn't work well.
So I delete the computed columns before copy a new one, then do an insert into select *, then add computed columns back, in this way I can save the time for writing 50 fields.
You can't insert a computed column. You need to select especific fields in select and in value() statements. No select *

Trigger to insert values in table2, when values are inserted in table1

I want to write trigger, when values are inserted in table1, same values must get added in table2.
create trigger TestTrigger
on table1
after insert
as
insert into table2
select *
from inserted
go
The assumption here is that table2 and table1 have the same amount of columns with compatible field types (that is needed when you do a "select star" with implicit fields).
If you want to put inserted values that were inserted into table1, you can utilize the inserted table (and deleted as well for DELETE and UPDATE statements).

Is it possible to copy some rows from one table to the other along with rowids?

If I type:
INSERT INTO table_b
SELECT rowid, somecolumn
FROM table_a
...the rowid column would copy into new table as ordinary column and this would most likely produce an error since columns wouldn't match.
But is there a way to copy exactly the same rowids from old table to new when I'm populating it fresh ?
I know it is possible to do it that way:
INSERT INTO table_b
(rowid, othercolumn)
VALUES (334, 'sometext')
...but that needs to be write row by row instead of one line sql command.
The first SQL you write is correct and will copy all information matching the columns.
You can also use a query like this:
INSERT INTO table2( rowId, rowValue)
SELECT rowId, rowValue FROM table1
Have you tried this:
INSERT INTO table_b (target_name, target_name2)
SELECT rowid, somecolumn
FROM table_a
Should work fine. But I've not done it on sqlite...