Oracle Apex Select List LOV conditional query - plsql

I have an Editable Interactive Grid (for product_sale table) with a Select List (to select a product) in Oracle Apex 20.2 app.
Currently I'm using below query to populate this list of values.
SELECT productName,productId FROM product WHERE productAvailability = 'Y'
There are a few products that I need to set the productAvailability as 'N'. When I made this change, Interactive Grid of product_sale shows productId instead of the productName.
What I need to achieve is, only show products with productAvailability = 'Y' for new records (when going to add new record in to the table by clicking Add Row button) and for the old records show the productName of the selected product regardless the productAvailability.
Table Structure
Sample Data
Interactive Grid View
How could I achieve this?

You can achieve this using the "cascading list of values" option in the column settings. Let me illustrate with an example on the EMP sample table - you should be able to translate this to your own code:
Situation: BLAKE can not be selected as manager for any other employees than the one he is already manager for.
This would be the select:
SELECT ename, empno FROM emp WHERE ename != 'BLAKE'
As expected, for records that have BLAKE as manager, the MGR column will display the id because the value is not in the result set of the select list query. This is the behaviour you are seeing.
The solution has 2 steps:
Union the query with a query that has the value of the current row. I have rewritten the query using a CTE.
WITH all_mgr(dv, rv) AS
(SELECT ename, empno FROM emp WHERE ename != 'BLAKE'
UNION
SELECT m.ename, m.empno
FROM emp e
JOIN emp m ON e.mgr = m.empno
WHERE e.ename = :ENAME
)
SELECT dv, rv FROM all_mgr
Make sure column ename from the current row is bind to :ENAME. In "Cascading List Of Values" set both "Parent Column" and "Items to Submit" to ENAME. Now the select list will take the current row value for :ENAME and the list of values will include 'BLAKE' for users that have 'BLAKE' as a manager.

Related

Apex Collection: how to have actual column names instead of C001, C002

created a page to allow users to input SQL statement, when user press button, it should Execute this query and another region with Classic Report will display results.
on Button a Dynamic Action execute PL/SQL code below:
on Click event:
begin
IF :P8_YOURSQL IS NOT NULL THEN
APEX_COLLECTION.CREATE_COLLECTION_FROM_QUERY (
p_collection_name => 'SOMECOLLECTION',
p_query => :P8_YOURSQL,
p_truncate_if_exists => 'YES'
);
end if;
end;
Item to Submit: P8_YOURSQL
second Action refreshes CR region and CR region is based on:
SELECT * FROM APEX_COLLECTIONS
WHERE COLLECTION_NAME = 'SOMECOLLECTION';
resulted columns are not limited to the query results and column headers are C001, C002, C003 etc.
(a) how i can limit the columns to the SQL statement contained?
(b) how to change header to the actual column names?
(c) how to check for a valid SQL statement?
please help with sample code How To.
APEX_COLLECTIONS is a generic table so it has a couple of columns for the most common data types with names like C001, N001 etc. As with any table columns can be aliased in a query.
Example:
Create page process before header to populate the collection
APEX_COLLECTION.CREATE_COLLECTION_FROM_QUERY (
p_collection_name => 'EMP_COLLECTION',
p_query => q'!SELECT * FROM emp!',
p_truncate_if_exists => 'YES'
);
Run the page and open the "Session" window from the developer toolbar. Select "Collections" in the "View" dropdown and click "Set". This will list the data in the APEX_COLLECTIONS table for any collections that exist for the session. Take note of the data and the columns the data is in.
Leave the "Sessions" window open and create a sql query on the APEX_COLLECTIONS view with appropriate aliases.
SELECT
c001 as EMPNO,
c002 as ENAME,
c003 as JOB,
c004 as MGR,
<rest_of_columns>...
FROM
APEX_COLLECTIONS WHERE collection_name = 'EMP_COLLECTION'
It's not possible to alias columns when doing a SELECT * FROM ...
If the SELECT * is important then there is another possibility. Create a view on top of APEX_COLLECTIONS with relevant column names. Example:
CREATE OR REPLACE view EMP_COLLECTION_V
AS
SELECT
c001 as EMPNO,
c002 as ENAME,
c003 as JOB,
c004 as MGR
FROM
APEX_COLLECTIONS WHERE collection_name = 'EMP_COLLECTION'
and then use SELECT * FROM EMP_COLLECTION_V as SQL source for the classic report.
To get the columns names for a SELECT * FROM ... have a look at [DBMS_SQL.DESCRIBE_COLUMNS3][https://docs.oracle.com/en/database/oracle/oracle-database/18/arpls/DBMS_SQL.html#GUID-00AB5DE3-C428-4E60-9398-FD4892F32402]. There is an example in the doc that shows how to print the column names.
This can then be implemented in a classic report
Create a page item per column: P1_C1, P1_C2, etc
Set the column header for each column to the corresponding page item: col1 has header &P1_C1, col2 has header &P1_C2, etc
Create a page process to set the column headers based on the sql query using DBMS_SQL.DESCRIBE_COLUMNS3 - OR - use a
Note that it is strongly advised to assert the sql query to avoid unwanted sql (like a delete or drop table command).

Sequence in sql operation?

I am passing datatable as input parameter to stored procedure. Datatable contains id, Name,Lname,Mobileno,EmpId.
Employee table contains [Name],[Lname],[mobno],[Did] as columns.
When user is logged in, his Id come as DId. There are more than 1000 records. Instead of passing that id to datatable, I have created
separete parameter to sp. I want to add records to Employee table, which are not already exist. If combination of mobileno and Did already exists, then
don't insert into Employee table, else insert. Datatable may contain records, which can be duplicate. So I don't want to include that record. I want select only
distinct records and add them to table. I am intrested in mobile no. If there are 10 record having same moble no, I am fetching record, which comes first.
Following code is right or wrong. According to my knowledge, first from clause, then inner join, then where, then select execute. Record get fetched from datatable,
then inner join happens generate result, from that result not from datatable it will check record. So it will give me proper output.
Create Procedure Proc_InsertEmpDetails
#tblEmp EmpType READONLY,
#DId int
as
begin
INSERT INTO Employee
([Name],[Lname],[mobno],[Did])
SELECT [Name],[Lname],[mobno] #DId
FROM #tblEmp A
Inner join (
select min(Id) as minID, mobno from #tblEmp group by mobno
) MinIDTbl
on MinIDTbl.minID = A.ExcelId
WHERE NOT EXISTS (SELECT 1
FROM Employee B
WHERE B.[mobno] = A.[mobno]
AND B.[Did] = #DId )
end
or does I need to change like this
INSERT INTO Employee
([Name],[Lname],[mobno],[Did])
SELECT C.[Name],C.[Lname],C.[mobno], C.D_Id
from
(SELECT [Name],[Lname],[mobno] #DId as D_Id
FROM #tblEmp A
Inner join (
select min(Id) as minID, mobno from #tblEmp group by mobno
) MinIDTbl
on MinIDTbl.minID = A.ExcelId
)C
WHERE NOT EXISTS (SELECT 1
FROM Employee B
WHERE B.[mobno] = C.[mobno]
AND B.[Did] = #DId )

How to Show Two table columns in a aspxgridview?

I have tow tables in mssql server, Car & Weight.
Car has IDCar, Name, Username, Description columns.
And Weight has IDWeight, CarID, Name, Date, Weight columns.
I want to show Name of Car table in the first column of aspx gridview and show weight and date from Weight Table.
The Name is static, but everyday user must input date and weight for each car.
I wish it was clear about it!
Write a query with the join with both tables and get the desired result and then bind that result to the gridview. Your query can be like,
Select c.Name,w.Weight,w.Date
FROM
Car c LEFT Join Weight w
ON c.CarID = w.CarID
UPDATE
Applied left join after the comment.Left Join will give you all the car names from the Car table irrespective of if there are data present in the Weight table for that car or not.
To use the Where in join just use it as normal where clause.
WHERE
w.Date = GETDATE() //Change according your need
AND w.Weight = 110 //Change according your need
To read more about join
To bind datatable to the Gridview read here
select car.Name,weigth.weight,weigth.date from car
inner join weight on car.carid=weigth.carid
you can add a filter to specifiy which item you want using where clause:
where carid in ('xx','123','guid') and weight.weight between 100 and 200

Can multitable Insert statement combine columns from view and columns from another view?

I'm planning to do SQL expert examination.
I have doubts that answer D is correct:
Evaluate the following command:
CREATE TABLE employees
( employee_id NUMBER(2) PRIMARY KEY
, last_name VARCHAR2(25) NOT NULL
, department_id NUMBER(2)NOT NULL
, job_id VARCHAR2(8)
, salary NUMBER(10,2));
You issue
the following command to create a view that displays the IDs and last
names of the sales staff in the organization:
CREATE OR REPLACE VIEW sales_staff_vu AS
SELECT employee_id, last_name,job_id
FROM employees
WHERE job_id LIKE 'SA_%'
WITH CHECK OPTION;
Which two statements are true regarding the above view? (Choose two.)
A. It allows you to insert rows into the EMPLOYEES table .
B. It allows you to delete details of the existing sales staff from
the EMPLOYEES table.
C. It allows you to update job IDs of the existing sales staff to any
other job ID in the EMPLOYEES table.
D. It allows you to insert IDs, last names, and job IDs of the sales
staff from the view if it is used in multitable INSERT statements.
Source
A is FALSE as the view doesn't allow inserting into the department_id column which is mandatory.
B is TRUE, although it would be more accurate to say that the view only allows deletions of employees where the job_id matches the predicate LIKE 'SA_%'.
C is FALSE, as the WITH CHECK OPTION means that you can't change the job_id if the new job_id doesn't match the view's predicate.
D is FALSE: a multitable insert statement can't just insert some columns into the view and the remaining columns into the employees table. Even if you join the view to the table, the insert must still insert into the base table, not into the view:
insert into
(select e.employee_id, e.last_name, e.department_id, e.job_id
from sales_staff_vu v
join employees e
on v.employee_id = e.employee_id)
values
(1, 'KEMP', 2, 'SA_X');
I suspect this is a test of your ability to verify and ignore wrong information on the internet - i.e. 99% of sites say D is true!
Now, my answer can be easily disproved by crafting a multitable insert statement that successfully inserts via the view.
According to my knowledge answer D is wrong.Reasons are
1.If A is wrong, definitely D is wrong.because department_id column is mandatory field on the table, but it is not mentioned in view.so we can't insert a row using this view.
2.In Answer D,multitable INSERT statements are INSERT ALL, INSERT FIRST,etc.
to check this Answer,i have tried these steps
CREATE TABLE employees123
( employee_id NUMBER(2) PRIMARY KEY
, last_name VARCHAR2(25) NOT NULL
, department_id NUMBER(2)NOT NULL
, job_id VARCHAR2(8)
, salary NUMBER(10,2));
CREATE OR REPLACE VIEW sales_staff_vu123 AS
SELECT employee_id, last_name,job_id,department_id
FROM employees123
WHERE job_id LIKE 'SA_%'
WITH CHECK OPTION;
--department_id is added to view
--here i am trying to insert my employees table rows to employees123 table
INSERT ALL
INTO sales_staff_vu123 --using View
SELECT employee_id, last_name,job_id,department_id
FROM employees;
Error at Command Line:153 Column:15
Error report:
SQL Error: ORA-01702: a view is not appropriate here
01702. 00000 - "a view is not appropriate here"
*Cause:
*Action:
So my decision is we cant use views with multitable insert statements.

repeat same row in the SQL selection statement under some conditions

I have a database table has four columns called "Courses(CourseID,CourseName,CreditHours,LabSession)", and I wanna write SQL Statement that retrieve all data in the Course table, furthermore, the SQL must repeat the row (record) if the LabSession column in the row has a value "Y" otherwise retrieve the row as it is without repeating.
BTW, I am using Microsoft SQL Server Database
select CourseID,CourseName,CreditHours,LabSession from Courses
union ALL
select CourseID,CourseName,CreditHours,LabSession from Courses where LabSession='Y'
order by CourseID
SELECT CourseID, CourseName, CreditHours, LabSession
FROM Courses
UNION ALL
SELECT CourseID, CourseName, CreditHours, LabSession
FROM Courses
WHERE LabSession = 'Y'
ORDER BY CourseId

Resources