Insert different values everytime when using insert into - sql

I have a table which has 3 columns and 6 records, I want to insert the values in another table which has an additional column of sysid, when I use insert into clause it inserts the records but the sysid value is same , I want different values everytime a record insert but I want to insert in bulk as well

Use a sequence:
CREATE SEQUENCE table_name__sysid__seq;
Then:
INSERT INTO table_name (sysid, col1, col2, col3, col4, col5, col6)
VALUES (table_name__sysid__seq.NEXTVAL, 'value1', 'value2', 'value3', 'value4', 'value5', 'value6');
or, for multiple rows:
INSERT INTO table_name (sysid, col1, col2, col3, col4, col5, col6)
SELECT table_name__sysid__seq.NEXTVAL, col1, col2, col3, col4, col5, col6
FROM first_table;
Or else, if you are using Oracle 12 or later, define sysid as an IDENTITY column:
CREATE TABLE table_name (
sysid NUMBER
GENERATED ALWAYS AS IDENTITY
PRIMARY KEY,
col1 VARCHAR2(20),
col2 VARCHAR2(20),
col3 VARCHAR2(20),
col4 VARCHAR2(20),
col5 VARCHAR2(20),
col6 VARCHAR2(20)
);
db<>fiddle here

Related

Perform same set of data manipulations on all tables in an SQLite database

I have an SQLite database with tables having a poor schema. Basically, there is no index column which acts like a primary key, so I wish to add a column that has an auto incremental value , to the existing set of rows.
For achieving the above I applied the following SQL commands to a particular table say table_orig. I create a new table named table1 and insert rows from table_orig into it.
create table table1(id integer primary key autoincrement, col1 varchar, col2 varchar, col3 float, col4 float, col5 float, col6 float, col7 float);
insert into table1(col1, col2, col3, col4, col5, col6, col7) select col1, col2, col3, col4, col5, col6, col7 from table_orig;
drop table table_orig;
alter table table1 rename to table_orig;
Now, there are 100 tables in my database and I wish to apply these commands to each of them. How do I automate this, as I don't want to manually do it for each of them?
Any help will be appreciated. Thank you in advance.

Insert extra columns with NULL values from one table to another table HIVE

Table1 have columns col1 , col2 ,col3 , col4 , col5
Table2 have columns col1 , col3 , col5
I want to insert rows from Table2 to Table1
But col2 , col4 should be NULL datatype after inserting into Table2
How can I do it in HIVE , Currently I am using Hortonworks 3.1 version
You just use insert . . . select:
insert into table1 (col1, col2, col3, col4, col5)
select col1, null, col3, null, col5
from table2;

How to Insert Into Table Values combining arguments and temp table?

I have INSERT statement where I have to use one of my argument values and values from my temporary table. I'm using SQL Server 2008. I haven't worked with temp tables before so I'm not sure if and how I can combine argument values and temp table since arguments have to be placed inside of the VALUES() and temp table values are pulled using SELECT. Here is my code:
INSERT INTO myTbl (col1, col2, col3, col4, col5, col6)
//col1 should use #arguments.myVal#
SELECT #col2,#col3,#col4,#col5,#col6
FROM #myTemp
Is there any way to combine values from two different sources into insert statement?
hope this what you are looking for.
Declare #Param1 int = 20
INSERT INTO myTbl (col1, col2, col3, col4, col5, col6)
SELECT #Param1, col2, col3, col4, col5, col6
FROM #myTemp

How to use insert into select to insert both values from select and known values in Oracle?

I have 5 columns out of which I have known values for 2 columns.
I have to insert values for 3 columsn using INSERT INTO...SELECT statement and the code is working.
I only want to know how do I combine both INSERT INTO SELECT with known values in ORACLE.
insert into destination (col1, col2, col3, col4, col5)
select 'static_value1', 'static_value2', col6, col7, col8
from source_table

Sql insert select from – multiple rows with unique column id

I am trying to copy multiple records using one query using insert select from.
Insert into tab_A(colId, col1, col2, col3)
Select colId, col1, col2, col3 form tab_A
Where colId in ( 2,4,6)
Would it be possible to assign different colId for new entries? For example colid 2 should be replaced with 23, 4 with 24 and 6 with 25. How could I achieve it in a single query?
this would work
Insert into tab_A(colId, col1, col2, col3)
Select 23 , col1, col2, col3 form tab_A Where colId = 2 UNION ALL
Select 24 , col1, col2, col3 form tab_A Where colId = 4 UNION ALL
Select 25 , col1, col2, col3 form tab_A Where colId = 6
If you give some more info I could provide somthing more reusable. Should/is colId (be) an identity column?
EDIT
This would work in this very specialised case
Insert into tab_A(colId, col1, col2, col3)
Select ((colId - 4) * (-1)) + colId + 20 , col1, col2, col3
form tab_A Where colId IN (2, 4, 6)
The function newId = ((oldId - 4) * (-1)) + oldId + 20 is obviously specific to the stated problem.
EDIT2
I suspect somthing like this is more generic approach is appropriate.
DECLARE #MaxColID INT
BEGIN TRANSACTION
SELECT #MaxColID = MAX(ColID) FROM tab_A
INSERT tab_A(colId, col1, col2, col3)
SELECT row + #MaxColID, col1, col2, col3
FROM
(
SELECT ROW_NUMBER() OVER (ORDER BY ColID) row, col1, col2, col3
FROM tab_A WHERE colID IN (2, 4, 6)
)
COMMIT
EDIT 3
If you think EDIT 2 is actually what you want then you really want to make ColID an IDENTITY column, then you could do this.
INSERT tab_A (col1, col2, col3)
SELECT col1, col2, col3 FROM tab_A WHERE colId IN (2, 4, 6)
I dont see col4 or col6 in your query, but is this what you want:
Insert into tab_A(colId, col1, col2, col3)
Select colId, col1, 23, col3 form tab_A
Where colId in ( 2,4,6)
have you just tried adding the disired difference to colId -
In your case, since you need to replace 2 by 23, difference is 21.
Insert into tab_A(colId, col1, col2, col3)
Select colId+21, col1, col2, col3
form tab_A Where colId in ( 2,4,6)
Note: I missed the part, that the differnce is not consistent in your case.
The proposed solution will work only if difference is same
There are a few options:
Add the new ID column to the original table and populate it with the new values before you do this insert, selecting the new ID column instead of the old. This would be the tidiest solution I think.
Alternative - Modify the ID value on the insert based on a rule e.g.
INSERT INTO tab_A(colID, col1, col2, col3)
SELECT colId + 20, col1, col2, col3
FROM tab_A
WHERE colID IN(2,4,6)
Last resort - Process the insert sequentially with a cursor, modifying the ID value each time.
You could also write case in the select. when 2 then 23 or whatever value.