to fetch the seq_no of the columns in plsql - plsql

How to get
table1
column1
123 -seq_no
table2
column1 column2
123 1
123 2
123 3
how to get the column2 of table2 for column1 seq no of first table.
Please help

Littlefoot,
What you are asking is a very basic question, one that might be expected of someone who has never had any training in PL/SQL. This will get you started, but I strongly suggest you look for some getting started training.
DECLARE
l_col_result table2.column2%TYPE;
BEGIN
SELECT column2
INTO l_col_result
FROM table1 INNER JOIN table2 ON table1.column1 = table2.column2;
DBMS_OUTPUT.put_line (a => l_col_result);
END;

Related

Count field from 2nd table

Here's a sample of what I'm trying to do:
select
t1.Field1
t1.Field2
from Table1 t1
inner join Table2 t2 on t2.Field1 = t1.Field1
where
t1.Field3 like '123-%'
and t1.CreateDate >= '01/01/2021'
having
Count(t2.Field4 = 419) >= 1
2 tables
Table 1 has unique records with an ID (Field 1)
Table 2 has multiple records, with ID Field 1 as well. Table2 may have 10, 20, etc records for Field1. I'm wanting to pull Table1 records where Table2 as at least 1 occurrence of Field4 = 419. Table2 may not have any Field4=419, it may have 1, or it may have 2 or more.
Pretty straight forward I think, but unfortunately I'm new to SQL writing which I why I'm posting for help as I've tried several ways to get this to work without any luck.
Normally you use having when you have a group by in your query.
There will be multiple ways of writing what you want, but one way that I think is easy to understand is selecting from Table1 and doing the count of Table2 in a subquery of the where clause. Depending on the size of your tables and the indexing, you might want to explore other options, but I think this is at least a good starting point that does what you want.
select
t1.Field1
t1.Field2
from Table1 t1
where
t1.Field3 like '123-%'
and t1.CreateDate >= '01/01/2021'
and (SELECT count(*) FROM Table2 t2 WHERE t2.Field1 = t1.Field1 AND t2.Field4 = 419) >= 1

Update table if any value =

I'm trying to update my table if any value from table1 = value from table2
Table1 as 1 column with data
Table2 as many columns with data
If table2(data) = table1(data) update
But isn't working
I had one code that was working if i set table2 with 1 column
This one is working but table2 needs to have 1 column only
UPDATE table1
SET column1 = 'correct'
WHERE column2 in (SELECT column1 from table2);
I want to be able to do having more column
maybe something like this:
UPDATE
SET column1 = 'correct'
WHERE column2 in (SELECT * from table2);
The error:
Result: sub-select returns 11 columns - expected 1
How should I do it?
Maybe you can do it with EXISTS:
UPDATE table1
SET column1 = 'correct'
WHERE EXISTS (
SELECT 1 FROM table2
WHERE table1.somecolumn = table2.someothercolumn
);
If you want to check table1.someothercolumn against multiple columns of table2:
UPDATE table1
SET column1 = 'correct'
WHERE EXISTS (
SELECT 1 FROM table2
WHERE table1.somecolumn in (table2.col1, table2.col2, ...)
);

A very simple CASE statement in sqlite

If an entry in column1 is larger than or equal to 0 then it should print 1 in column2. Else it should print a 0.
The table looks like this:
create table table1 (column1 REAL);
insert into table1 values (8);
insert into table1 values (-10);
insert into table1 values (53);
And this is the query:
SELECT column1
CASE
WHEN column1 >=0 THEN 1
ELSE 0
END as column2
FROM table1;
So simple, but not working. Any help is greatly appreciated!!!
Result columns must be separated with a comma:
SELECT column1,
CASE ... END AS column2
FROM table1;
You are missing a comma after the column1 column name.
SELECT column1,
CASE
WHEN column1 >=0 THEN 1
ELSE 0
END as column2
FROM table1;

Updating a table with the columns of another

Relatively new to Oracle. I am trying to replace the columns of one table with the columns of another. Here is my code:
update ems.ptnaddress p
set (p.ptnid, p.address1,p.address2, p.address3,p.address4) =
(select p2.ptnid, p2.address1,p2.address2, p2.address3,p2.address4
from tempptnaddress p2
where p.ptnid = p2.ptnid);
I am getting the error :
SQL Error: ORA-01427: single-row subquery returns more than one row
Any ideas of what to do?
Basically your tempptnaddress table has more than one row for at least some of the ptnid values and Oracle isn't going to pick which one of those rows to use to do the update for you. :)
For example, if I have table1 as
COL1 COL2 COL3
1 ONE ENG
1 UNO SPA
2 TWO ENG
3 THREE ENG
4 FOUR ENG
4 CUATRO SPA
and table2 as
COL1 COL2
1
2
3
4
and try and update TABLE2.COL2 using something like:
UPDATE table2 t2
SET col2 = (SELECT col2 FROM table1 t1 WHERE t1.col1 = t2.col1)
what value in column TABLE1.COL2 should it use for 1 and 4? Oracle won't guess for us and that's when you get the ORA-01427.
It might be as easy as just picking one arbitrarily like:
UPDATE table2 t2
SET col2 = (SELECT col2 FROM table1 t1 WHERE t1.col1 = t2.col2 AND ROWNUM = 1)
but you probably want to put in some proper logic there like:
UPDATE table2 t2
SET col2 = (SELECT t1.col2 FROM table1 t1 WHERE t1.col1 = t2.col1 AND t1.col3 = 'ENG')
In your case, you need to get it so that the subquery in your update only returns one row per ptnid.
If you run this:
SELECT ptnid, COUNT(*)
FROM tempptnaddress
GROUP BY ptnid
HAVING COUNT(*) > 1
it will show you what ptnids that have more than one row in tempptnaddress. The trick will be to find out why there is more than one row per ptnid and how to pick the right one to use for the update.
Try this
UPDATE (SELECT p.ptnid ptnid1,p.address1 Addressnew1, p.address2 p.address3 p.address4 Addressnew4,p.ptnid ptnid2, p.address1 Addressold1, p.address2 p.address3 Addressold3, p.address4 Addressold4 FROM ems.ptnaddress p,tempptnaddress p2 WHERE p.ptnid = p2.ptnid) SET ptnid1 = ptnid2,Addressnew1 = Addressold1, Addressnew2 = Addressold2,Addressnew3 = Addressold3,Addressnew4 = Addressold4

How to return dynamic cursor from oracle stored procedure

I have 2 tables in which ID field is common.
I am fetching all records of first table in a cursor.
Then I want to do is that on the basis of each ID from cursor, I want to get the values from second table and then return that.
How can I do that...
Please help !!!
homework?
this is basic SQL. generally you'd join the two tables.
begin
for r_row in (select b.*
from tab1 a
inner join tab2 b
on b.id = a.id)
loop
null; -- do whatever
end loop;
end;
/
if you have an existing cursor and can't change it
eg where your_cursor is just returning an ID column.
begin
open your_cursor;
loop
fetch your_cursor into v_id;
exit when your_cursor%notfound;
for r_row in (select * from tab2 b where b.id = v_id)
loop
null; -- do whatever here.
end loop;
end loop;
end;
/
edit:
as per comments:
some sample data:
SQL> create table table1 (id number primary key, name varchar2(20));
Table created.
SQL> create table table2 (id number, col1 varchar2(20), col2 varchar2(20));
Table created.
SQL> insert into table1 values (1, 'test');
1 row created.
SQL> insert into table1 values (2, 'foo');
1 row created.
SQL>
SQL> insert into table2 values (1, 'John', 'Smith');
1 row created.
SQL> insert into table2 values (1, 'Peter', 'Jones');
1 row created.
SQL> insert into table2 values (1, 'Jane', 'Doe');
1 row created.
SQL> insert into table2 values (2, 'Nina', 'Austin');
1 row created.
SQL> insert into table2 values (2, 'Naman', 'Goyal');
1 row created.
SQL> commit;
Commit complete.
create a type to hold the return structure. note the datatypes NEED to match the datatypes of the tables table1 and table2 (%type won't work, so make sure they match)
SQL> create type my_obj as object (
2 id number,
3 name varchar2(20),
4 col1 varchar2(20),
5 col2 varchar2(20)
6 );
7 /
Type created.
SQL> create type my_tab as table of my_obj;
2 /
Type created.
now create your function (you can put this in a package if, in your real code, you have it that way).
SQL> create function function1
2 return my_tab pipelined
3 is
4 begin
5 for r_row in (select t1.id, t1.name, t2.col1, t2.col2
6 from table1 t1
7 inner join table2 t2
8 on t1.id = t2.id)
9 loop
10 pipe row(my_obj(r_row.id, r_row.name, r_row.col1, r_row.col2));
11 end loop;
12 end;
13 /
Function created.
SQL>
SQL> select *
2 from table(function1);
ID NAME COL1 COL2
---------- -------------------- -------------------- --------------------
1 test John Smith
1 test Peter Jones
1 test Jane Doe
2 foo Nina Austin
2 foo Naman Goyal
you could pass inputs if required into that function eg table(function1('a', 'b')); etc..

Resources