Following from the below question, I have a query:

Declaring a variable and setting its value from a SELECT query in Oracle

Query:

Is it possible to write some equivalent oracle statement for the following SQL statement:

    declare @var_name nvarchar(5)='Vinay'
    select @var_name    

I know the question is far too basic, but i am just a beginner in Oracle, and would appreciate if you guys could also help me learning it. Thanks in advance.

有帮助吗?

解决方案

  1. Oracle variables are not prefixed with @ like in SQL Server.
  2. Oracle queries must have a FROM clause; there is a system-provided dummy table called DUAL for this
  3. In Oracle, the line between SQL code and procedural code is far harder than in SQL Server.

If all you want to do is print out the value of a variable, you will need to do something like this:

DECLARE
  var_name NVARCHAR2(5) := 'Vinay';
BEGIN
  -- Enable printing
  DBMS_OUTPUT.ENABLE();
  -- Print the value
  DBMS_OUTPUT.PUT_LINE(var_name);
END;
/

Note: If you are doing this in SQL*Plus, you must issue SET SERVEROUTPUT ON to see the result.

其他提示

I hope this should work

DECLARE
   var_name varchar2(5) := 'Vinay' ;

BEGIN
   DBMS_OUTPUT.PUT_LINE(var_name);
END;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top