Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 6: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.

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
SQL
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
SQL
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[^]
 
Share this answer
 
Comments
Maciej Los 10-Jan-20 15:18pm    
5ed!
 
Share this answer
 
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900