sql >> Database teknologi >  >> RDS >> Oracle

Calling Oracle procedure med PL/SQL record Type fra Spring JDBC

Desværre giver Oracle JDBC-driveren ikke adgang til PL/SQL RECORD type, hverken for IN heller ikke for OUT parametre.

Løsning for et enkelt procedurekald:

Men du kan omgå denne begrænsning ved at bruge en anonym PL/SQL-blok i JDBC (eller Spring JDBC) direkte:

DECLARE
  rec MY_PACKAGE.MY_RECORD;
BEGIN

  -- Replace these by your actual record attributes:
  rec.first_name := ?;
  rec.last_name := ?;
  ...

  p_insertclient(rec);
END;
 

Dette fungerer perfekt til et enkelt procedurekald.

Solid løsning til mange af disse opkald:

Hvis du gør ovenstående ret ofte, er det værd at generere stubs, der producerer de anonyme PL/SQL-strenge automatisk, i stedet for at skrive dem manuelt hele tiden. Du kan automatisk finde alle PL/SQL RECORD indtast dit skema med følgende forespørgsel:

SELECT x.TYPE_OWNER, x.TYPE_NAME, x.TYPE_SUBNAME, a.ARGUMENT_NAME ATTR_NAME, a.SEQUENCE ATTR_NO, a.TYPE_OWNER ATTR_TYPE_OWNER, nvl2(a.TYPE_SUBNAME, a.TYPE_NAME, NULL) package_name, COALESCE(a.TYPE_SUBNAME, a.TYPE_NAME, a.DATA_TYPE) ATTR_TYPE_NAME, a.DATA_LENGTH LENGTH, a.DATA_PRECISION PRECISION, a.DATA_SCALE SCALE FROM SYS.ALL_ARGUMENTS a JOIN ( SELECT a.TYPE_OWNER, a.TYPE_NAME, a.TYPE_SUBNAME, MIN(a.OWNER) KEEP (DENSE_RANK FIRST ORDER BY a.OWNER ASC, a.PACKAGE_NAME ASC, a.SUBPROGRAM_ID ASC, a.SEQUENCE ASC) OWNER, MIN(a.PACKAGE_NAME) KEEP (DENSE_RANK FIRST ORDER BY a.OWNER ASC, a.PACKAGE_NAME ASC, a.SUBPROGRAM_ID ASC, a.SEQUENCE ASC) PACKAGE_NAME, MIN(a.SUBPROGRAM_ID) KEEP (DENSE_RANK FIRST ORDER BY a.OWNER ASC, a.PACKAGE_NAME ASC, a.SUBPROGRAM_ID ASC, a.SEQUENCE ASC) SUBPROGRAM_ID, MIN(a.SEQUENCE) KEEP (DENSE_RANK FIRST ORDER BY a.OWNER ASC, a.PACKAGE_NAME ASC, a.SUBPROGRAM_ID ASC, a.SEQUENCE ASC) SEQUENCE, MIN(next_sibling) KEEP (DENSE_RANK FIRST ORDER BY a.OWNER ASC, a.PACKAGE_NAME ASC, a.SUBPROGRAM_ID ASC, a.SEQUENCE ASC) next_sibling, MIN(a.DATA_LEVEL) KEEP (DENSE_RANK FIRST ORDER BY a.OWNER ASC, a.PACKAGE_NAME ASC, a.SUBPROGRAM_ID ASC, a.SEQUENCE ASC) DATA_LEVEL FROM ( SELECT lead(a.SEQUENCE, 1, a.SEQUENCE) OVER ( PARTITION BY a.OWNER, a.PACKAGE_NAME, a.SUBPROGRAM_ID, a.DATA_LEVEL ORDER BY a.SEQUENCE ASC ) next_sibling, a.TYPE_OWNER, a.TYPE_NAME, a.TYPE_SUBNAME, a.OWNER, a.PACKAGE_NAME, a.SUBPROGRAM_ID, a.SEQUENCE, a.DATA_LEVEL, a.DATA_TYPE FROM SYS.ALL_ARGUMENTS a WHERE a.OWNER IN ('MY_SCHEMA') -- Possibly replace schema here ) a WHERE (a.TYPE_OWNER IN ('MY_SCHEMA') -- Possibly replace schema here AND a.OWNER IN ('MY_SCHEMA') -- Possibly replace schema here AND a.DATA_TYPE = 'PL/SQL RECORD') GROUP BY a.TYPE_OWNER, a.TYPE_NAME, a.TYPE_SUBNAME ) x ON ((a.OWNER, a.PACKAGE_NAME, a.SUBPROGRAM_ID) = ((x.OWNER, x.PACKAGE_NAME, x.SUBPROGRAM_ID)) AND a.SEQUENCE BETWEEN x.SEQUENCE AND next_sibling AND a.DATA_LEVEL = (x.DATA_LEVEL + 1)) ORDER BY x.TYPE_OWNER ASC, x.TYPE_NAME ASC, x.TYPE_SUBNAME ASC, a.SEQUENCE ASC

Se mere detaljer om denne teknik i dette blogindlæg (hvorfra forespørgslen blev taget) .



  1. 3 måder at returnere rækker, der indeholder alfanumeriske tegn i SQL Server

  2. Sådan parses JSON i MYSQL trigger

  3. jOOQ EXTRACT(EPOCH FRA [field]) løsning?

  4. Mysql::Fejl:Dubleret indtastning