Looping through multiple results in PLSQL [closed] - plsql

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have a select query which returns 50 results. The select query returns id .
The next part i need to run a select query which value for all the 50 ids.
Please can some one tell me what is the best way to get it done.
is using a for loop a good idea
e.g.
begin
declare #count= select id from table1
for 1 to #count ..loop
select value from table2 where id =1
end loop
Can i use cursors what is the best method.

Please elaborate on this statement "The next part i need to run a select query which value for all the 50 ids."
However, if you want to fetch those 50 records in a pl/sql block and manipulate them in some way, you would require a cursor. Records from cursor can be fetched both with and without FOR loop.
Cursor example:
declare
cursor c1 is select col1 from table1;
begin
for a in c1
loop
dbms_output.put_line(a.col1);
end loop;
end;
The above piece of code would display the values from col1 when server output is on.

In a simple case such as you suggest I think it's a bad idea to build up a collection of values and then iterate through them to fetch data from a second table. It's a better idea to use a JOIN to, well, join together the data from the two tables based on whatever criteria you've got. In this case it appears you're select an ID value from TABLE1 which is also on TABLE2. Thus, you might consider something like the following:
FOR myRow IN (SELECT t1.ID, t2.VALUE
FROM TABLE1 t1
INNER JOIN TABLE2 t2
ON (t2.ID = t1.ID))
LOOP
-- Do something useful with the data
DBMS_OUTPUT.PUT_LINE('t1.ID=' || myRow.ID || ' t2.VALUE=' || t2.VALUE);
END LOOP;
Share and enjoy.

Given below can be onw good waye
declare
cursor c1 is select value from table2 where id in (select id from table1)
begin
for i in c1
loop
...
...
end loop;
end;

Related

How to get number of rows updated by a query in .net

In my database, i have a trigger which insert the change log entries when a row in Table tblA is updated.
Now, in my code i have to update it through a plain Sql query like
int count = DBContext.ExecuteStoreCommand("<sql query to update records>");
This count variable contains the number of rows affected(no of rows updated + no of rows inserted) due to query.
So my question is, How do i can get only the number of updated rows?
Currently i'm using Entity framework 4. I have looked for solution through connected or disconnected model but couldn't help myself.
int count = DBContext.ExecuteStoreCommand("");
I think you hv to change this to return Select result set
then do this,
<sql query to update>
Select ##RowCount rowcountAffected
Or
suppose your update is
update table1 set col1='foo' where id=2
select count(*) rowcountAffected from table1 where id=2
The most efficient way to return row affected can be
i) Assuming you only update (don't refresh any record after that)
Put Set Nocount ON
Declare #Output parameter inside proc

PL/SQL fill tables

hello guys it's my first time posting a question but i really need help
i'am actually new in programming with SQL and i 'am now faced to too many problemes espcially with this exercice
we had 4 tables
CLient (numcli , name , address,Towncli )
account (numacc , money,#numcli,#numag)
Agence (numag,nameag,townag)
clientel (numcli,numag)
now we had an empty table "STATES" that we need to fill it with informations from the 4 table
STATES (numcli,countaccount,countag,summoney,taux)
taux must be initialised to 0
values in STRONG means primary key
values in # means foreign key
so i write this BLOC pl/sql but it has a problem
DEclare
cursor c1 is select
client1.numcli,count(numacc),sum(money),count(numag),taux from account ,states group by (numcli) ;
begin
for c2 in c1
loop
c2.taux:='0' ;
insert into states (numcli,countaccount,countag,summoney,taux) values c2 ;
end loop ;
end ;
he said for me oracle values is not enough something like that
can any one helps me
Insights:
1) What does in "client1.numcli" client1 stand for? Its misleading. Is this numcli is from table account?
2) Cursor "from" clause, you state two tables, but here is no joining. Mistype? If no, join them;
3) Cursor attributes are listed as numcli,count(numac),sum(money),count(numag) and taux, but in insert statement attributes are listed in different order: numcli, countaccount,countag,summoney,taux;
4) You cant insert into tables like that, with ".. values c2;" in this situation. You need to declare them one-by-one.
As for solution, if you purpose is just to populate table, you can do this:
insert into states(numcli,countaccount,countag,summoney,taux)
select
acn.numcli,
count(acn.numacc) countaccount,
count(acn.numag) countag,
sum(acn.money) summoney,
0 taux
from
account acn
group by
acn.numcli;
There is no need for cursor here.
Hope it helps.
Good luck in exploring PL/SQL world!

Create an index-by table and populate with rows from emp, then loop over and print them [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Create an index-by table and populate with rows from emp, then loop over and print them.
hi can anyone understand this question and give me the suggestions to solve this please..
A collection is an ordered group of elements having the same data type
which can be accessed though subscript/index. Oracle PL/SQL supports
three different types of collection
Index-by tables or Associative array
Nested table
Variable-size array or Varray
Below is an example of how you can define a Index-by table and populate it with rows from Emp table and print them accordingly.
DECLARE
CURSOR c_emp is
select name from emp;
TYPE c_list IS TABLE of emp.name%type INDEX BY binary_integer;
name_list c_list;
counter integer :=0;
BEGIN
FOR n IN c_emp LOOP
counter := counter +1;
name_list(counter) := n.name;
dbms_output.put_line('Employee('||counter|| '):'||name_list(counter));
END LOOP;
END;
/
See Oracle Documentation as well as PL/SQL - Collections for more information on the same.

prevent repeating data retrieved from SQL database by a select statement

I have a database with a question table each question has a level attribute, topic, and the answers. I want to pick up randomly question in an ASP.net project but i don't want the same question to be repeated in the Details View.
This is the select statement:
SELECT TOP 3 [Question Number] AS Question_Number
,[Question Title] AS Question_Title
,[Answer 1] AS Answer_1
,[Answer 2] AS Answer_2
,[Answer 3] AS Answer_3
,QuizID
,Level
FROM Question
WHERE ( Level = 1 )
ORDER BY NEWID()
I don't think so you will get duplicate row until unless you have duplicate record in table. if you have then use DISTINCT to get unique record from table.
The basic syntax of DISTINCT keyword to eliminate duplicate records is as follows:
SELECT DISTINCT column1, column2,...columnN FROM table_name WHERE [condition]
http://www.w3schools.com/sql/sql_distinct.asp

Select for Update sql is a read and write mode?

I have simultaneous request to a particular row in a table and PL/SQL statement is used to update the table by reading the data from master row in the same table and update the current range row and master row it read.
Algorithm is like this:-
Declare
variable declaration
BEGIN
Select (Values) into (values1) from table where <condition1> for update;
select count(*) into tempval from table where <condition2>;
if (tempval == 0) then
insert into table values(values);
else
select (values) into (values2) from table where <condition2> for update;
update table set (values1) where <condition2>;
end if
update table set (values1+incrval) where <condition1>
END;
Unfortunately the master row is updated properly with the correct sequence but the current range picks up the old value of the master range. It does the dirty read. Even though the transaction isolation level for the table is serialized.
Please could some tell me what is happening here?
This is working as designed. Oracle default, and only, read isolation lets the session see all of their own updates. If you perform:
INSERT INTO TABLE1 (col1) values (1);
COMMIT;
UPDATE TABLE1 SET col1 = 2 where col1 = 1;
SELECT col1 FROM TABLE1;
you will see 2 returned from the last query. Please read the Merge Explanation for how to use a MERGE statement to perform the insert or update based upon a single criteria.

Resources