Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
759 views
in Technique[技术] by (71.8m points)

plsql - Oracle PL/SQL select into variables

I am trying to run the following query in SQL Developer, but I am receiving an error. I am trying to declare two local variables (var_num1 and payDate) and then set the variables. Does anyone know what I can be doing incorrectly? I know Oracle SQL is a little different than SQL Server.

DECLARE
  var_num1 number; 
  payDate date;
 BEGIN 
  var_num1 := 100; 
  payDate := '10/1/2013'
  BEGIN 
    SELECT * FROM Paycode WHERE PaycodeID = var_num1 and PaycodeDate = payDate;
  End;
END; 

Error report:
ORA-06550: line 6, column 2:
PLS-00428: an INTO clause is expected in this SELECT statement
06550. 00000 -  "line %s, column %s:
%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You cannot use SELECT without INTO clause in PL/SQL. The output of the SELECT must be stored somewhere. Eg. table or variable or perhaps a record. See example below how to store result of the SELECT statement into record.

DECLARE
  var_num1 number; 
  payDate date;
  v_result Paycode%ROWTYPE;
BEGIN 
  var_num1 := 100; 
  payDate := '10/1/2013';

    SELECT * INTO v_result
    FROM Paycode
    WHERE PaycodeID = var_num1 and PaycodeDate = payDate;

END; 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...