Click here to Skip to main content
14,427,367 members
Rate this:
Please Sign up or sign in to vote.
See more:
I Need to convert below MS SQL Server query to oracle format

declare @n int
set @n=10
select id,@n from Table


Please help me.

What I have tried:

I Have tried the below query but it's not working

DECLARE n number:=10;
Begin

select id,:n from Table

end
Posted
Updated 10-Jan-20 7:50am
Comments
Dave Kreskowiak 10-Jan-20 10:22am
   
Sorry, I just have to vent about a pet peeve of mine. THERE IS NO SUCH THING AS "CALLING A VARIABLE"!

You use the content of a variable.
Rate this:
Please Sign up or sign in to vote.

Solution 2

If I understand your question correctly, you're trying to return data from PL/SQL using the query mentioned. While a procedure in SQL Server can return data as a result set, an Oracle procedure cannot do this the same way.

Consider the following example
DECLARE
   n number;
   numvalue number;
   datevalue date;
BEGIN
   n := 10;

   SELECT SYSDATE, n
   FROM dual;
END;
/

This would case an ORA-06550 error since an INTO clause is expected. In other words, you need to return the data to variables.

Note that I didn't use colon before the variable names. Colon is typically used in client side programs to indicate a bind variable, but not inside a PL/SQL block.

Now, if you change the block to the following
DECLARE
   n number;
   numvalue number;
   datevalue date;
BEGIN
   n := 10;

   SELECT SYSDATE, n
   INTO datevalue, numvalue
   FROM dual;

   RAISE_APPLICATION_ERROR(-20001, 'Date: ' || TO_CHAR(datevalue) || ', numvalue: ' || TO_CHAR(numvalue));
END;
/

When the block is executed the query works fine and you can see the result via the custom exception. The result should be
ORA-20001: Date: 10-JAN-20, numvalue: 10

So now the data is known inside the block. In order to return this data to the calling program you have several options. For example you can use a REF_CURSOR, define table type use a pipeline function and so on.

For few examples to return data, have a look at Examples for Creating Oracle Functions[^]
   
Comments
Maciej Los 10-Jan-20 15:18pm
   
5ed!
Rate this:
Please Sign up or sign in to vote.

Solution 1

   
Comments
Maciej Los 10-Jan-20 15:18pm
   
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100