Insert into a table using values from another - sql

I am trying to perform an insert using the following command:
insert into table2(COL1, COL2, COL3, COL4) values((select COL1 FROM table1 WHERE COL1 = 121212),120,10,"Y");
But I get the following error:
ERROR at line 1: ORA-00984: column not allowed here
Any help?

INSERT INTO table2( col1, col2, col3, col4 )
SELECT col1, 120, 10, 'Y'
FROM table1
WHERE col1 = 121212
should work.

Related

use if else in insert statement plsql

i have plsql code that insert data from table1 to table2
and i want to put if statment to insert certain value in a specific column and this is my code
for rec_table1 in (
select * from table1
where salary > 100)
loop
insert into table2 (
col1 , col2 , col3 col4 ,col5)
values
(rec_table1.col1 ,
rec_table1.col2,
rec_table1.col3 ,
rec_table1.col4 ,
if (rec_table1.col2 = 1)
then
rec_table1.col2
else
rec_table1.col5
end if
but it give me syntax error at the if statment
A plain insert into with case would do just fine. There's no need to go for loops.
INSERT INTO table2 (
col1,col2,col3,col4,col5)
select col1 , col2,col3 col4,CASE col2
WHEN 1 THEN col2
ELSE col5
END AS col5
FROM table1 where salary > 100;
IF is a PL/SQL statement and can't be used inside a SQL statement.
You can use a SQL CASE expression:
for rec_table1 in (select * from table1
where salary > 100)
loop
insert into table2 (col1, col2, col3, col4, col5)
values (rec_table1.col1, rec_table1.col2, rec_table1.col3, rec_table1.col4,
case
when rec_table1.col2 = 1 then rec_table1.col2
else rec_table1.col5
end);
END LOOP;
However the whole loop is inefficient and slow. You don't need it. This can be written as a single INSERT statement which will perform a lot better.
You can replace the complete LOOP .. END LOOP; part with the following INSERT statement:
insert into table2 (col1, col2, col3, col4, col5)
select col1, col2, col3, col4,
case
when col2 = 1 then col2
else col5
end
from table1
where salary > 100;
You can manage it within one statement by using decode() as :
insert into table2 (col1 , col2 , col3 col4 ,col5)
select col1 , decode(col2,1,col2,col5) , col3, col4 ,col5
from table1
where salary > 100;
OR by using case..when expression as :
insert into table2 (col1 , col2 , col3 col4 ,col5)
select col1 , case when col2=1 then col2 else col5 end , col3, col4 ,col5
from table1
where salary > 100;
P.S. decode() is spesific to Oracle DB, case..when is generic.
I believe, instead of If-Else, but you would have to implement Case-When syntax.
Here's the documentation: Case-When
First focus on constructing the correct Select statement, then you can just insert values using output of the select query.

How to get NULL as values for invalid column

I'm using Sql Server 2012. Consider the table schema to be,
create table A (col1 int, col2 int)
I'm trying to execute this query,
select col1, col2, col3, col4 from A
I get execution error as col3 and col4 are not in table.
But is there any way, these 2 columns can be displayed in result with NULL as value for every row, even though it's not available in the table?
Use an alias for each one of these 2 columns:
select col1, col2, null as col3, null as col4 from A
Try below select query..
select col1, col2, null as col3, null as col4 from A
Cast Null as desired data type:
select col1, col2, cast(null as int) as col3, cast(null as int) as col4 from A

TSQL INSERT INTO SELECT - display rows

Is it possible to display which rows I did inserted via this query:
INSERT INTO dbo.Table (Col1, Col2, Col2)
(SELECT
Col1,
Col2,
'Something modified',
FROM dbo.Table
WHERE Col10 = 66)
It is important to obtain whole new row with it's PrimaryKey value, etc., not only Col1, Col2, Col3
The OUTPUT clause is your friend now:
INSERT INTO steam.DocumentGeographies (Col1, Col2, Col3)
OUTPUT inserted.*
(SELECT
Col1,
Col2,
'Something modified',
FROM dbo.Table
WHERE Col10 = 66)
You can insert the result of the OUTPUT by specifying the destination table with the INTO clause or by wrapping the query as a subquery:
INTO clause
This is useful, when you want to insert the same data into two tables. You can always list the required fileds in the OUTPUT clause (inserted.Col1, inserted.Col2)
INSERT INTO steam.DocumentGeographies (Col1, Col2, Col3)
OUTPUT inserted.* INTO DestinationTableName(Col1, Col2, Col3)
(SELECT
Col1,
Col2,
'Something modified',
FROM dbo.Table
WHERE Col10 = 66)
SUBQUERY
This is useful, when you want to join the OUTPUT to another tables or you want to make calculations (like summing or counting values) and insert those results into another table.
INSERT INTO DestinationTableName
(Col1, Col2, Col3)
SELECT Col1, Col2, Col3 FROM (
INSERT INTO steam.DocumentGeographies (Col1, Col2, Col3)
OUTPUT inserted.*
(SELECT
Col1,
Col2,
'Something modified',
FROM dbo.Table
WHERE Col10 = 66)
) TMP

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.

Stored procedure for inserting a constant plus values from another table

How to modify the following stored procedure if we want in the inserted records the Col3 contain a constant string 'Foo'?
INSERT Table2
(Col1,
Col2,
Col3)
SELECT T1Col1,
T1Col2
FROM Table1
INSERT Table2
(Col1,
Col2,
Col3)
SELECT T1Col1,
T1Col2,
'Something'
FROM Table1
INSERT Table2
(Col1,
Col2,
Col3)
SELECT T1Col1,
T1Col2,
'Foo'
FROM Table1