Insert Data From One Table to Another Table with default values - sql

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.

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

How to select data and insert those data using single sql?

I want to select some data using simple sql and insert those data into another table. Both table are same. Data types and column names all are same. Simply those are temporary table of masters table. Using single sql I want to insert those data into another table and in the where condition I check E_ID=? checking part. My another problem is sometime there may be any matching rows in the table. In that time is it may be out sql exception? Another problem is it may be multiple matching rows. That means one E_ID may have multiple rows. As a example in my attachment_master and attachments_temp table has multiple rows for one single ID. How do I solve those problems? I have another problem. My master table data can insert temp table using following code. But I want to change only one column and others are same data. Because I want to change temp table status column.
insert into dates_temp_table SELECT * FROM master_dates_table where e_id=?;
In here all data insert into my dates_temp_table. But I want to add all column data and change only dates_temp_table status column as "Modified". How should I change this code?
You could try this:
insert into table1 ( col1, col2, col3,.... )
SELECT col1, col2, col3, ....
FROM table2 where (you can check any condition here on table1 or table2 or mixed)
For more info have a look here and this similar question
Hope it may help you.
EDit : If I understand your requirement properly then this may be a helpful solution for you:
insert into table1 ( col-1, col-2, col-3,...., col-n, <Your modification col name here> )
SELECT col-1, col-2, col-3,...., col-n, 'modified'
FROM table2 where table1.e_id=<your id value here>
As per your comment in above other answer:
"I send my E_ID. I don't want to matching and get. I send my E_ID and
if that ID available I insert those data into my temp table and change
temp table status as 'Modified' and otherwise don't do anything."
As according to your above statements, If given e_id is there it will copy all the columns values to your table1 and will place a value 'modified' in the 'status' column of your table1
For more info look here
You can use merge statement if I understand your requirement correctly.
Documentation
As I do not have your table structure below is based on assumption, see whether this cater your requirement. I am assuming that e_id is primary key or change as per your table design.
MERGE INTO dates_temp_table trgt
USING (SELECT * FROM master_dates_table WHERE e_id=100) src
ON (trgt.prm_key = src.prm_key)
WHEN NOT MATCHED
THEN
INSERT (trgt.col, trgt.col2, trgt.status)
VALUES (src.col, src.col2, 'Modified');
More information and examples here
insert into tablename( column1, column2, column3,column4 ) SELECT column1,
column2, column3,column4 from anothertablename where tablename.ID=anothertablename.ID
IF multiple values are there then it will return the last result..If not you have narrow your search..

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.

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

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

PostgreSql Function "Insert data from a table to an other"

I want to make a function in PostgreSQL that would make the following:
First of all read some data from a table lets say “Select col1,col2 from table1”
Then for each row of the above selection I want to make an insert to an other table lets say table2 (that contains some extra columns like date and so on).
For each insertion I want a unique key that starts from a given number and is increased in every new row.
Can someone give me an example about how I can do it?
I need to be more specific
I want to do what is discribed below:
For(every row in table1)
if(table1.col1>0)
insert into table2 (c1,c2,c3,c4) nalues (id,table1.col1,table1.col2,'oposite',current_timestamp)
else if(table1.col1<0)
insert into table2 (c1,c2,c3,c4) nalues (id,table1.col1,table1.col2,'negative',current_timestamp)
id+=1
insert table2(id, col1, col2)
select startingpoint-1+row_number() over (order by col1, col2),
col1,
col2
from table1
You can use select into to do this, just set up the second table with an autoincrementing field and it will give you your unique key.