2016-06-21 3 views
0

을 감안할 때 두 테이블스크립트 변수

USERS 
UID NAME 
1 KEN 

ADRESS 
AID UID CITY 
1 1 LONDON 

. 출력은 물론

UID NAME 
1 KEN 

AID UID CITY 
1 1 LONDON 

답변

1

는 SQL Developer에서이 작업을 수행하는 적어도 두 가지 방법이 있습니다해야

select UID into u_id from USERS where NAME='KEN'; 
select * from USERS where UID = u_id; 
select * from ADRESS where UID = u_id; 

:이 작동하지 않습니다

, 나는 u_id 변수를 할당 할 수 . 바인드 변수와

:

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; 

이 경우에 당신은 간단하게 할 수있는 상태 :

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; 

variable command 대해 자세히 알아

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; 

또는 대체 변수와

, execute command, column command 및입니다.및 substitution variables이 SQL * Plus 설명서에 포함되어 있습니다. 대부분 SQL Developer에도 적용됩니다. 키/예약어 방지하기 위해 약간 다른 열 이름으로 만든 테이블

데모 : 스크립트로

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; 

실행을, 스크립트 출력 창을 보여줍니다

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  

당신은을 억제 할 수 물론 set feedback offPL/SQL procedure successfully completed 메시지.

+0

Alex, 대단히 감사합니다. 바인드 변수 예제는 나를 위해 완벽하게 작동합니다. NEW_VALUE 동사는 Oracle SQL Developer SQL 워크 시트 편집기에서 오류로 표시되지만 스크립트는 여전히 작동합니다. – weberjn

+0

예, 구문에 약간의 표식이 있습니다. 아마도 SD 포럼을 통해 그들이 실제로보고되었는지 확인해야합니다. * 8) –