Re: is there a simpler way to do this?

From: VasantKumar Naidu <vask001_at_yahoo.com>
Date: 21 Feb 2003 23:28:09 -0800
Message-ID: <d9a73221.0302212328.419cc573_at_posting.google.com>


tanalbit_at_aol.com (Keith Langer) wrote in message news:<15c7b652.0302211516.5819a704_at_posting.google.com>...
> I am tring to select the a MAX value from a table into a variable.
> In SQL Server, I would do the following:
>
> declare _at_n_ID int
> SELECT _at_n_ID = MAX(Change_ID) FROM Daily_Changes
> print _at_n_id
>
>
> Is there a simple way to do this in Oracle? For now, the best I could
> come up with is a cursor or a for loop:
>
>
> declare
> n_ID NUMBER := 0;
> cursor ChangeIDCursor IS SELECT MAX(Change_ID) MaxID FROM
> Daily_Changes where daily_id=1;
> cursor_val ChangeIDCursor%ROWTYPE;
> BEGIN
> OPEN ChangeIDCursor;
> FETCH ChangeIDCursor INTO cursor_val;
> n_ID := cursor_val.MaxID;
> dbms_output.put_line('value is: ' || n_ID);
> CLOSE ChangeIDCursor;
> end;
>
>
> thanks,
> Keith

you can assign variable straight away to a variable like this:

declare
  n_ID number := 0;
begin
  SELECT MAX(Change_ID) MaxID into n_ID
  FROM Daily_Changes where daily_id=1;

  dbms_output.put_line('Value is: '||n_ID); end;

or if you are using SQL*Plus, u can create variable during runtime, check out the documentation for SQL Statements and PL/SQL Statements (Variable Declaration) and also the SQL*Plus documentation:

http://download-east.oracle.com/docs/cd/A81042_01/DOC/index.htm

vasant Received on Sat Feb 22 2003 - 08:28:09 CET

Original text of this message