1

我正在尝试在 Oracle 中执行与此等效的 SQL。基本上,我需要将给定列中的值放入变量中,然后使用这些变量来查询表

DECLARE @transID INT
       ,@pol_no varchar(20)

SELECT @pol_no = 'xxx'

SELECT @transID = p.transID 
  FROM policy_dimension p 
 WHERE p.pol_no = @pol_no

SELECT pd.* 
  FROM property_dimension pd 
 WHERE pd.trans_id = @transID

我试过这个

DECLARE 
  policy_no VARCHAR2(20);
  transaction_id int;
BEGIN
  :policy_no := '20P0000175';

  SELECT pd_transaction_id
    INTO transaction_id
    FROM policy_dimension p
   WHERE p.pd_policy_no = :policy_no
     AND ROWNUM = 1 
   ORDER BY pd_transaction_id desc;

   BEGIN
     SELECT *
       FROM property_dimension pd
      WHERE pd.pd_transaction_id = :transaction_id;
   END;


END;
/
4

3 回答 3

0

丢失冒号

DECLARE 
  v_policy_no VARCHAR2(20);
  v_transaction_id number;
  v_some_value number;
BEGIN 
 -- no colon here ( :policy_no )
  v_policy_no := '20P0000175';

  SELECT pd_transaction_id
    INTO v_transaction_id -- no colon here
    FROM policy_dimension p
   WHERE p.pd_policy_no = v_policy_no -- no colon here
     AND ROWNUM = 1 
   ORDER BY pd_transaction_id desc;

   BEGIN
     SELECT some_column into v_some_value
       FROM property_dimension pd
      WHERE pd.pd_transaction_id = v_transaction_id; -- no colon here
   END;


END;
/
于 2012-10-15T16:37:38.180 回答
0

此代码显示pd_transaction_id来自property_dimension(我不知道其他列):

set serveroutput on -- need to display message using dbms_output.put_line
DECLARE 
  policy_no VARCHAR2(20);
  transaction_id int;
  cur_pd_transaction_id int;

  CURSOR cur (param VARCHAR2)  -- cursor with parameter
  is       
    SELECT pd_transaction_id /*You can add here more columns*/
    FROM property_dimension pd
    WHERE pd.pd_transaction_id = param;
begin
   policy_no := '20P0000175';

   SELECT pd_transaction_id
     INTO transaction_id -- set policy_dimension.pd_transaction_id to transaction_id
    FROM policy_dimension p
    WHERE p.pd_policy_no = policy_no; 

   open cur(transaction_id); -- open cursor with parameter transaction_id
   fetch cur into cur_pd_transaction_id;    
    LOOP
    FETCH cur INTO cur_pd_transaction_id;
       EXIT WHEN cur%NOTFOUND;
       dbms_output.put_line('pd_transaction_id: '||cur_pd_transaction_id);
   END LOOP;

   close cur;  
end;

下面是更简单的方法:

set serveroutput on
DECLARE 
  policy_no VARCHAR2(20);
  transaction_id int;
begin
   policy_no := '20P0000175';

   SELECT pd_transaction_id
     INTO transaction_id 
    FROM policy_dimension p
    WHERE p.pd_policy_no = policy_no; 

   for cur in ( SELECT *
                FROM property_dimension pd
                WHERE pd.pd_transaction_id = transaction_id)   
    LOOP
       dbms_output.put_line('pd_transaction_id: '||cur.pd_transaction_id);
   END LOOP;

end;

在这里您可以找到有关游标的更多信息。

于 2012-10-15T17:42:37.690 回答
0

变量之前的符号:使其成为Bind变量,在您的情况下这不是必需的。PLSQL 块内的ASELECT不会按原样显示结果,您需要遍历结果集的每条记录并调用dbms_output包以显示输出。因此,对您的 PLSQL 块进行如下微小更改应该可以使其正常工作。

SET SERVEROUTPUT ON
DECLARE 
  policy_no VARCHAR2(20);
  transaction_id int;
BEGIN
  policy_no := '20P0000175';

 BEGIN             --Since the select query can return no rows which will cause this Block to fail, use proper exception handling
    SELECT pd_transaction_id
      INTO transaction_id
      FROM policy_dimension p
     WHERE p.pd_policy_no = policy_no
       AND ROWNUM = 1 
     ORDER BY pd_transaction_id desc;
 EXCEPTION
 WHEN NOTFOUND THEN
     dbms_output.put_line ('No pd_transaction_id found');
     transaction_id := '0';
 END;

   FOR c_rec in (SELECT *
                   FROM property_dimension pd
                  WHERE pd.pd_transaction_id = transaction_id) --Notice the LOOP
   LOOP
    dbms_output.put_line ('Displaying - ' || c_rec.column1 ||' '||c_rec.column2||....); --Display every row with columns column1, column2 , column3.... as the column names
   END LOOP;
END;
/
于 2012-10-15T18:14:07.737 回答