Issue with dynamic execute immediate query - sql

I have a code in my procedure that looks like this. But when i execute this code, i get the error as mentioned below.
The Error report that i got is:
Error report -
ORA-06553: PLS-306: wrong number or types of arguments in call to 'OGC_Y'
ORA-06512: at line 20
06553. 00000 - "PLS-%s: %s"
*Cause:
*Action:
The error has something to do with primary_flag = "Y" <-- this. How else can i write primary_flag = 'Y' inside a string?
The dynamic query is required in my case.
MY CODE IS:
DECLARE
p_assignee_id NUMBER := 10153;
time_stamp timestamp := '12-DEC-2011';
create_task_view_sql VARCHAR2(4000);
BEGIN
create_task_view_sql:=
'select unique cp.sub_last_name
from cs_sr_contact_points_v cp
where cp.incident_id = 55500
and cp.contact_phone is not null
and primary_flag = "Y"';
dbms_output.put_line(create_task_view_sql);
execute immediate create_task_view_sql;
END;

To embed quoted string in a quoted string, use two single quotes:
'...and primary_flag=''Y''';
Or you can use the newer q' syntax to avoid doubling up the embedded quotes:
q'[...and primary_flag='Y']';

Related

Wrong number or types of arguments error using cursors with parameters

I am getting this error trying to have an input parameter "var_city" in my cursor so I can use the user's input of a city.
Error report -
ORA-06550: line 17, column 5:
PLS-00306: wrong number or types of arguments in call to 'CUSTOMERS'
ORA-06550: line 17, column 5:
PL/SQL: SQL Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
p_city is commented out because I was trying to use that input parameter as the value used but it didn't work as well.
SET SERVEROUTPUT ON
SET VER OFF
-- ACCEPT p_city PROMPT 'Enter city name: '
DECLARE
v_custname si.customer.custname%TYPE;
v_total_spent NUMBER;
v_total_cars NUMBER;
CURSOR customers (var_city varchar2) IS
SELECT
custname
FROM
si.customer
WHERE
UPPER(custcity) = UPPER(var_city);
BEGIN
dbms_output.put_line('Customer Name'
|| 'Total Cars'
|| 'Total Spent');
OPEN customers;
LOOP
FETCH customers INTO v_custname;
EXIT WHEN customers%NOTFOUND;
cust_total_spending_and_cars(v_custname, v_total_cars, v_total_spent);
dbms_output.put_line(v_custname
|| v_total_cars
|| v_total_spent);
END LOOP;
CLOSE customers;
END;
/
An argument must be passed to the OPEN statement
Change this line:
OPEN customers;
To something like this:
OPEN customers('city name here');

Postgresql function auto add 's' character to the end of string

I have a function in PostgreSQL database. However, every time I run the function in PostgreSQL, it auto-adda the character s behind of the query, therefore, I receive an error when it executes.
An example query ends up looking like this:
WHAT IN HERE: query SELECT uom_id FROM ps_quantity where id = 11s
My version is PostgreSQL 9.2.13. How can I solve this? Thanks.
CREATE OR REPLACE FUNCTION select_field(
selected_table text,
selected_field text,
field_type_sample anyelement,
where_clause text DEFAULT ''::text)
RETURNS anyelement AS
$BODY$ DECLARE
-- Log
ME constant text := 'selected_field()';
-- Local variables
_qry varchar := '';
_result_value ALIAS FOR $0;
where_clause1 varchar := 'asdasdsad';
BEGIN
RAISE NOTICE 'FUNCTION: SELECT_FIELD';
-- BANGPH
RAISE NOTICE 'BANGPH - CHANGE 11s to 11';
_qry := 'SELECT uom_id FROM ps_quantity where id = 11';
RAISE NOTICE 'WHERE = %s', where_clause1;
RAISE NOTICE 'WHAT IN HERE: query %s', _qry;
As the manual explains:
instead of:
RAISE NOTICE 'WHAT IN HERE: query %s', _qry;
you need to use:
RAISE NOTICE 'WHAT IN HERE: query %', _qry;
Placeholders for the RAISE statement don't have a "type modifier", it's a plain %

How to Convert Output of listagg() function into XMLTYPE

I have one stored Procedure P_FP_GET_PATTERN.I expect output in following format
PATTERN_ID |PATTERN_NAME | SHIFT
1 Pattern 1 A,B,C,B,A
2 Pattern 2 C,B,A,C
I'm getting this output in SYS_REFCURSOR ALL_RESULT_SET.I need to pass it in XML format.But the moment i put that output in ALL_RESULT_SET_XML which is my out parameter of stored procedure of XMLTYPE using ALL_RESULT_SET_XML:= XMLTYPE(ALL_RESULT_SET);
Im getting an error as
Error encountered: ORA-31061: XDB error: special char to escaped char conversion failed.
I`m getting this error due to column shown using LISTAGG() function.Anybody can please tell me how to handle this ?
My stored Procedure
create or replace
PROCEDURE P_FP_GET_PATTERN
(
ALL_RESULT_SET_XML OUT XMLTYPE,
P_MESSAGE_ALL OUT VARCHAR2
)
AS
V_ERROR VARCHAR2(2000);
ALL_RESULT_SET SYS_REFCURSOR;
BEGIN
OPEN ALL_RESULT_SET FOR
SELECT PM.PATTERN_ID ,PM.PATTERN_NAME,
LISTAGG(SM.SHIFT_NUMBER) WITHIN GROUP (ORDER BY PD.INSTANCE_DAY) "SHIFT"
FROM T_FP_PATTERN_MASTER PM,
T_FP_PATTERN_DETAILS PD,
T_FP_SHIFT_MASTER SM
WHERE SM.SHIFT_ID= PD.SHIFT_ID
AND PM.PATTERN_ID = PD.PATTERN_ID
GROUP BY PM.PATTERN_NAME,PM.PATTERN_ID;
--Adding output in XML output parameter
ALL_RESULT_SET_XML:= XMLTYPE(ALL_RESULT_SET);
EXCEPTION
WHEN OTHERS
THEN
V_ERROR := SUBSTR(SQLERRM,1,1000);
P_MESSAGE_ALL := 'Error encountered: '||V_ERROR ;
END;

Compilation error in Oracle PL/SQL

I have been staring at this hours trying to figure out why it won't run, and cannot see any syntax error which should stop this from compiling, but I am nonetheless getting the following errors:
Error(6,1): PL/SQL: SQL Statement ignored
Error(22,7): PL/SQL: ORA-00933: SQL command not properly ended
The function I am trying to create is as follows:
create or replace function caps_get_Uoffer_count_BD(ayrc in varchar2
,mcrc in varchar2)
return number
is
app_count number;
begin
select count(*)
from
(select distinct cap.cap_stuc,cap.cap_apfs , cap.cap_seqn from
intuit.srs_cap cap
,intuit.srs_apf apf
,intuit.srs_ioi ioi
where cap.cap_ayrc = ayrc
and cap.cap_mcrc = mcrc
and apf.apf_stuc = cap.cap_stuc
and apf.apf_seqn = cap.cap_apfs
and apf.apf_recd <= to_date('1501'||substr(ayrc,1,4),'DDMMYYYY')
and cap.cap_stuc = ioi.ioi_stuc
and cap.cap_mcrc = ioi.ioi_mcrc
and ioi.ioi_iodc ='PAPERLESS'
and cap.cap_stac like 'A%'
and cap.cap_idrc in ('U','UF','UI','UD','CFU','CFUF','CIUI','CIUF','CIUD','CIU','CFUI','CFUD'))
return(app_count);
end;
Database is Oracle 11g. Can anyone help?
Many thanks in advance

Character string buffer too small error in Oracle Stored Procedure

I am getting an error in an Oracle 11g stored procedure. The error is...
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
It is happening at line 31, the line that contains out_cnt_tot := 0; I'm really not sure why there is anything wrong with that line. Another programmer created this procedure and I'm really not familiar with SQL procedures. Can anyone help me figure this out?
create or replace
PROCEDURE "FIP_BANKREC_PREP"
(
in_file_date in varchar2,
in_bank_code in varchar2,
out_cnt_apx_miss_no out integer,
out_cnt_prx_miss_no out integer,
out_cnt_apx_no_mtch out integer,
out_cnt_prx_no_mtch out integer,
out_cnt_ap_dup out integer,
out_cnt_pr_dup out integer,
out_cnt_bad out integer,
out_cnt_ap_load out integer,
out_cnt_pr_load out integer,
out_cnt_ap_not_load out integer,
out_cnt_pr_not_load out integer,
out_cnt_tot out integer,
out_message out varchar2
) as
file_date date;
ap_acct_no varchar2(16);
pr_acct_no varchar2(16);
-- ------------------------------------------------------
-- begin logic
-- ------------------------------------------------------
begin
file_date := to_date(in_file_date,'yyyymmdd');
out_cnt_tot := 0; --- THE ERROR IS ON THIS LINE ---
out_message := 'Test Message';
select brec_acct_code into ap_acct_no
from MSSU.zwkfi_bankrec_accts
where brec_acct_bank = in_bank_code
and brec_acct_type = 'AP';
select brec_acct_code into pr_acct_no
from MSSU.zwkfi_bankrec_accts
where brec_acct_bank = in_bank_code
and brec_acct_type = 'PR';
// The rest of the procedure...
Simple demo of the scenario mentioned in comments:
create or replace procedure p42(out_message out varchar2) as
begin
out_message := 'Test message';
end p42;
/
If I call that with a variable that is declared big enough, it's fine. I have a 12-char variable, so assigning a 12-char value is not a problem:
declare
msg varchar2(12);
begin
p42(msg);
end;
/
anonymous block completed
But if I make a mistake and make the caller's variable too small I get the error you're seeing:
declare
msg varchar2(10);
begin
p42(msg);
end;
/
Error report:
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at "STACKOVERFLOW.P42", line 3
ORA-06512: at line 4
06502. 00000 - "PL/SQL: numeric or value error%s"
*Cause:
*Action:
The error stack shows both the line in the procedure that errored (line 3), and the line in the caller that triggered it (line 4). Depending on where you're calling it you might not have the whole stack, of course.
You mentioned that there would be various error messagesin the future. You need to make sure that anything that ever calls this defines the variables to be big enough to cope with any of your messages. If they were stored in a table you could semi-automate that, otherwise it'll be a manual code review check.
OK, saw your c# comment after posting this. It looks like you're calling this constructor; that doesn't say what default size it gets, but not unreasonable to think it might be 1. So you need to call this constructor instead to specify the size explicitly:
OracleParameter(String, OracleType, Int32)
Initializes a new instance of the OracleParameter class that uses the parameter name,
data type, and length.
... something like:
OracleParameter prm15 = new OracleParameter("out_str_message",
OracleDbType.Varchar2, 80);
Unless there's a way to reset the size after creation, which I can't see. (Not something I've ever used!).