Selecting a count into a variable in oracle - database

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;

Related

invalid context at is table of cursor_name%rowtype

..is not valid in the context where it is used.. SQLCODE=-206, SQLSTATE=42703
can you please let me know the issue and clarify me why?
environment: DB2 (oracle compatibility enabled)
the code resembles as:`
CREATE OR REPLACE PACKAGE body PACK1 IS
PROCEDURE PROC1 AS
CURSOR CUR1 IS SELECT COL1,COL2 FROM TABLE1 WHERE NOT EXISTS ( SELECT * FROM TABLE2 ) AND COL3 = 'Y';
TYPE TYP1 IS TABLE OF CUR1%ROWTYPE INDEX BY BINARY_INTEGER; --facing issue here.. please clarify
ALIAS1 TYP1;
BEGIN
...STATEMENT1..
OPEN CUR1;
LOOP
FETCH CUR1 BULK COLLECT INTO ALIAS1 LIMIT 1000;
...STATEMENTS2...
EXIT WHEN CUR1%NOTFOUND;
END LOOP;
....STATEMENTS3...
END PROC1;
END PACK1;`
line4 ([provided comment in the line where error is encountered). Please help me in resolving this issue. Thanks in advance.
move declaration of your TYPE up before procedure declaration:
CREATE OR REPLACE PACKAGE BODY PACK1
IS
TYPE TYP1 IS TABLE OF CUR1%ROWTYPE
INDEX BY BINARY_INTEGER;
PROCEDURE PROC1
AS
CURSOR CUR1
IS
SELECT COL1, COL2
FROM TABLE1
WHERE NOT EXISTS (SELECT * FROM TABLE2) AND COL3 = 'Y';
ALIAS1 TYP1;
BEGIN
...STATEMENT1..
OPEN CUR1;
LOOP
FETCH CUR1 BULK COLLECT INTO ALIAS1 LIMIT 1000;
...STATEMENTS2...
EXIT WHEN CUR1%NOTFOUND;
END LOOP;
....STATEMENTS3...
END PROC1;
END PACK1;
I have faced this issue long ago, hope this helps

How I can write this code to work in oracle?

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;
/

building a select statement in oracle using values stored in variables

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;

PL/SQL querying a table on multiple databases

I'm a bit stuck. I have a table with a list of database names. I want to query for the database name and then query this database to return details from its "systemtable".
I've been trying to use 2 cursors but its not quite working out for me (just can't find the syntax), any pointers/help would be appreciated.
declare
cursor c_dbNames is select dbname
from DB_INFO order by name ASC;
v_curr_dbname VARCHAR2(60);
begin
open c_dbNames;
LOOP
FETCH c_dbNames into v_curr_dbname;
EXIT WHEN c_dbnames%NOTFOUND;
begin
cursor c_dbDetails is select value
from SYSTEMTABLE#'||v_curr_dbname||' order by name ASC;
v_curr_detail VARCHAR2(60);
open c_dbDetails;
LOOP
FETCH c_dbDetails into v_curr_detail;
EXIT WHEN c_dbDetails%NOTFOUND;
htp.p('<tr><th>'||v_curr_detail||'</th></tr>');
END LOOP;
close c_dbDetails;
end;
END LOOP;
close c_dbnames;
end;
You have to adjust it a little:
declare
cursor c_dbNames is
select 'dual' dbname from dual union all
select 'dual' dbname from dual union all
select 'dual' dbname from dual
order by dbname ASC;
v_curr_dbname VARCHAR2(60);
begin
open c_dbNames;
LOOP
FETCH c_dbNames into v_curr_dbname;
EXIT WHEN c_dbnames%NOTFOUND;
DECLARE
v_cursor integer;
v_rows integer;
v_curr_detail char(20);
begin
v_cursor := DBMS_SQL.OPEN_CURSOR;
DBMS_SQL.PARSE(v_cursor, 'select ''c_dbDetails'' c_dbDetails FROM ' || v_curr_dbname, DBMS_SQL.NATIVE);
DBMS_SQL.DEFINE_COLUMN_CHAR(v_cursor, 1, v_curr_detail, 20);
v_rows := DBMS_SQL.EXECUTE(v_cursor);
loop
if DBMS_SQL.FETCH_ROWS(v_cursor) = 0 then
exit;
end if;
DBMS_SQL.COLUMN_VALUE_CHAR(v_cursor, 1, v_curr_detail);
DBMS_OUTPUT.PUT_LINE('<tr><th>' || v_curr_detail ||'</th></tr>');
end loop;
DBMS_SQL.CLOSE_CURSOR(v_cursor);
end;
END LOOP;
close c_dbnames;
end;
declare
cursor databases_c is
-- put your database links here
select 'XXX' as dbname from dual union
select 'YYY' from dual;
v_global_name varchar2(4000);
begin
for v_dbname in databases_c loop
-- query the database details
execute immediate
'select global_name from global_name#' || v_dbname.dbname
into v_global_name;
dbms_output.put_line(v_global_name);
end loop;
end;
/
Output:
SQL> #so27.sql
XXX
YYY
PL/SQL procedure successfully completed.
SQL>

SQL Function error [invalid use of side-effecting operator 'select' within a function]

Can any one help me to create function in sql server 2012 ,I M new in sql it shows error [invalid use of side-effecting operator 'select' within a function]
CREATE FUNCTION Get_RearEnd_Outflow( #I_TXN_ID INT)
RETURNS int
AS BEGIN
DECLARE #V_COUNT INT
SELECT COUNT(*) INTO #V_COUNT FROM IRR_CHARGES_M
RETURN #V_COUNT
END
The correct syntax should be
CREATE FUNCTION Get_RearEnd_Outflow( #I_TXN_ID INT)
RETURNS int
AS BEGIN
DECLARE #V_COUNT INT
SELECT #V_COUNT = (SELECT COUNT(*) FROM IRR_CHARGES_M)
RETURN #V_COUNT
END
however SET is the better way to assign variable in T-SQL
SET #V_COUNT = (SELECT COUNT(*) FROM IRR_CHARGES_M)
EDIT 2: For multiple assignment you can use SELECT
For multiple assin
SELECT #variableOne = someColumn, #variableTwo = someColumn from tblTable
EDIT: From the MSDN link about slect
For assigning variables, we recommend that you use SET #local_variable instead of SELECT #local_variable. For more information, see SET #local_variable.
CREATE FUNCTION Get_RearEnd_Outflow()
RETURNS int
AS
BEGIN
DECLARE #COUNT INT;
SET #COUNT =(SELECT COUNT(*) FROM IRR_CHARGES_M);
return #COUNT
END
GO
or use :
CREATE FUNCTION Get_RearEnd_Outflow()
RETURNS int
AS
BEGIN
return (SELECT COUNT(*) FROM IRR_CHARGES_M)
END
GO
Try something like this:
CREATE FUNCTION Get_RearEnd_Outflow( #I_TXN_ID INT)
RETURNS int
AS BEGIN
DECLARE #V_COUNT INT;
SET #V_COUNT = (SELECT COUNT(*) FROM IRR_CHARGES_M);
RETURN #V_COUNT;
END

Resources