Why do we have to do nesting of blocks in Oracle PL/SQL ? [closed] - plsql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am new to Oracle PL/SQL.Being a procedural language why do we have to do nesting multiple times ?
Thanks in advance.

Nesting of blocks help in exception handling.
for example:
begin -- BLOCK A
begin --BLOCK B
Statement1;
end; --End of block B
end; --End of block A
If there is an error in the execution of statement1, an exception is raised this exception will navigate to the outer block (A) as it is unhandled in block B. Consider another example below
begin -- BLOCK A
begin --BLOCK B
Statement1;
exception
when others then
Statement; --This statement is executed if there is an exception
end; --End of block B
end; --End of block A
In the above snippet, the exception will be handed within block B. It will not navigate to block A.

Related

When table is select query do it change [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 days ago.
Improve this question
I want to change charset to select calderma table
when table is select query in oracle I would like to make the following changes
ALTER SESSION SET NLS_NUMERIC_CHARACTERS= ',.';
create or replace TRIGGER alter_session AFTER LOGON ON DATABASE
Begin
if ( osuser='solentra') then
execute immediate
'alter session set nls_date_fomat = ''dd-mon-yyyy hh24:mi:ss'' ';
End if;
End;
OR
create or replace TRIGGER alter_session AFTER LOGON ON DATABASE
Begin
if ( SELECT from calderma ) then
execute immediate
'alter session set nls_date_fomat = ''dd-mon-yyyy hh24:mi:ss'' ';
End if;
End;
I don't have much to do with pl\sql but I faced a problem like this, I will be glad if you help

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.

Update in multiview using Linq [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to update details using linq to entities. But instead of takin a new aspx page i want to update details in another view. will it work.? and please give the linq to entity update query
Let us assume:
db is the context of your Database Entity.
Table_name is the name of Table you need to update.
row_id is the value you are using to search for the data in the Table.
To update using linq you need to fetch the record first using the below query:
var data = (from r in db.Table_name
where r.id == row_id
select r).FirstOrDefault();
Now to update the values just update them. For example:
data.Name = "Firstname lastname"
data.IsActive = true;
.
.
and so on
After you have updated the values in data you need to Save the changes made by you by this command:
db.SaveChanges();
That's it.

Looping through multiple results in PLSQL [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 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;

Unable to handle the exception properly [duplicate]

This question already has answers here:
How to handle exception and execute remaining statements?
(2 answers)
Closed 8 years ago.
In the below code first insert statement is,
insert into customer_master select max(customerid)+1,customer_name from customer_master
where customer_name not in (select customername from customer_master);
Am getting null value insert error for this. Yes, that is right.
But it is stopping below statements of the block from executing.
insert into customer_account_mapping select customerid,upper(pcd(i)),upper(acd(i)),cost from customer_master where customername=customer_name and concat(upper(pcd(i)),upper(acd(i))) not in (select concat(upper(pcode),upper(acode)) from customer_account_mapping);
insert into user_permissions select distinct user_id,sales_person_name,sales_mgr_name,upper(pcd(i)),upper(acd(i)) from user_permissions where sales_person_name=sales_person and concat(upper(pcd(i)),upper(acd(i))) not in (select concat(upper(pcode),upper(acode)) from user_permissions) and rownum<2 ;
Complete code is given below.
create or replace
procedure dashboard_addtion
(customer_name varchar2,pcd parray,acd aarray,sales_person varchar2,cost number)
IS
begin
insert into customer_master select max(customerid)+1,customer_name from customer_master
where customer_name not in (select customername from customer_master);
for i in 1..acd.count loop
insert into customer_account_mapping select customerid,upper(pcd(i)),upper(acd(i)),cost from customer_master where customername=customer_name and concat(upper(pcd(i)),upper(acd(i))) not in (select concat(upper(pcode),upper(acode)) from customer_account_mapping);
insert into user_permissions select distinct user_id,sales_person_name,sales_mgr_name,upper(pcd(i)),upper(acd(i)) from user_permissions where sales_person_name=sales_person and concat(upper(pcd(i)),upper(acd(i))) not in (select concat(upper(pcode),upper(acode)) from user_permissions) and rownum<2 ;
commit;
end loop;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE (SQLERRM);
end;
Am getting null value insert error for this. Yes, that is right.
But it is stopping below statements of the block from executing.
The exception caused process flow to go to the EXCEPTION handler section. That is the end of the program block. So processing stops after handling the exception. This is standard. So in fact, your have titled your question wrongly, because this is the proper way to handle exceptions. The whole point is to preserve the ACIDity of the transaction. Why would you want to continue processing if the first part of your program fails?
Although, having described this as the proper way I feel I must say that "handling" exceptions by simply using DBMS_OUTPUT is extremely bad practice. That won't work in production code because calling progarm won't know an exception occurred. Your procedure needs to log the error and then re-raise the exception. Anything else is just asking for trouble.

Resources