use if else in insert statement plsql - sql

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.

Related

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 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

Merge rows into one row based on condition

I Have a table structure which similar to below
Create Table #Temp(Name varchar(10),Col1 int,Col2 int,Col3 int,Col4 int,Col5 int)
In this case table can have same name repeated but the other values to be different
So.
Sample values can be like
Insert Into #Temp
Values('ABC',1,0,0,1,1)
Insert Into #Temp
Values('ABC',1,0,1,1,0)
Insert Into #Temp
Values('ABC',1,0,1,1,0)
Insert Into #Temp
Values('DEF',0,0,0,1,0)
Insert Into #Temp
Values('DEF',1,0,1,1,1)
Insert Into #Temp
Values('DEF',1,1,0,1,1)
What I am trying to do here is select only one row for each name, but select the column with priority where it has value 1.
So the expected result in this case is
Name Col1 Col2 Col3 Col4 Col5
ABC 1 0 1 1 1
DEF 1 1 1 1 1
I have achieved it by doing something like below, which works absolutely fine. But is there any proper(easy) way of doing this.
SELECT Name,
(Select top 1 Col1
from #Temp T
Where T.Name=M.Name
Order By Col1 desc) as Col1,
(Select top 1 Col2
from #Temp T
Where T.Name=M.Name
Order By Col2 desc) as Col2,
(Select top 1 Col3
from #Temp T
Where T.Name=M.Name
Order By Col3 desc) as Col3,
(Select top 1 Col4
from #Temp T
Where T.Name=M.Name
Order By Col4 desc)as Col4,
(Select top 1 Col5
from #Temp T
Where T.Name=M.Name
Order By Col5 desc) as Col5
FROM #Temp M
Group By Name
It seems to me that you need to use MAX:
SELECT [Name],
MAX(Col1) Col1,
MAX(Col2) Col2,
MAX(Col3) Col3,
MAX(Col4) Col4,
MAX(Col5) Col5
FROM #Temp
GROUP BY [Name]
SELECT
Name,
Max(Col1) as Col1,
Max(Col2) as Col2,
Max(Col3) as Col3,
Max(Col4) as Col4,
Max(Col5) as Col5
FROM #Temp
GROUP BY Name

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.

Switching 1 row with few columns into 1 column with few rows in MS SQL SERVER

i've got 1 row with many columns:
col1|col2|col3|...
and i want to have 1 column with many rows, like that:
col1
col2
col3
..
UNPIVOT if you're using version 2005+
http://www.tsqltutorials.com/unpivot.php
If SQL Server 2000 or lower...
How to rotate a table in SQL Server: http://support.microsoft.com/kb/175574
Take a look at UNPIVOT:
CREATE TABLE Table1 (col1 INT, col2 INT, col3 INT, col4 INT);
INSERT INTO Table1 (col1, col2, col3, col4) VALUES (1,2,3,4);
SELECT col, value FROM Table1
UNPIVOT (value FOR col IN (col1, col2, col3, col4)) AS unpvt
Result:
col value
col1 1
col2 2
col3 3
col4 4
If you don't want to know which value came from which column, only select value:
SELECT value FROM Table1
UNPIVOT (value FOR col IN (col1, col2, col3, col4)) AS unpvt