How I can write this code ( Sql Server) to work in Oracle PL/SQl ?
DECLARE #ID INTEGER
SELECT #ID = ISNULL(MAX(EmployeeID),0) + 1
FROM EmployeeTable
Below is the equivalent code for oracle
declare
id number;
begin
select nvl(max(employeeid),0)+1 into id from employeetable;
dbms_output.put_line(id);
end;
Try this PL/SQL block:
DECLARE ID INTEGER;
BEGIN
SELECT
NVL(MAX(EmployeeID),0) + 1 INTO ID
FROM
EmployeeTable
END;
/
Related
Is there a way to build a select statement in oracle using values stored in variables?
For example can you do this:
declare
tbl_var varchar2(10) := "map_set";
begin
select count(*) from tbl_var;
end;
Yes there is, using execute immediate:
declare
tbl_var varchar2(10) := 'map_set';
result number;
begin
execute immediate 'select count(*) from '||tbl_var into result; --save result into variable
dbms_output.put_line('Total rows:'||result); --print result
end;
Second way, you can create a function that receives table name as parameter and return the count:
create function get_count(tbl_var varchar2) return number is
result number;
begin
execute immediate 'select count(*) from '||tbl_var into result;
return result;
end;
After create the function you can query it like this:
select get_count('map_set') from dual;
I am writing code on entity framework 6.0, linq on asp.net, sql server platform.
I've written a stored procedure which supposed to return an output value (0 or 1)
Firstly I wrote SP by using output parameter as follows:
ALTER proc [dbo].[checkUser]
(
#oNo int,
#pw varchar(50),
#val int output
)
as
begin
if EXISTS (select * from Users where #oNo=sNo and #pw=password)
begin
set #val=1
end
if not exists(select * from Users where #oNo=sNo and #pw=password)
begin
set #val=0
end
end
Didnt work. It returned -1. Then I changed SP like this:
ALTER proc [dbo].[checkUser]
(
#oNo int,
#pw varchar(50)
)
as
begin
if EXISTS (select * from Users where #oNo=sNo and #pw=password)
begin
return 1
end
else
begin
return 0
end
end
Each ways stored procedure returns only -1
Can you help me please, where am i doing wrong?
Try to replace "return" by "select".
if EXISTS (select * from Users where #oNo=sNo and #pw=password)
begin
select 1
end
else
begin
select 0
end
I'm pretty new to Microsoft T-sql (Use to Oracle PL/SQL) and I ran into a annoying problem with a very simple procedure.
I created the following procedure
CREATE PROCEDURE [dbo].[ele_test] #productId INT
AS
DECLARE #productCode VARCHAR(100);
DECLARE #productDescription VARCHAR(100);
DECLARE #error VARCHAR(100);
--Fetch product
IF #productId != NULL
BEGIN
SELECT #productCode = ProductCode
,#productDescription = ProductDescription
FROM d_afterpay.dbo.Product
WHERE ProductId = #productId
END
IF ##ROWCOUNT = 0
BEGIN
SET #error = 'Product not found: ' + cast(#productId AS VARCHAR(19))
RAISERROR (#error,16,1);
END
And when I run it this way:
exec ele_test 5
I get:
Msg 50000, Level 16, State 1, Procedure ele_test, Line 20
Product not found. Productid : 5
Yet when I run just the query like this:
SELECT * FROM d_afterpay.dbo.Product
WHERE ProductId = 5
I do get a proper result...
Any idea what I am doing wrong?
Your query syntax is slightly wrong, change the query to read:
IF (#productId IS NOT NULL)
instead of using !=
This meant your SELECT statement was never being called hence why the product was always missing.
declare #amount float
declare #result char (20)
select #amount = cost from PurchaseDoc where id = 1
if #amount > 0 set #result = 'ok'
else set #result = 'empty'
print #result
Here is one representation of your script which can be executed against an Oracle database:
DECLARE
amount NUMBER;
result varchar2(20);
BEGIN
SELECT SUM(cost) INTO amount
from PurchaseDoc
WHERE id = 1;
if (amount > 0)
then
result := 'ok';
else
result := 'empty';
end if;
dbms_output.put_line(result);
END;
/
See this link for some useful information about dbms_output.
I recommend taking a look at some PL/SQL tutorials, such as the ones located at www.plsql-tutorial.com.
EDIT
Updated select statement based on suggestion by Cade Roux
There is really no need for PL/SQL here, a normal select statement with 'decode' function can do the trick.
SELECT DECODE(SUM(COST),0,'empty','ok') FROM PurchaseDoc where id = 1;
Hi I have been trying this today and haven't had any luck. this stored procedure does not work :(
CREATE OR REPLACE PROCEDURE LEAD_PURGE(closed IN DATE,
oprtr IN INTEGER,
leadscount OUT INTEGER)
is
BEGIN
SELECT COUNT(*) FROM LEADS_DELETED INTO leadscount;
COMMIT;
END LEAD_PURGE;
The INTO clause is misplaced. It should be:
SELECT COUNT(*) INTO leadscount FROM LEADS_DELETED
you have the into at the wrong place.
Try something like this instead and proceed from there:
declare
cnt number;
begin
select count(*)
into cnt
from leads_delete;
end;
Another way :
declare
cnt number;
cmd varchar2(100);
begin
cmd := 'select count(*) from leads_delete';
execute immediate cmd into cnt;
end;