Script Variables in Oracle SQL Developer - oracle-sqldeveloper

Given two tables
USERS
UID NAME
1 KEN
ADRESS
AID UID CITY
1 1 LONDON
I'd like to have an Oracle SQL Developer script that outputs two result tables just like if I'd had entered two select statements one after another.
This does NOT work, I couldn't assign the u_id variable:
select UID into u_id from USERS where NAME='KEN';
select * from USERS where UID = u_id;
select * from ADRESS where UID = u_id;
The output should of course be
UID NAME
1 KEN
AID UID CITY
1 1 LONDON

There are at least two ways to do this in SQL Developer.
With a bind variable:
variable u_id number
execute select U_ID into :u_id from USERS where U_NAME='KEN';
select * from USERS where U_ID = :u_id;
select * from ADRESS where U_ID = :u_id;
Or with a substitution variable:
column U_ID new_value sub_u_id;
set verify off
select U_ID from USERS where U_NAME='KEN';
select * from USERS where U_ID = &sub_u_id;
select * from ADRESS where U_ID = &sub_u_id;
Which in this case you could simplify to:
column U_ID new_value sub_u_id;
set verify off
select * from USERS where U_NAME='KEN';
select * from ADRESS where U_ID = &sub_u_id;
Read more about the variable command, the execute command, the column command and it's new_value clause, and substitution variables in the SQL*Plus documentation - much of which applies to SQL Developer as well.
Demos with tables created with slightly different column names, to avoid key/reserved words:
create table USERS (U_ID number, U_NAME varchar2(10));
insert into users values (1, 'KEN');
create table ADRESS(A_ID number, U_ID number, CITY varchar2(10));
insert into adress values (1, 1, 'LONDON');
prompt Demo 1: bind variables
var u_id number
exec select U_ID into :u_id from USERS where U_NAME='KEN';
select * from USERS where U_ID = :u_id;
select * from ADRESS where U_ID = :u_id;
prompt Demo 2: substitution variables
column U_ID new_value sub_u_id;
set verify off
select * from USERS where U_NAME='KEN';
select * from ADRESS where U_ID = &sub_u_id;
Run as a script, the script output window shows:
Table USERS created.
1 row inserted.
Table ADRESS created.
1 row inserted.
Demo 1: bind variables
PL/SQL procedure successfully completed.
U_ID U_NAME
---------- ----------
1 KEN
A_ID U_ID CITY
---------- ---------- ----------
1 1 LONDON
Demo 2: substitution variables
U_ID U_NAME
---------- ----------
1 KEN
A_ID U_ID CITY
---------- ---------- ----------
1 1 LONDON
You can suppress the PL/SQL procedure successfully completed message with set feedback off, of course.

Related

PL/SQL error ORA-00904: invalid identifier using merge, select and set

I'm new to PL/SQL. I've created a table called orders which has a pk as orders_id and a products table with a pk as products_id. Both are numbers 1 through 10. In the orders table is a column called item_name, which I want to merge into the products column called item_name.
This is what I have tried:
MERGE INTO products
USING (
SELECT item_name
FROM orders)
ON (orders.order_id = products.product_id)
WHEN MATCHED THEN
UPDATE SET
products.item_name = orders.item_name
AND
MERGE INTO products
USING (
SELECT item_name
FROM orders)
ON (products.product_id=orders.order_id )
WHEN MATCHED THEN
UPDATE SET
products.item_name2 = orders.item_name
I keep getting the error "ORA-00904: "ORDERS"."ORDER_ID": invalid identifier". I've checked order_id is a NUMBER the same as the product_id. I have also created a column in products called item_name2, but that didn't work either. What does the error mean? Any help is appreciated, thank you.
Here's how.
Sample tables:
SQL> select * from orders;
ORDERS_ID ITEM_N
---------- ------
1 Name A
2 Name B
3 Name C
5 Name E
SQL> select * from products;
PRODUCTS_ID ITEM_NAME
----------- ----------
1
2
3
4
Updating products.item_name from the orders table:
SQL> merge into products p
2 using orders o
3 on (o.orders_id = p.products_id)
4 when matched then update set
5 p.item_name = o.item_name;
3 rows merged.
Result:
SQL> select * from products;
PRODUCTS_ID ITEM_NAME
----------- ----------
1 Name A
2 Name B
3 Name C
4 --> no matching IDs, so it is left empty
SQL>

SQL Query required where we should not used the group by and data count based on dep_id

Need your help in getting the SQL Query.
1. I have one table which is having following columns
Name Null? Type
------------ -------- ------------
EMP_ID NOT NULL NUMBER(2)
DEP_ID NUMBER(2)
SALARY NUMBER(14,3)
NAME1 VARCHAR2(50)
NAME2 VARCHAR2(50)
JOINING_DATE DATE
Now I want the result - COUNT(1) based on DEP_ID without using GROUP BY .
EXAMPLE :
select DEP_ID,COUNT(1) from unipartemp group by DEP_ID;
DEP_ID COUNT(1)
1 2
2 2
3 1
What is the Query where we should get the same result but we should not use group by ...
Please suggest .
I am assuming that the result u r looking for is the count of the distinct dept_id. Try using the distinct(dept_id) to get the result.

include in result set columns the 5 returned columns in a User Defined Table Function

I have a UDTF that will always return 1 row of 6 columns
The UDTF has one parameter
I want the 5 columns included in the result set of a query. The table I'm querying has a column that I want to use as the parameter for each row
I have not been able to figure out the correct syntax.
Any suggestions?
The UDTF
create function xxxx.UF_yyyyyy(USERID CHAR(10))
returns table (
p2User char(10),
STATUS CHAR(3),
USED DEC(7, 0),
CREATED DEC(7, 0),
SIGNON DEC(7, 0),
EXCLUDE DEC(7, 0))
language RPGLE
NOT DETERMINISTIC
NO SQL
DISALLOW PARALLEL
NOT FENCED
EXTERNAL NAME 'xxxx/UF_yyyyyy'
PARAMETER STYLE DB2SQL
example
select * from table(xxxx/UF_yyyyyy(CHAR('CMFIRST '))) a
result
P2USER STATUS USED CREATED SIGNON EXCLUDE
---------- ------ ------- ------- ------- -------
CMFIRST ACT 1170926 1150826 1170926 0
Here is an example of a select I tried
SELECT T1.AQABVN, T1.AQA8TX,
(SELECT COUNT(*) FROM fffff T4 WHERE T4.BDABVN = T1.AQABVN) AS SACCMS,
t2.p2User, t2.used
FROM
zzzzz T1
full join table(xxxx.UF_yyyyyy(T1.AQABVN)) t2 on T1.AQABVN = t2.p2User
Result
[SQL0205] Column P2USER not in table T2 in *N.
Got it working
SELECT T1.AQABVN, T1.AQA8TX,
(SELECT COUNT(*) FROM fffff T4 WHERE T4.BDABVN = T1.AQABVN) AS SACCMS,
t2.status, t2.used, t2.created, t2.signon, t2.exclude
FROM
zzzzz T1
join
table(SMLFQA.UF_XAJKUPR(T1.AQABVN)) t2 on T1.AQABVN = t2.p2User

How to get records from table based on two other tables JPA

I'm trying to do something simple in JPA.
I have a table Businesses:
BusinessId name
------------ ------
1 Joe
2 bob
And table Products:
productID name
------------ ------
1 Pen
2 paper
Because they related as meny-to-many I created another table businessesHasProductID:
BusinessId productID
------------ -----------
1 1
1 2
2 2
Now I want to select BusinessId and productID form businessesHasProductID where the name of BusinessId = 'x' and the name of productID = 'y'.
I built the tables and then I created the entity classes (from wizard in netBeans). I know how to get the "Businesses" table where Businesses.name = 'x' and I know how to get "Products" table where Products.name = 'y'. but I want to combine these results and get the IDs.
I tried to do :
Query query = getEntityManager().createQuery("
SELECT b FROM businessesHasProductID WHERE b.BusinessId IN
(SELECT t0.BusinessId FROM Businesses t0 WHERE t0.BusinessId = 'x')
AND b.productID IN
(SELECT t1.productID FROM Products t1 WHERE t1.productID = 'y')
");
That's not worked. It complains that the IN contains invalid data.
If I understand correctly, you want to get all the [bId, pId] tuples that exist in the join table and for which the name of the business identified by bId is 'x' and the name of the product identified by pId is 'y'.
If so, the following query should do what you want:
select business.businessId, product.productId
from Business business
inner join business.products product
where business.name = 'x'
and product.name = 'y'

How to fetch two column value in one colume based on boolean field

I am working with SQL Server 2005.
I am having 3 table
Product
PId | PName
Service
SId | SName
Bill
BId | TypeId | IsService
TypeId is PId or SId. based on IsService Field.
If IsService is 1 then TypeId is SId and If IsService is 0 then TypeId is PId
I want to fetch PName and SName with Bill so how can I?
I am thinking of to Write stored Procedure for this..
and Adding Dynamic Column to Stored Procudure that column contain Either SName or PName as per IsService.
But don't know how to write this also?
Maybe something like this:
SELECT
Bill.*,
CASE
WHEN Bill.IsService=1
THEN Service.SName
ELSE Product.PName
END AS Name
FROM
Bill
LEFT JOIN Service
ON Bill.TypeId=Service.SId
LEFT JOIN Product
ON Product.TypeId=Bill.BId
SELECT
t.BId,
tt.Name
FROM Bill t
Outer Apply
(
SELECT PName AS name FROM Product WHERE 0 = t.IsService AND PId = t.TypeId
UNION ALL
SELECT SName AS name FROM Servicess WHERE 1 = t.IsService AND SId = t.TypeId
) tt