How to execute sql file in spring boot? - spring

I have this query in .sql I am very much new to the spring-boot framework. Is it possible to hit this query from JPA?
What other option do we have to execute SQL statement can I run this query as a native query?
I am just aware of spring-data-jpa but here I didn't find anything how to execute the saved SQL file
INSERT INTO user_lookup (
user_no, user_id, user_name, email_id, address
)
SELECT
user_no, user_id, user_name, email_id, address
FROM(
SELECT
CAST(c.bank AS NUMBER) AS user_no, c.user_id, c.user_name, c.email_id, c.address
FROM user_news c
LEFT JOIN user_sector d
ON d.name = c.user_name
LEFT JOIN user_info e
ON e.name = c.email_id AND
e.user_id = d.id
LEFT JOIN user_lookup f
ON f.user_no = c.user_no
WHERE f.user_no IS NULL
)

You can use liquibase with spring boot.
Something like:
application.yaml:
spring:
datasource:
url: jdbc:postgresql://localhost:5432/db1
username: test
password: test
liquibase:
enabled: true
change-log: classpath:changelog.xml
changelog.xml:
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
<include file="script1.sql" relativeToChangelogFile="true"/>
</databaseChangeLog>
script1.sql:
INSERT INTO user_lookup (
user_no, user_id, user_name, email_id, address
)
SELECT
user_no, user_id, user_name, email_id, address
FROM(
SELECT
CAST(c.bank AS NUMBER) AS user_no, c.user_id, c.user_name, c.email_id, c.address
FROM user_news c
LEFT JOIN user_sector d
ON d.name = c.user_name
LEFT JOIN user_info e
ON e.name = c.email_id AND
e.user_id = d.id
LEFT JOIN user_lookup f
ON f.user_no = c.user_no
WHERE f.user_no IS NULL
)
Note: script1.sql will be executed once on app startup.

Related

Oracle - How to find where a table is used in a foreign key constraint? [duplicate]

In Oracle SQL Developer, if I'm viewing the information on a table, I can view the constraints, which let me see the foreign keys (and thus which tables are referenced by this table), and I can view the dependencies to see what packages and such reference the table. But I'm not sure how to find which tables reference the table.
For example, say I'm looking at the emp table. There is another table emp_dept which captures which employees work in which departments, which references the emp table through emp_id, the primary key of the emp table. Is there a way (through some UI element in the program, not through SQL) to find that the emp_dept table references the emp table, without me having to know that the emp_dept table exists?
No. There is no such option available from Oracle SQL Developer.
You have to execute a query by hand or use other tool (For instance PLSQL Developer has such option). The following SQL is that one used by PLSQL Developer:
select table_name, constraint_name, status, owner
from all_constraints
where r_owner = :r_owner
and constraint_type = 'R'
and r_constraint_name in
(
select constraint_name from all_constraints
where constraint_type in ('P', 'U')
and table_name = :r_table_name
and owner = :r_owner
)
order by table_name, constraint_name
Where r_owner is the schema, and r_table_name is the table for which you are looking for references. The names are case sensitive
Be careful because on the reports tab of Oracle SQL Developer there is the option "All tables / Dependencies" this is from ALL_DEPENDENCIES which refers to "dependencies between procedures, packages, functions, package bodies, and triggers accessible to the current user, including dependencies on views created without any database links.". Then, this report have no value for your question.
To add this to SQL Developer as an extension do the following:
Save the below code into an xml file (e.g. fk_ref.xml):
<items>
<item type="editor" node="TableNode" vertical="true">
<title><![CDATA[FK References]]></title>
<query>
<sql>
<![CDATA[select a.owner,
a.table_name,
a.constraint_name,
a.status
from all_constraints a
where a.constraint_type = 'R'
and exists(
select 1
from all_constraints
where constraint_name=a.r_constraint_name
and constraint_type in ('P', 'U')
and table_name = :OBJECT_NAME
and owner = :OBJECT_OWNER)
order by table_name, constraint_name]]>
</sql>
</query>
</item>
</items>
Add the extension to SQL Developer:
Tools > Preferences
Database > User Defined Extensions
Click "Add Row" button
In Type choose "EDITOR", Location is where you saved the xml file above
Click "Ok" then restart SQL Developer
Navigate to any table and you should now see an additional tab next to SQL one, labelled FK References, which displays the new FK information.
Reference
http://www.oracle.com/technetwork/issue-archive/2007/07-jul/o47sql-086233.html
Replace [Your TABLE] with emp in the query below
select owner,constraint_name,constraint_type,table_name,r_owner,r_constraint_name
from all_constraints
where constraint_type='R'
and r_constraint_name in (select constraint_name
from all_constraints
where constraint_type in ('P','U')
and table_name='[YOUR TABLE]');
You may be able to query this from the ALL_CONSTRAINTS view:
SELECT table_name
FROM ALL_CONSTRAINTS
WHERE constraint_type = 'R' -- "Referential integrity"
AND r_constraint_name IN
( SELECT constraint_name
FROM ALL_CONSTRAINTS
WHERE table_name = 'EMP'
AND constraint_type IN ('U', 'P') -- "Unique" or "Primary key"
);
SQL Developer 4.1, released in May of 2015, added a Model tab which shows table foreign keys which refer to your table in an Entity Relationship Diagram format.
SELECT DISTINCT table_name,
constraint_name,
column_name,
r_table_name,
position,
constraint_type
FROM (SELECT uc.table_name,
uc.constraint_name,
cols.column_name,
(SELECT table_name
FROM user_constraints
WHERE constraint_name = uc.r_constraint_name) r_table_name,
(SELECT column_name
FROM user_cons_columns
WHERE constraint_name = uc.r_constraint_name
AND position = cols.position) r_column_name,
cols.position,
uc.constraint_type
FROM user_constraints uc
inner join user_cons_columns cols
ON uc.constraint_name = cols.constraint_name
WHERE constraint_type != 'C')
START WITH table_name = '&&tableName'
AND column_name = '&&columnName'
CONNECT BY NOCYCLE PRIOR table_name = r_table_name
AND PRIOR column_name = r_column_name;
This has been in the product for years - although it wasn't in the product in 2011.
But, simply click on the Model page.
Make sure you are on at least version 4.0 (released in 2013) to access this feature.
How about something like this:
SELECT c.constraint_name, c.constraint_type, c2.constraint_name, c2.constraint_type, c2.table_name
FROM dba_constraints c JOIN dba_constraints c2 ON (c.r_constraint_name = c2.constraint_name)
WHERE c.table_name = <TABLE_OF_INTEREST>
AND c.constraint_TYPE = 'R';
To add to the above answer for sql developer plugin, using the below xml will help in getting the column associated with the foreign key.
<items>
<item type="editor" node="TableNode" vertical="true">
<title><![CDATA[FK References]]></title>
<query>
<sql>
<![CDATA[select a.owner,
a.constraint_name,
a.table_name,
b.column_name,
a.status
from all_constraints a
join all_cons_columns b ON b.constraint_name = a.constraint_name
where a.constraint_type = 'R'
and exists(
select 1
from all_constraints
where constraint_name=a.r_constraint_name
and constraint_type in ('P', 'U')
and table_name = :OBJECT_NAME
and owner = :OBJECT_OWNER)
order by table_name, constraint_name]]>
</sql>
</query>
</item>
</items>
I like to do this with a straight SQL query, rather than messing about with the SQL Developer application.
Here's how I just did it. Best to read through this and understand what's going on, so you can tweak it to fit your needs...
WITH all_primary_keys AS (
SELECT constraint_name AS pk_name,
table_name
FROM all_constraints
WHERE owner = USER
AND constraint_type = 'P'
)
SELECT ac.table_name || ' table has a foreign key called ' || upper(ac.constraint_name)
|| ' which references the primary key ' || upper(ac.r_constraint_name) || ' on table ' || apk.table_name AS foreign_keys
FROM all_constraints ac
LEFT JOIN all_primary_keys apk
ON ac.r_constraint_name = apk.pk_name
WHERE ac.owner = USER
AND ac.constraint_type = 'R'
AND ac.table_name = nvl(upper(:table_name), ac.table_name)
ORDER BY ac.table_name, ac.constraint_name
;
Only Replace table_name with your primary table name
select *
from all_constraints
where r_constraint_name in (
select constraint_name
from all_constraints
where table_name='table_name'
);
Replace MY_OWNER_NAME and MY_TABLE_NAME below and you are ready to go RECURSIVELY:
DECLARE
FUNCTION list_all_child_tables_and_constraints(asked_table_name in VARCHAR2, parent_table_name in VARCHAR2)
RETURN VARCHAR2 IS
current_path VARCHAR2(100);
BEGIN
FOR item IN
(SELECT fk.TABLE_NAME, constraint_parent.FK FK1, constraint_child.FK FK2
FROM all_constraints fk, all_constraints pk,
(SELECT acc.CONSTRAINT_NAME, LISTAGG(acc.COLUMN_NAME, ', ') WITHIN GROUP (ORDER BY acc.COLUMN_NAME) AS FK
FROM ALL_CONS_COLUMNS acc
WHERE acc.OWNER = 'MY_OWNER_NAME'
GROUP BY acc.CONSTRAINT_NAME) constraint_parent,
(SELECT acc.CONSTRAINT_NAME, LISTAGG(acc.COLUMN_NAME, ', ') WITHIN GROUP (ORDER BY acc.COLUMN_NAME) AS FK
FROM ALL_CONS_COLUMNS acc
WHERE acc.OWNER = 'MY_OWNER_NAME'
GROUP BY acc.CONSTRAINT_NAME) constraint_child
WHERE pk.owner = fk.r_owner
AND pk.constraint_name = fk.r_constraint_name
AND fk.constraint_type = 'R'
AND pk.table_name = asked_table_name
AND constraint_parent.CONSTRAINT_NAME = fk.CONSTRAINT_NAME
AND constraint_child.CONSTRAINT_NAME = fk.R_CONSTRAINT_NAME
AND pk.owner = 'MY_OWNER_NAME'
AND fk.owner = 'MY_OWNER_NAME')
LOOP
current_path := parent_table_name || ' // ' || item.TABLE_NAME;
DBMS_OUTPUT.PUT_LINE(current_path);
DBMS_OUTPUT.PUT_LINE(' [' || item.FK1 || '] [' || item.FK2 || ']');
DBMS_OUTPUT.PUT_LINE('');
current_path := list_all_child_tables_and_constraints(item.TABLE_NAME, current_path);
END LOOP;
RETURN '-----------FINISHED-----------';
EXCEPTION
WHEN OTHERS THEN
RETURN '-----------FINISHED-----------';
END list_all_child_tables_and_constraints;
BEGIN
DBMS_OUTPUT.PUT_LINE(list_all_child_tables_and_constraints('MY_TABLE_NAME', ''));
END;

ORACLE SQL DEVELOPER-select count(*) from multiple databases

How can I select count(*) from two different databases(call them ZEOTA and SP) having as result:
Zeota SP
88 3
I have tried this:
SELECT COUNT(CONSTRAINT_TYPE) NumberOfPrimaryKeys_Zeota
FROM ALL_CONSTRAINTS
WHERE CONSTRAINT_TYPE = 'P'
AND
OWNER = 'ZEOTA';
SELECT COUNT(*) AS NumberOfAttributes_SP
FROM ALL_COL_COMMENTS
WHERE OWNER = 'SP';
But the output shows two separate query results:
Query result 1:
Zeota
88
Query Result 2:
SP
3
However I am trying to do it this way, but having issues:
SELECT
COUNT(DISTINCT TABLE_NAME) AS ZNumOfTables,
COUNT(DISTINCT TABLE_NAME) AS SNumOfTables,
COUNT(DISTINCT TABLE_NAME) AS PNumOfTables
FROM ALL_TAB_COLUMNS
WHERE OWNER = 'SP';
WHERE OWNER = 'ZEOTA';
One option is to use your current queries as CTEs and then just cross join them:
with
t1 as
(select count(constraint_type) num_pk_zeota
from all_constraints
where owner = 'ZEOTA'
and constraint_type = 'PK'
),
t2 as
(select count(*) num_attr_sp
from all_col_comments
where owner = 'SP'
)
select a.num_pk_zeota,
b.num_attr_sp
from t1 a cross join t2 b;
P.S. What you call "databases" are users (schemas) in Oracle.

Hibernate generate sql query missing "(" character

I faced an issue when I use #Query
#Query(value = "select c.id as id, c.store_id as store_id, c.name as name, c.type as type, c.rate as rate, " +
"(select count(pc.partner_id) from partner_commission pc where pc.commission_id = c.id) as number_of_partner " +
"from commission c join partner_commission pc on c.id = pc.commission_id where c.store_id=:storeId", nativeQuery = true)
Page<Commission> findCommissionForManagement(#Param("storeId") Long storeId, Pageable pageable);
I already tested this script on database (postgresql), it worked! But, the sql script generated by hibernate as below:
Hibernate: select c.id as id, c.store_id as store_id, c.name as name, c.type as type, c.rate as rate, (select count(pc.partner_id) from partner_commission pc where pc.commission_id = c.id) as number_of_partner from commission c join partner_commission pc on c.id = pc.commission_id where c.store_id=? limit ?
Hibernate: select count(pc) from partner_commission pc where pc.commission_id = c.id) as number_of_partner from commission c join partner_commission pc on c.id = pc.commission_id where c.store_id=?
2021-08-16 04:05:44.645 WARN 15976 --- [llCommissions-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 42601
2021-08-16 04:05:44.645 ERROR 15976 --- [llCommissions-1] o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: syntax error at or near ")"
Position: 74
I hope to get some help
It seems like #Query has broken up the subquery incorrectly. Can you restructure the query using Joins?
Here is my suggestions, not tested but I hope you can debug it further to get your desired result.
SELECT
c.id AS id,
c.store_id AS store_id,
c.name AS name,
c.type AS type,
c.rate AS rate
pc.count AS number_of_partner
FROM
commission c
LEFT JOIN (
SELECT
id,
COUNT(*) AS count
FROM
partner_commission
GROUP BY
partner_commission.id
) AS pc
ON pc.id = c.store_id
WHERE
c.store_id = :storeId
Seems like a Spring Data JPA bug. You can try to workaround this by providing a custom count query in the #Query annotation.

How can I solve the ORA-01799 when creating a VIEW with a LEFT OUTER JOIN and a MAX value?

I am trying to create a view that joins a few tables, one of the joins is using a max value and I think that is generating the following error in our Oracle DB:
ORA-01799: a column may not be outer-joined to a subquery.
Can anyone help me to change the estatement to create the view? The query works fine, the problem only comes when I try to create a view with it.
CREATE or replace VIEW xxxxxx_V (ID, SERVICE_ID, PARENT_ID, PARENT_MATERIALITY, PARENT_END_STATUS,
PARENT_OUTS_TYPE, END_STATUS, MATERIALITY, OUTS_TYPE, CREATED_BY,
SERVICE_DESCRIPTION_SHORT, VERSION, OUTSOURCER_ID, OUTSOURCER_NAME, INSOURCER_ID, INSOURCER_NAME, PLANNED_SERVICE_START) AS
select c.id id, s.id service_id, s.outsourcing_id parent_id, p.materiality parent_materiality, p.end_status parent_end_status,
p.outsourcing_type parent_outs_type, c.end_status end_status, c.materiality materiality, c.outsourcing_type outs_type, c.created_by created_by,
r.service_description_short, r.version, r.outsourcer outsourcer_id, o.name outsourcer_name , r.insourcer insourcer_id,i.name insourcer_name, r.planned_service_start
from OS_OUTSOURCING_CONTRACT c
left join OS_SERVICES s on s.id = c.service_id
left join OS_OUTSOURCING_CONTRACT p on p.id = s.outsourcing_id
left join OS_RISK_ASSESSMENT r on r.outsourcing_id = c.id and r.version = (select max(version) from OS_RISK_ASSESSMENT where outsourcing_id = c.id)
left join OS_COMPANY_INSOURCER i on i.id = r.insourcer
left join OS_COMPANY_OUTSOURCER o on o.id = r.outsourcer
Join the subquery into the query:
CREATE or replace VIEW xxxxxx_V
(ID, SERVICE_ID, PARENT_ID, PARENT_MATERIALITY, PARENT_END_STATUS,
PARENT_OUTS_TYPE, END_STATUS, MATERIALITY, OUTS_TYPE, CREATED_BY,
SERVICE_DESCRIPTION_SHORT, VERSION, OUTSOURCER_ID, OUTSOURCER_NAME,
INSOURCER_ID, INSOURCER_NAME, PLANNED_SERVICE_START) AS
select c.id,
s.id service_id,
s.outsourcing_id parent_id,
p.materiality parent_materiality,
p.end_status parent_end_status,
p.outsourcing_type parent_outs_type,
c.end_status,
c.materiality,
c.outsourcing_type outs_type,
c.created_by,
r.service_description_short,
r.version,
r.outsourcer outsourcer_id,
o.name outsourcer_name,
r.insourcer insourcer_id,
i.name insourcer_name,
r.planned_service_start
from OS_OUTSOURCING_CONTRACT c
LEFT OUTER JOIN (select outsourcing_id,
max(version) AS MAX_VERSION
from OS_RISK_ASSESSMENT
group by outsourcing_id) ora
ON ora.OUTSOURCING_ID = c.id
left join OS_SERVICES s
on s.id = c.service_id
left join OS_OUTSOURCING_CONTRACT p
on p.id = s.outsourcing_id
left join OS_RISK_ASSESSMENT r
on r.outsourcing_id = ora.OUTSOURCING_ID and
r.version = ora.MAX_VERSION
left join OS_COMPANY_INSOURCER i
on i.id = r.insourcer
left join OS_COMPANY_OUTSOURCER o
on o.id = r.outsourcer

List of tables along with key information - Oracle

I am trying to fetch the list of tables, column information including the data type from the Oracle metadata tables. You can see the list of columns below. Somehow primary key and foreign key constraint data that I am pulling seems to be replicating for all the columns of that table in question, whereas I expect null for the columns which are not tagged as primary key or foreign key in the constraint tables.
I have used a left outer join, but seems like it is not working as expected due to the joining conditions. Any help would be appreciated.
SELECT AO.OWNER,
AO.OBJECT_NAME,
ATC.COLUMN_NAME,
ATC.DATA_TYPE,
ATC.NULLABLE,
AC.CONSTRAINT_NAME
FROM ALL_OBJECTS AO
JOIN
ALL_TAB_COLUMNS ATC
ON AO.OBJECT_NAME=ATC.TABLE_NAME
LEFT JOIN
ALL_CONS_COLUMNS ACC
ON
--ACC.column_name=ATC.column_name and
ACC.TABLE_NAME=ATC.TABLE_NAME
inner JOIN
ALL_CONSTRAINTS AC
ON
AC.TABLE_NAME=ACC.TABLE_NAME
AND
AC.CONSTRAINT_NAME=ACC.CONSTRAINT_NAME
--and atc.table_name=ac.table_name
WHERE AO.OBJECT_TYPE = 'TABLE'
AND AO.OWNER = 'XYZ'
AND AO.OBJECT_NAME='ABC'
AND CONSTRAINT_TYPE IN ('P','R')
ORDER BY AO.OBJECT_NAME, ATC.COLUMN_NAME
I think your join conditions are not proper and owner must be used in join conditions as all_ views are uses
You should try following query:
SELECT AT.OWNER,
AT.TABLE_NAME,
LISTAGG(ATC.COLUMN_NAME,',') WITHIN GROUP (ORDER BY ATC.COLUMN_ID) AS COLUMN_NAMES,
LISTAGG(ATC.DATA_TYPE,',') WITHIN GROUP (ORDER BY ATC.COLUMN_ID) AS DATA_TYPE,
LISTAGG(ATC.NULLABLE,',') WITHIN GROUP (ORDER BY ATC.COLUMN_ID) AS NULLABLE,
AC.CONSTRAINT_NAME
FROM ALL_TABLES AT
JOIN
ALL_TAB_COLUMNS ATC
ON AT.TABLE_NAME=ATC.TABLE_NAME
AND AT.OWNER = ATC.OWNER
LEFT JOIN
ALL_CONS_COLUMNS ACC
ON ATC.COLUMN_NAME = ACC.COLUMN_NAME
AND ACC.TABLE_NAME = AT.TABLE_NAME
AND AC.OWNER = ACC.OWNER
LEFT JOIN ALL_CONSTRAINTS AC
ON AT.TABLE_NAME = AC.TABLE_NAME
AND AC.CONSTRAINT_NAME = ACC.CONSTRAINT_NAME
AND AT.OWNER = AC.OWNER
WHERE AT.OBJECT_TYPE = 'TABLE'
AND AT.OWNER = 'XYZ'
AND AT.OBJECT_NAME='ABC'
AND CONSTRAINT_TYPE IN ('P','R')
GROUP BY AT.OWNER,
AT.TABLE_NAME,
AC.CONSTRAINT_NAME
Cheers!!

Resources