GraphQL Mutation Insert using Inner Query - graphql

How can I perform the following SQL Insert Query as a GraphQL Mutation Insert?
INSERT INTO User (id, name, user_type_id)
VALUES
(1, "Name", (SELECT id FROM UserType WHERE user_type="Guest"))
Provided that there is a One to Many relation from UserType table (id column) to User table (user_type_id column).

You should checkout an ORM that work with your technology.
like 'sequelize' for example.

Related

Spring JPA: Need to join two table with dynamic clauses

Requirement is as below:
Table 1 - Order(ID, orderId, sequence, reference, valueDate, date)
Table 2 - Audit(AuditId, orderId, sequence, status, updatedBy, lastUpdatedDateTime)
For each record in ORDER table, there could be one or many rows in AUDIT table.
User is given a search form where he could enter any of the search params including status from audit table.
I need a way to join these two tables with dynamic where clause and get the top row for a given ORDER from AUDIT table.
Something like below:
select * from
ORDER t1, AUDIT t2
where t1.orderid = t2.orderId
and t1.sequence = t2.sequence
and t2.status = <userProvidedStatusIfAny>
and t2.lastUpdatedDateTime in (select max(t3.lastUpdatedDateTime) --getting latest record
AUDIT t3 where t1.orderid = t3.orderId
and t1.sequence = t3.sequence)
and t1.valudeDate = <userProvidedDataIfAny> ..... so on
Please help
-I cant do this with #Query because i have to form dynamic where clause
-#OneToMany fetches all the records in audit table for given orderId and sequence
-#Formula in ORDER table works but for only one column in select. i need multiple values from AUDIT table
Please help

Hibernate - How to get single value from another table without primary/Foreign key relationship?

I have 2 tables(Employee & Department).
Employee(ID, employee name, dept id, employee address) --> ID primary key
Department(ID, department name, address) --> ID primary key
Note: Consider no primary key / foreign key relationship between these 2 tables
Now based on dept id in Employee, i need to fetch its corresponding dept name from Department & display it in Employee.
Employee(ID, employee name, dept id(Display dept name instead of dept id, query department table to get dept name through passing dept id), employee address)
What are all possible ways to achieve this?
Regards
Raj
I see at least three possible solutions:
Create a "normal" SQL query for this, joins can be perfectly done without a PK/FK-relationship.
Perform two SQL queries: One query for fetching the employee, then extract the ID and then a second query to fetch the department by ID.
Change your JPA entities to include the relationship as you would normally do having a #OneToMany relationship so you can load the employee with its department as usual. Same as 1 applies, joins (whether done by you or by the JPA provider) don't need a PK/FK-relationship.
Whilst 1 can be done with plain SQL, 2 can be done using the entity manager without the need to write "plain" SQL. 3 would be the easiest solution, obviously.
You can #Query in repository and write your JPA query using JOIN.
Sample Query:
#Query( value = "select new map(employee.name as empName, employee.id as id, department.id as depId, department.name as depName) from Employee employee INNER JOIN Department department ON employee.depId = department.id where employee.id = :id" )
public Map<String,Object> getEmployeeById(long id);

Insert data to table column from column of different table

I am using Oracle Database.
I have view called VW_MREQ
It has column as follows :
M_Product_ID
AD_Client_ID
AD_ORG_ID
It has records inside.
Then, I have empty table called M_Requisition
It has column as follows :
M_Product_ID
AD_Client_ID
AD_ORG_ID
DESCRIPTION
CREATEDBY
I am making Procedure and would like to insert Data manually to M_Requisition, the foreign key is M_Product_ID and I want the AD_Client_ID and AD_ORG_ID to be the same as in VM_REQ as I insert M_Product_ID to M_Requisition manually.
INSERT INTO M_Requisition(M_Product_ID, AD_Client_ID, AD_ORG_ID, DESCRIPTION, CREATEDBY) VALUES(123, ?? , ??,"Insert Data","Me")
I plan to use SELECT INTO but still confused how I arrange it as I am newbie in Oracle.
Your help will be useful.
You could use the insert-select syntax, and just query the hardcoded values as literals:
INSERT INTO M_Requisition
(M_Product_ID, AD_Client_ID, AD_ORG_ID, DESCRIPTION, CREATEDBY)
(SELECT 123, AD_Client_ID, AD_ORG_ID, 'Insert Data', 'Me'
FROM VW_MREQ)

Inserting data from table (SELECT) with REF - Oracle Database

I'm trying to insert all data from Pacient table to Pacient_OR table (Object-Relational). Is there a simple way to do that (one script), if Pacient table has column with Pojistovna_ID (foreign key) and in Pacient_OR table there is REF to the Pojistovna_OR. Both Pojistovna and Pojistovna_OR are populated with the same data, but one is relational, second is based on object type.
I tried this (and more ofc):
INSERT INTO pacient_or
(pacient_or.id,
pacient_or.jmeno,
pacient_or.prijmeni,
pacient_or.datum_narozeni,
pacient_or.rodne_cislo,
pacient_or.telefon,
pacient_or.krevni_skupina,
pacient_or.rodinna_anamneza,
pacient_or.adresa,
pacient_or.pojistovna)
SELECT pacient.id,
pacient.jmeno,
pacient.prijmeni,
pacient.datum_narozeni,
pacient.rodne_cislo,
pacient.telefon,
pacient.krevni_skupina,
pacient.rodinna_anamneza,
Adresa_typ(pacient.ulice, pacient.mesto, pacient.psc),
(SELECT Ref(poj)
FROM pacient pac,
pojistovna_or poj
WHERE pac.pojistovna_id = poj.id)
FROM pacient;
This code throws error:
single-row subquery returns more than one row
Do not use pacient pac in the subquery. Link it to the pacient in your main from-clause. And even better do not use a subquery for this.

sql statement to select and insert one value from one table rest from outside the tables

I have two db2 tables, table one contains rows of constant data with id as one of the columns. second table contains id column which is foreign key to id column in first table and has 3 more varchar columns.
I am trying to insert rows into second table, where entry into id col is based on a where clause and the remaining columns get values from outside.
table1 has columns id, t1c1, t1c2, t1c3
table2 has columns id, t2c1, t2c2, t2c3
to insert into table2, I am trying this query:
insert into table2 values
(select id from table1 where t1c2 like 'xxx', 'abc1','abc2');
I know it is something basic I am missing here.
Please help correcting this query.

Resources