delete query using execute Immediate - sql

I want convert varchar2 into date but its occur error give me solution
DECLARE
ADATE VARCHAR2(100):='02/20/1981';
BEGIN
EXECUTE IMMEDIATE 'DELETE FROM emp WHERE HIREDATE= '||to_char(to_date(ADATE,'mm/dd/yy'),'mm/dd/yy');
DBMS_OUTPUT.PUT_LINE('DELETE');
END;

You can also use this one:
DECLARE
ADATE VARCHAR2 (100) := '02/20/1981';
BEGIN
EXECUTE IMMEDIATE
'DELETE FROM emp WHERE HIREDATE= :aDate' using TO_DATE(ADATE, 'mm/dd/yyyy');
DBMS_OUTPUT.PUT_LINE ('DELETE');
COMMIT;
END;

You can use this. Make sure to use USING clause to pass the bind variable to avoid SQL injection. Also as mentioned by #Kaushik, there is no need to do TO_CHAR.
DECLARE
ADATE VARCHAR2 (100) := '02/20/1981';
BEGIN
EXECUTE IMMEDIATE
'DELETE FROM emp WHERE HIREDATE= TO_DATE(:ADATE, ''mm/dd/yyyy'')' using adate;
DBMS_OUTPUT.PUT_LINE ('DELETE');
COMMIT;
END;

You don't need to convert it to TO_CHAR. The quotes have to be right and use proper date formats.
DECLARE
ADATE VARCHAR2(100):='02/20/1981';
BEGIN
EXECUTE IMMEDIATE 'DELETE FROM emp WHERE HIREDATE=to_date('''||ADATE||''',''mm/dd/yyyy'')';
DBMS_OUTPUT.PUT_LINE('DELETE');
END;
For your delete operation, you don't even need an EXECUTE IMMEDIATE. A block like this should be sufficient.
DECLARE
ADATE VARCHAR2(100):='02/20/1981';
BEGIN
DELETE FROM emp WHERE HIREDATE=to_date(ADATE,'mm/dd/yyyy');
DBMS_OUTPUT.PUT_LINE('DELETE');
END;

Related

How to insert old date into a table correctly?

I am trying to insert an old date (01/01/1888) into a table within pl/sql code, but I only got value (01/01/1988) inserted.
What will be the correct way to insert this old date into a table?
declare
p_date date;
p_eventDate date;
sql_str varchar2(2000);
begin
select to_date('01-01-1888','dd-mm-yyyy') into p_eventDate from dual;
p_date := add_months(sysdate,-60);
if( p_eventDate < p_date)
then
sql_str := 'insert into test_date values (''' ||p_eventDate ||''')';
sql_str := 'insert into test_date values (to_date(''' ||p_eventDate ||''',''dd-mm-yyyy''))';
else
sql_str := 'insert into test_date values (''' ||p_date ||''')';
end if;
EXECUTE IMMEDIATE sql_str;
commit;
--dbms_output.put_line(sql_str);
end;
When I check the table test_date, value 01/01/1988 and 01/01/0088 inserted.
Thanks!
I think you code can be simplified.
If I understand well, you need to insert the minimum value from two variables; if so, you don't need dynamic SQL:
declare
p_date date;
p_eventDate date;
begin
select to_date('01-01-1888','dd-mm-yyyy') into p_eventDate from dual;
p_date := add_months(sysdate,-60);
insert into test_date(d) values ( least(p_date, p_eventDate));
commit;
end;
Keeping aside the dynamic SQL, the main issue in your code is that you are using dates in strings without casting them, thus relying on implicit conversions.
If, for some reason, you want to use dynamic SQL, a good way could be with bind variables, with something like:
...
sql_str := 'insert into test_date values (:1)';
execute immediate sql_str using someVariable;
...
If you want to keep the structure of your code, your SQL string should be:
sql_str := 'insert into test_date values ( to_date(''' || to_char(p_date, 'dd-mm-yyyy') || ''', ''dd-mm-yyyy''))';
that is, you first have to cast the date variable to string, use it to concatenate your statement and then, in within your statement, cast back the string to a date type.

Set an out parameter value from a pl/sql block stored in a clob column

I have the following table and relevant column..
TABLEA
SQL_SCRIPT CLOB
Here is a procedure that executes the pl/sql block in the clob..
Create procedure (p_exit_code IN OUT NUMBER)
AS
V_sql_val sql_script%TYPE;
…
BEGIN
Select sql_script into v_sql_val from tablea;
EXECUTE IMMEDIATE sql_script;
END;
In SQL_SCRIPT I am trying to do following in the exception handler:
EXCEPTION
WHEN EXCP_STOP_PROCESS THEN
p_exit_code := 1;
END;
I need to set the value of p_exit_code parameter in procedure from the clob script. How can I do this?
If I understand correctly, you need to set the value of a variable within sql_script and return this to the calling procedure. This can be achieved using bind variables. A very simple test case is below:
declare
-- ':return_code' is the bind variable
sql_script varchar2(1000) := 'begin :return_code := 1; end;';
vn_result number;
begin
execute immediate sql_script using out vn_result;
dbms_output.put_line(vn_result);
end;

How to do "Insert into... Select... in a PL/SQL block (IF/THEN)

I'm trying to run the script below in PL/SQL Developer, and I got an error says 'V_INSERT' is not a procedure or is undefined, and that statement is ignored.
Anyone can help? Thanks!
DECLARE chktime date; v_trunc varchar2(200); v_insert varchar2(200);
BEGIN
Select trunc(max(a.action_timestamp)) into chktime from hcr_dm.hcr_dm_fact a;
If chktime <> trunc(sysdate) then
v_trunc:='truncate table yxg3509.account_crosswalk_hcrdm';
execute immediate v_trunc;
Else
v_trunc:='truncate table yxg3509.product_to_ah_092514';
v_insert:='insert into yxg3509.product_to_ah_092514
select prod.oracle_prod_code,
prod.oracle_prod_description,
prod.ah_code,
prod.effective_date
from hcr_sandbox.product_to_ah prod';
execute immediate v_trunc; v_insert;
END IF;
END;
You'll need to use two EXECUTE IMMEDIATE statements to do this:
DECLARE
chktime date;
v_trunc varchar2(2000);
v_insert varchar2(2000);
BEGIN
Select trunc(max(a.action_timestamp)) into chktime from hcr_dm.hcr_dm_fact a;
If chktime <> trunc(sysdate) then
v_trunc:='truncate table yxg3509.account_crosswalk_hcrdm';
execute immediate v_trunc;
Else
v_trunc:='truncate table yxg3509.product_to_ah_092514';
v_insert:='insert into yxg3509.product_to_ah_092514
select prod.oracle_prod_code,
prod.oracle_prod_description,
prod.ah_code,
prod.effective_date
from hcr_sandbox.product_to_ah prod';
execute immediate v_trunc;
execute immediate v_insert;
END IF;
END;
although IMO there's no reason to use an EXECUTE IMMEDIATE for the INSERT statement, nor can I see a good reason to put the TRUNCATE TABLE statements into a variable, and thus you might be better off with:
DECLARE
chktime date;
BEGIN
Select trunc(max(a.action_timestamp))
into chktime
from hcr_dm.hcr_dm_fact a;
If chktime <> trunc(sysdate) then
execute immediate 'truncate table yxg3509.account_crosswalk_hcrdm';
Else
execute immediate 'truncate table yxg3509.product_to_ah_092514';
insert into yxg3509.product_to_ah_092514
select prod.oracle_prod_code,
prod.oracle_prod_description,
prod.ah_code,
prod.effective_date
from hcr_sandbox.product_to_ah prod;
END IF;
END;
Share and enjoy.

Oracle function dynamic column

I have a function, and I want to determine the name of the column in run time. For this I am passing one variable as an argument, like column_name.
Below is the code with the function:
l_column_name as varchar2(100)
Begin
If(column_name='emp_name')
Then
l_column_name:=EMPLOYEE.EMP_NAME
End If;
begin
select l_column_name from employee
end;
In above code, l_column_name:=EMPLOYEE.EMP_NAME is giving the error
Not allowed in this context.
Any help is much appreicated.
Regards,
Chaitu
As the error says, you can not do this.
You need to look into using the PL/SQL Execute Immediate: http://docs.oracle.com/cd/B14117_01/appdev.101/b10807/13_elems017.htm
declare
l_column_name as varchar2(100);
l_column_results as VARCHAR2(100);
begin
if (column_name = 'emp_name') then
l_column_name := 'EMPLOYEE.EMP_NAME';
end if;
query := 'SELECT ' || l_column_name || ' from employeee';
EXECUTE IMMEDIATE query INTO l_column_results;
end;

pl/sql - to_date not working with execute immediate parameter

i wanna be able to execute my below proc like so:
exec procname('29-JAN-2011');
proc code is:
PROCEDURE procname(pardate VARCHAR2) IS
vardate DATE := to_date(pardate, 'DD-MON-YYYY');
SQLS VARCHAR2(4000);
BEGIN
SQLS := 'SELECT cola, colb
FROM tablea
WHERE TRUNC(coldate) = TRUNC(TO_DATE('''||pardate||''',''DD/MON/YYYY''))';
EXECUTE IMMEDIATE SQLS;
END;
It keeps throwing error:
ORA-00904: "JAN": invalid identifier.
It compiles, but it throws the error when I run this command:
EXEC procname('29-JAN-2011');
You declare a variable which casts the input parameter to a date: why not use it?
Also, the TRUNC() applied to a date removes the time element. You don't need it here because the value you're passing has no time.
So, your code should be:
PROCEDURE procname(pardate VARCHAR2) IS
vardate DATE := to_date(pardate, 'DD-MON-YYYY');
SQLS VARCHAR2(4000) := 'select cola, colb FROM tablea
WHERE TRUNC(coldate) = :1';
l_a tablea.cola%type;
l_b tablea.colb%type;
BEGIN
EXECUTE IMMEDIATE SQLS
into l_a, l_b
using vardate;
END;
Specifying the dynamic SQL statement with a bind variable and executing it with the USING syntax is a lot more efficient. Note that we still have to SELECT into some variables.
You're using two different notations in the two calls to to_date. I think one of them (the second) is wrong.