SQL: use SELECT to INSERT data into table with extra column - sql

I have a table Table1 with 59 columns. Table2 is a copy of Table1 with one extra column, COLUMN 60 at the end. Hence table2 has 60 columns.
I am trying to copy the values from table1 to table2 and set value of the extra column in table2 to "value"
Something like this
INSERT INTO Table2
SELECT * FROM Table1, 'value' AS 'COLUMN 60'
How can I do this? Using the code above give me an error:
An explicit value for the identity column in table 'TableLocation' can only be specified when a column list is used and IDENTITY_INSERT
is ON.
I do not want to specify column names because there are too many of those.

Try this:
SET IDENTITY_INSERT dbo.Table2 ON
INSERT INTO Table2
SELECT *, 'value' AS 'COLUMN 60' FROM Table1
SET IDENTITY_INSERT dbo.Table2 OFF

You know that you can just drag the column names from SSMS right? Navigate to the table and then drag the columns folder into the query window, it will list all the columns for you
Now if you want to preserve the identity values then use
SET IDENTITY_INSERT dbo.Table2 ON
--INSERT HERE
-- make sure to list all the columns, it is required
SET IDENTITY_INSERT dbo.Table2 OFF

Does it really need to be done in one statement?
INSERT INTO Table2
SELECT * FROM Table1
ALTER TABLE Table2
ADD Column60 varchar

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

Insert Data From One Table to Another Table with default values

The following query works fine but I have a different requirement.
My requirement is as follows.
I have table1 and table2, table1 contains 5 columns and table2 contains 3 columns. Now, we need to pull data from table2 and insert into table1. Here in table2 contains only 3 columns and table1 contains 5 columns. So the rest should insert with some default values.
Could someone help me how to write a query for this.
Change the default values 1 and 2 here
Insert into table1(col1,col2,col3,col4,col5)
select col1,col2,col3,1,2 from table2
You can simply ignore default value colums in Insert statements.
Insert into table1(col1,col2,col3)
select col1,col2,col3 from table2
It'll have default values in col4 & 5.

Need to add a constant value in a column while loading a hive table

I created a table named table1 in hive and i need to insert data from table2 into table1. I used the below statemnt to get the output.
Also i need to add a new column with some constant value -- colx = 'colval' along with the columns in table2 but am not sure how to add it.. Thanks!
INSERT INTO TABLE table1 select * FROM table2;
If you are willing to drop table1 and recreate it from scratch, you could do this:
-- I'm using Hive 0.13.0
DROP TABLE IF EXISTS table1;
CREATE TABLE table1 AS SELECT *, 'colval' AS colx FROM TABLE2;
If that is not an option for some reason, you can use INSERT OVERWRITE:
ALTER TABLE table1 ADD COLUMNS (colx STRING); -- Assuming you haven't created the column already
INSERT OVERWRITE TABLE table1 SELECT *, 'colval' FROM table2;

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 *

SQL Insert from other table with Same Schema

Table1 and Table2 have same schema, same columns and same types, and Table2 is empty while Table1 has some data
Insert into Table2 values(Select * from Table1)
how to transfer the data with sql statement? i think syntax is valid in oracle, but how to do with sql-server
You can leave out the values statement:
insert into table2
select * from table1
That said, you should really be in the habit of listing column names, both for the insert and select in this case. The columns could have the same name and type -- but be in different order.
You might possibly want to drop table 2 and then do a select * into table2 from table1. This way you are guaranteed to have the same structure. Because when somebody changes the structure of either table, but not the other, insert into will bomb.