ORA-00918 returns from stored procedure but it works executing a query in SQL Page - oracle

I'm trying return a list from db but it gives me Error "ORA-00918: column ambiguously defined".
When I execute this query inside new SQL page, it returns true list. However, when I write it in a package as stored procedure, it returns ORA-00918 and package goes invalid status.
What is the reason for this difference?
select distinct c.customer_no, m.title, c.group_id, g.name, c.pricelist_id, p.name from db.customer c
join db.pricelist p on c.pricelist_id = p.pricelist_id
join db.master m on c.customer_no = m.customer_no
join db.group g on c.group_id = g.id
where (c.customer_no = pn_customer_no or pn_customer_no=-1)
and (c.group_id = pn_group_no or pn_group_no=-1)
and (c.pricelist_id = pn_pricelist_no or pn_pricelist_no=-1)
and (c.kom_type = ps_kom_tip)
order by c.customer_no asc

You are selecting the columns:
select distinct
c.customer_no,
m.title,
c.group_id,
g.name, -- NAME column
c.pricelist_id,
p.name -- NAME column
When you run the query in SQL/Plus or SQL Developer (or another IDE) it will output the columns:
CUSTOMER_NO TITLE GROUP_ID NAME PRICELIST_ID NAME1
and will rename the second NAME column to NAME1.
In the PL/SQL scope, it will not do this and will try to handle the two columns with the names you have given (i.e. the same names), fail and return ORA-00918.
You need to give one (or both) column an alias so they have distinct names.

New SQL page assigns your dublicate columns new temporary column names.
But stored procedures add your values a list matched column names.
Therefore, two columns have same names, it confuses which name should desired name.
Like bundle, your column name will be key to learn value and value will be value.
You should change one of them p.name or g.name or both of them.
select distinct c.customer_no, m.isim_unvan, c.group_id, g.name as groupName, c.pricelist_id, p.name as tarifeName from db.customer c
join db.pricelist p on c.pricelist_id = p.pricelist_id
join db.master m on c.customer_no = m.musteri_no
join db.group g on c.group_id = g.id
where (c.customer_no = pn_customer_no or pn_customer_no=-1)
and (c.group_id = pn_group_no or pn_group_no=-1)
and (c.pricelist_id = pn_pricelist_no or pn_pricelist_no=-1)
and (c.kom_type = ps_kom_tip)
order by c.customer_no asc

Related

ORA-00936: missing expression using SELECT INTO local_variable

I am trying to assing a result to a local variable in stored procedure sql.
For example
Select c.parm_val from Cusomter.name c where c.id = '102';
The above query gives me a result like 36,1508,4399,4403,4405,4407,4409,4411,4419
I want to assign it to a local variable
So I created in stored procedure like below
DECLARE
values VARCHAR2(500 BYTE);
BEGIN
Select into values c.parm_val from Cusomter.name c where c.id = '102';
END
When I execute this I get different errors each time
Something like PL/SQL: ORA-00936: missing expression
I want to assign those result a variable. I don't know if I can use INSERT as it not a table.
Can someone help me how to assign it to a variable.
I'm not sure about the syntax you are using. The FROM clause requires a table name like Customer, not Customer.name, which seems to be a column.
Starting with 11g Release 2 you can use the LISTAGG function to concatenate a column from the result rows into a single string.
SELECT LISTAGG(c.name, ',') WITHIN GROUP (ORDER BY c.name) INTO "values"
FROM Customer c
WHERE c.id = '102';
If c.id has a numeric type, drop the quotes: WHERE c.id = 102.
According to your comment, you probably want something like
SELECT c.name INTO "values"
FROM Customer c
WHERE c.id = '102';
See: PL/SQL SELECT INTO
Also, VALUES is a reserved word in SQL. Therefore, either choose another name, or escape it as "values" (in the declaration as well).
INTO comes after the field list:
Select c.parm_val into values from Cusomter.name c where c.id = '102';

Join to another table when the join column is null

I have a sql that is selecting many things from the database however I would like that data to only comeback which is matched to a personal table I have.
I would like to join a column [vin_code] from my table [population] however there are nulls in here and were there are nulls I would like to join another column from my table to another table in the database.
I will give an example sql below:
Select distinct v.kegal_rntity_id
From vin v
Inner join ops$dami.population pop
On v.vin_code = pop.vin_code
Then were pop.vin_code is null I would like to join pop.vis_code on a table in the database called zegal_rentity z column z.vis_code
So something like
join zegal_rentity z
On pop.vis_code = z.vis_code
But I only want to do this were pop.vin_code is null
As sample data is not available, I am unable to test the solution but try the following query with condition based outer join.
Select distinct v.kegal_rntity_id
From ops$dami.population pop
Left join vin v
On v.vin_code = pop.vin_code
Left join zegal_rentity z
On (case when pop.vin_code is null and
pop.vis_code = z.vis_code then 1 end = 1);
Cheers!!

Hibernate HQL GroupBy in Oracle

I created this query using HQL with Hibernate and Oracle
select c from Cat c
left join c.kittens k
where (c.location= 1 OR c.location = 2)
and (i.activo = 1)
group
by c.id,
c.name,
c.fulldescription,
c.kittens
order by count(e) desc
The problem comes with the fact that in HQL you need to specify all fields in Cat in order to perform a Group By, but fulldescription is a CLOB, and you cannot group by by a CLOB (I get a "Not a Group By Expression" error. I've seen a few solutions around for a pure SQL sentence but none for HQL.
A serious issue GROUP BY of HQL because if you specify your object in GROUP BY and in your SELECT field list behaviours are differents. In GROUP BY has considered only id field but in SELECT field list all fields are considered.
So you can use a subquery with GROUP BY to return only id from your object, so that result becomes an input for the main query, like the follow I write for you.
Pay attention there are some alias table (i and e) not defined, so this query doesn't work, but you know as fixed.
Try this:
select c2 from Cat c2
where c2.id in (
select c.id from Cat c
left join c.kittens k
where (c.location= 1 OR c.location = 2)
and (i.activo = 1) <-- who is i alias??
group by c.id)
order by count(e) desc <-- who is e alias???

Assignment in Hive query

I have below query in which i need to assign one table column value to another table column.
Query:
SELECT A.aval,B.bval,B.bval1 FROM A JOIN B ON (A.aval = B.bval)
How do I assign one table column value to another table column in Hive?
Have tried
SELECT A.aval,B.bval,B.bval1, A.aval = B.bval1 FROM A JOIN B ON (A.aval = B.bval)
In results:
A.aval = B.bval1, returning false since its not assigning to A.aval.
I guess you want to write in a table ?
So You have to create a table (for example C) which contains all the fields you need.
And then you do :
INSERT [OVERWRITE] INTO TABLE C
SELECT A.aval,B.bval,B.bval1, A.aval
FROM A
JOIN B ON (A.aval = B.bval)
The result of the select will be inserted in the table C
insert overwrite table c SELECT A.aval,B.bval,B.bval1 FROM A JOIN B ON (A.aval = B.bval)

LINQ Weirdness with joining tables

I have to join multiple tables. The tables have similar data for each table.
ID
(table id for FK)
Name
Description
Owner
from d in Departments
join f in Functions on d.DepartmentID equals f.DepartmentID
join pg in Processes on f.FunctionID equals pg.FunctionID
select new { d.DepartmentID, f.Name, pg.Name }
This throws an error 'An anonymous type cannot have multiple properties with the same name'
is there a better way to join these tables?
Should I do Select new { d, f, pg } ? Is it easy to grab the data from that?
I am very new to LINQ so any help is appreciated.
The compiler creates the properties of the anonymous type based on the property you use. In your case you use the property Name twice. Once in f.Name and once in pg.Name.
To fix it you have to specify at least one of the two property names explicitly:
select new { d.DepartmentID, FunctionName = f.Name, ProcessName = pg.Name }
You just need to give unique names for the two Name properties. Something like:
from d in Departments
join f in Functions on d.DepartmentID equals f.DepartmentID
join pg in Processes on f.FunctionID equals pg.FunctionID
select new { d.DepartmentID, FName = f.Name, PGName = pg.Name }
When you don't specify your own names, the compiler will just use the full name of the property, but since in this case they are both just Name, it will generate an error.

Resources