3

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:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:
1
  • That's a PL/SQL block, not a query. Commented Dec 10, 2013 at 8:22

2 Answers 2

7

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; 
Sign up to request clarification or add additional context in comments.

9 Comments

THank you for your response. Is "v_result" a variable or a table? I do not want to create a table.
It is basically a row stored inside variable (some might call that object). It is common way to store content of whole row when selecting all columns in the table (using *), see oracle.com/technetwork/issue-archive/2012/12-may/…
Oh great, so it is not actually creating a physical row or table? This is just stored in memory while the query is being executed, correct? Thanks for your help.
When I try this, the result is "annoymous block completed", it doesn't actually select the result.
@user3083460 It ran the select statement. You just didn't do anything useful with the result.
|
0

If you want to get a resultset back in SQL Developer with this code block you will need to open a ref cursor for the query e.g.

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

OPEN refc FOR
    SELECT * 
    FROM Paycode
    WHERE PaycodeID = var_num1 and PaycodeDate = payDate;

END; 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.