|
我目前正在学习PL / SQL,所以我仍然是新手。假设您有一个生产数据库,您可以使用Oracle SQL( J2 t8 k8 D7 c7 j* {; r
Developer将其连接到该数据库。您只有该数据库的READ特权。因此,您不能创建或编辑任何表。% a, m; a2 e9 j' a: ^* ]" j- p
我的问题是,如果我有大量的ID,我必须将其与该数据库中的表连接起来,该怎么办?
9 L$ o3 U1 A2 ?显然,我可以将ID加载到临时表上,然后进行联接,但这确实很乏味,因为我只有READ特权。硬编码ID也不是一种选择,因为列表太大。
# T \6 t g0 R1 w u5 V8 O另外请注意,我知道TEMPORARY表的概念。但不幸的是,我也没有创建这些特权。
# ~: c4 P8 K& H8 KSQL Developer中有什么解决方案可以加载ID列表以与数据库中的表匹配?
$ b0 X0 b: O# [, g, I q 9 H7 g! D6 y. }4 ], w4 |) ?& O
解决方案:" o! B9 T. U# Q. r' k
& n( A. s0 E1 r# v, c& b- }3 }+ m; e
4 ]0 Q! O# Z/ Z" T# G1 o7 {: M; }! C, N8 n0 E+ ]4 E( s# B
使用收藏% O: Y; Z" P& o' B" O
VARIABLE cursor REFCURSOR;
1 _( `, d0 u3 ?! y7 y6 rDECLARE" ?0 B$ r% K: c% A
your_collection SYS.ODCIVARCHAR2LIST := SYS.ODCIVARCHAR2LIST();$ c2 f! t( E _. n+ C' v* h1 o7 Y
BEGIN- H$ Z( t3 e2 P# o: w
your_collection.EXTEND( 10000 );3 y& N, z" F) H! ]# }4 g
FOR i IN 1 .. 10000 LOOP
7 D3 F8 c4 g7 s, Y( Q3 {$ T -- Populate the collection.& X* i y- H" T4 i- s) q& O
your_collection(i) := DBMS_RANDOM.STRING( 'x', 20 );1 \: b( ~1 U8 S" A, t" ]
END LOOP;6 _7 m9 D u3 \$ z0 ~5 i
OPEN :cursor FOR5 P7 W: {2 Z# @6 R7 b6 E Q
SELECT t.*
. Q _1 M }5 d- R% J FROM your_table t1 M' m& ^) j3 S1 Z( _: b) P( ~
INNER JOIN
+ T: T0 f% Z7 Y- w TABLE( your_collection ) c% m, g8 @# g) ~' o) h: h
ON t.id = c.COLUMN_VALUE;3 E% p0 e1 N1 ]* u& M% S. @
END;
8 `& Y$ U+ J7 m# l/
; ]- J. m+ ~% H. v rPRINT cursor;1 o# W% }# }5 B2 J2 ^
或通过java做同样的事情: z; J' f2 M W3 Y
import java.sql.Connection;
7 w8 J$ s3 i9 H* i. Pimport java.sql.DriverManager;. [- N7 m( w2 I( |
import java.sql.PreparedStatement;
# \& l! w0 e9 N* I, ^1 r9 Yimport java.sql.ResultSet;9 F# y* P" z9 b
import java.sql.SQLException;0 ^0 i: |, Y7 i# _* a7 Y3 X
import oracle.jdbc.OraclePreparedStatement;# T/ Q2 ~2 |% n3 L- i! Q; S0 k
import oracle.sql.ARRAY;6 W( ~, J- m2 ^' n
import oracle.sql.ArrayDescriptor;% M6 _+ M8 ^0 B! H, T0 Q
public class TestDatabase2 {
9 D% @( L! {* S public static void main(String args[]){" B3 l5 e7 \: y& _& y( U8 m% w+ d
try{
$ G% T: J1 f% g! \/ |& | Class.forName("oracle.jdbc.OracleDriver");! ?8 c. U5 N! m' x+ O3 c
Connection con = DriverManager.getConnection("jdbcracle:thinlocalhost:1521:XE","username","password");
0 u6 Q8 v+ D4 d3 r String[] ids = { "1", "2", "3" };
: ~$ j& c% q) m8 e$ ?: B2 L8 N& @* p ArrayDescriptor des = ArrayDescriptor.createDescriptor("SYS.ODCIVARCHAR2LIST", con);
1 s& v) o! I: p. }! Q. h7 R PreparedStatement st = con.prepareStatement("SELECT t.* FROM your_table t INNER JOIN TABLE( :your_collection ) c ON t.id = c.COLUMN_VALUE");
3 K0 @: ]- E. B3 ] // Passing an array to the procedure - % e4 Z8 G% `$ G. C; p! Y
((OraclePreparedStatement) st).setARRAYAtName( "your_collection", new ARRAY( des, con, ids ) );
0 g5 A- m/ }: u0 i ResultSet cursor = st.executeQuery();
2 D, C- F8 U# Z2 _1 z' T while ( cursor.next() )( v; H$ F+ Z: V" ~# R
{3 ^8 Z/ x2 s! K) Q: I1 w
int id = cursor.getInt(1);
+ R6 i& l7 R+ @4 B6 V; ? double column1 = cursor.getDouble(2);1 N3 p& Y0 F* c: y1 e |
double column2 = cursor.getDouble(3);" e; m; F5 R% b( ~- @" K# _& K
System.out.println( String.format( "Id: %5d", id ) );; n! S$ o S& h, O B- H
System.out.println( String.format( " Column1: %s", column1 ) );# D1 B! k8 O% `/ I( K4 p+ @
System.out.println( String.format( " Column2: %s", column2 ) );* @/ c$ F {) @- d, t& s# w
}1 {* n3 p$ W. n! G
} catch(ClassNotFoundException | SQLException e) {7 ~5 V7 \! j/ f4 j" O8 u3 }0 J+ v
System.out.println(e);
" [8 {6 k" D& m/ j# i$ r }9 j9 c* `- V' R* ]# y( Y
}
; x+ @( ]+ l- ~# f, n" u6 Y! }* z} |
|