Date today from a Display Item insert to database - oracle

I made a table that has a column Date_Created in it. And in my form I put a display item above having a formula of sysdate so that each time the form runs and insert new data, the date will always e updated but when I looked up into the database, Date_Created is blank!
What causes this problem? tnx
screen shots:
http://static.tumblr.com/ezdv8nl/YcUmu2vcc/untitled_1.jpg
My code for the submit button:
DECLARE
adminPass VarChar(20);
alert Number;
BEGIN
IF :USERS.PASS != :USERS.PASS2 OR :USERS.PASS IS NULL THEN
MESSAGE('Passwords did not match');
GO_ITEM('PASS2');
ELSIF :USERS.ACCESS_LEVEL = 'admin' THEN
SELECT password
INTO adminPass
FROM admin_pass_history
WHERE id = (SELECT MAX(id) FROM admin_pass_history);
IF :USERS.ADMIN_PASS IS NULL THEN
MESSAGE('Please enter the Administrator Password');
ELSIF :USERS.ADMIN_PASS != adminPass THEN
MESSAGE('Administrator Password did not match');
GO_ITEM('ADMIN_PASS');
ELSE
COMMIT;
alert:= show_alert('USER_CREATED');
IF alert = alert_button1 THEN
OPEN_FORM('C:\Documents and Settings\Richzer Cruz\Desktop\LOGIN.fmx');
END IF;
END IF;
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
MESSAGE('Administrator Password did not match');
GO_ITEM('ADMIN_PASS');
END;

The first thing is to make sure that your display item is a database item property set to Yes.

From what I remember items with formulas are not saved to database. Remove formula and use WHEN-CREATE_RECORD trigger.

Related

How to use dynamic alerts for login form?

I am new at Oracle development using oracle 10g Forms.
I am trying to write this code for login purpose. When user enters wrong password, it gives alert in popup window. Also, when user enters wrong username, it raise an alert.
I wrote this code but I am unable to correct it.
DECLARE
un VARCHAR (15);
pwd VARCHAR (15);
BEGIN
--- Dynamic altert properties code start--
SET_ALERT_PROPERTY ('LOGIN_ALERTS', TITLE, 'Security Altert');
SET_ALERT_PROPERTY ('LOGIN_ALERTS',
ALERT_MESSAGE_TEXT,
'Wrong Username?');
SET_ALERT_PROPERTY ('LOGIN_ALERTS',
ALERT_MESSAGE_TEXT,
'Wrong Password?');
SELECT username, users_password
INTO un, pwd
FROM MENU_USERS
WHERE username = :TXT_USERNAME;
IF un = :TXT_USERNAME
THEN
IF pwd = :TXT_PWD
THEN
CALL_FORM ('F:\ISMS\INV\inv_stock.fmx');
ELSE
SHOW_ALERT ('LOGIN_ALERTS');
END IF;
ELSE
SHOW_ALERT ('LOGIN_ALERTS');
END IF;
END;
Alerts are OK, but - consecutive message calls will do as well. This example shows how to use that approach.
Code you wrote is kind of "wrong" as it doesn't handle "wrong username" situation. When that happens, select won't return anything and will raise the no_data_found exception which should be handled.
Here you go:
declare
pwd varchar2(15);
begin
select users_password
into pwd
from menu_users
where username = :txt_username;
-- if TXT_USERNAME value exists, SELECT will return its password
if pwd = :txt_pwd then
call_form('whichever form you want');
else
message('Wrong password');
message('Wrong password');
raise form_trigger_failure;
end if;
exception
when no_data_found then
-- SELECT didn't return anything, but raised an exception
message('Wrong username');
message('Wrong username');
end;

Check ID Exist and delete record in PL SQL

What I have to do is, whenever a STORE_CODE is entered it will check in the db and delete the store code. SO for that, I have written a procedure which is as below
PROCEDURE DELETE_STORE_INFO
(
P_STORE_CODE IN NVARCHAR2
)
AS
BEGIN
UPDATE TBL_RRSOC_STORE_INFO set ISACTIVE = 'N' where STORE_CODE = P_STORE_CODE;
END DELETE_STORE_INFO;
But here what missing is
what if the user enter the wrong store_code and does the operation so what it will do. how to check with that part ?
I guess something for COUNT wont work at this stage. Kindly suggest
You want to use SQL%ROWCOUNT to find out how many rows were affected by the previous SQL statement:
PROCEDURE DELETE_STORE_INFO
(
P_STORE_CODE IN NVARCHAR2
)
AS
BEGIN
UPDATE TBL_RRSOC_STORE_INFO
SET ISACTIVE = 'N'
WHERE STORE_CODE = P_STORE_CODE;
IF SQL%ROWCOUNT = 0 THEN
-- DBMS_OUTPUT.PUT_LINE( 'Store code does not exist.' );
RAISE_APPLICATION_ERROR( -20000, 'Store code does not exist.' );
END IF;
END DELETE_STORE_INFO;
/
You can use DBMS_OUTPUT.PUT_LINE( string ) to output to the SQL console (if you are calling this from an external language like PHP or Java then you will not see the output and you may not see it in the console if you have SET SERVEROUTPUT OFF).
You could also use RAISE_APPLICATION_ERROR( error_code, error_message ) to raise an exception if something invalid happens.
Alternatively you could return a status in an OUT parameter:
PROCEDURE DELETE_STORE_INFO
(
P_STORE_CODE IN NVARCHAR2,
O_STATUS OUT NUMBER
)
AS
BEGIN
UPDATE TBL_RRSOC_STORE_INFO
SET ISACTIVE = 'N'
WHERE STORE_CODE = P_STORE_CODE;
IF SQL%ROWCOUNT = 0 THEN
o_status := 0;
ELSE
o_status := 1;
END IF;
END DELETE_STORE_INFO;
/
Generally contract for your procedure is: caller gives some store_code and procedure guarantee that there is no active store with such code. What if caller gives the wrong store_code? It means there is no such store, so contract is accomplished. You should do nothing, no more :)
But if you wish to check whether update found the record or not, you can add something like
if sql%notfound then
dbms_output.put_line('There is no such store!');
end if;
immediately after update statement.
Especially for MT: just check simple script
create table t$(id integer);
insert into t$ values(1);
set serveroutput on
begin
update t$ set id = 2 where id = 1;
if sql%notfound
then dbms_output.put_line('#1: not found');
else dbms_output.put_line('#1: found');
end if;
update t$ set id = 4 where id = 3;
if sql%found then
dbms_output.put_line('#2: found');
else dbms_output.put_line('#2: not found');
end if;
end;
/
drop table t$;
My results are
Connected to Oracle Database 12c Enterprise Edition Release 12.1.0.2.0
#1: found
#2: not found
PL/SQL procedure successfully completed
There are something missunderstand in your request: you said you want to delete it but in the code, you just updated the store as incative.
Here is the proceure with both situation. You choose the right one:
CREATE OR REPLACE PROCEDURE DELETE_STORE_INFO (P_STORE_CODE IN NVARCHAR2) AS
n_count number;
BEGIN
select count(1) INTO n_count from TBL_RRSOC_STORE_INFO where STORE_CODE = p_store_code;
if n_count > 0 then
UPDATE TBL_RRSOC_STORE_INFO set ISACTIVE = 'N' where STORE_CODE = P_STORE_CODE;
-- or for deletion
-- DELETE TBL_RRSOC_STORE_INFO set ISACTIVE = 'N' where STORE_CODE = P_STORE_CODE;
else
DBMS_OUTPUT.PUT_LINE('the required store was not found');
end if;
END DELETE_STORE_INFO;

Checking if every table object has a different name

I have a Users table, each User has a identifier, a name and some other fields.
I need to check if there is another user with the same name after inserting or updating, so I made this trigger:
CREATE OR REPLACE TRIGGER user_name
BEFORE
INSERT OR UPDATE
ON Users
FOR EACH ROW
DECLARE
count INTEGER;
BEGIN
SELECT COUNT(*) INTO count FROM Users WHERE name = :new.name AND idUser <> :new.idUser;
IF count > 0
THEN raise_application_error(-20007, 'There is already another user with the same name');
END IF;
END;
It seems to work when inserting new users, but it doesn't when I update them, it looks like it "ignores" the idUser check so it always fails as it finds the same user with the same name.
Why is this happening? Thank you
In triggers , there are 3 states as you know inserts update and delete , In update states there are new values and old values , I mean when you update a column you , you will replace the old value with new one , in trigger you can use the old and the new value of the column .. Please check this example
CREATE or REPLACE TRIGGER test001
AFTER INSERT OR UPDATE OR DELETE ON tabletest001
DECLARE
Operation NUMBER;
CustomerCode CHAR(10 BYTE);
BEGIN
IF INSERTING THEN
Operation := 1;
CustomerCode := :new.field1;
END IF;
IF UPDATING THEN
Operation := 2;
CustomerCode := :old.field1;
END IF;
// DO SOMETHING ...
EXCEPTION
WHEN OTHERS THEN ErrorCode := SQLCODE;
END;
/
in the above example , in update state I used old.value of the column , so in your example you should check the old value not new , please try it
if updating then
SELECT COUNT(*) INTO count FROM Users WHERE name = :old.name AND idUser <> :old.idUser;
IF count > 0
THEN raise_application_error(-20007, 'There is already another user with the same name');
end if
however as in the comments adviced you is to use primary key, however my answer is to give you understanding to triggers

Oracle PL/SQL function: if parameter not in table column => continue program

Please see my code below. If the parameter p_cust_id is not in the column cust_id I want "0" to be printed out (which means not logged in). If it is in cust_id I want oracle to continue the second part of the code (the part below the empty row).
I have tried to solve this by inserting the values of column cust_id in a cursor and then inserting it in the variable v_cust_id. Perhaps this results in unnecessarily much code?
My problem is that the program does not seem to run the second part even if p_cust_id is in cust_id. It just prints out "0" even though the customer ID and the password are correct.
Before I added the cursor the program worked as it was supposed to, unless the parameter p_cust_id didn't match any value in the cust_id column. If this was the case nothing was printed out.
create or replace function log_in(
p_cust_id in customer.cust_id%type,
p_passwd in customer.passwd%type)
return varchar2
as
cursor c_cust_id is select cust_id from customer;
v_cust_id customer.cust_id%type;
v_passwd customer.passwd%type;
v_logged_in number(1);
v_not_logged_in number(1);
begin
v_logged_in := 1;
v_not_logged_in := 0;
if not c_cust_id%isopen then
open c_cust_id;
end if;
loop
fetch c_cust_id
into v_cust_id;
exit when c_cust_id%notfound;
end loop;
if p_cust_id not in(v_cust_id) then
return v_not_logged_in;
end if;
close c_cust_id;
select passwd
into v_passwd
from customer
where cust_id = p_cust_id;
if v_passwd = p_passwd then
return v_logged_in;
else
return v_not_logged_in;
end if;
end;
I could see, you don't need a cursor at all, to check if the cust_id is in the table. Just search for the cust_id in the table, and attempt to fetch the password. If it exists, you get the value, and NO_DATA_FOUND exception otherwise which means not logged in.
BEGIN
select passwd
into v_passwd
from customer
where cust_id = p_cust_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
return v_not_logged_in;
END;
Full code will be:
create or replace function log_in(
p_cust_id in customer.cust_id%type,
p_passwd in customer.passwd%type)
return varchar2
as
v_cust_id customer.cust_id%type;
v_passwd customer.passwd%type;
v_logged_in number(1);
v_not_logged_in number(1);
begin
v_logged_in := 1;
v_not_logged_in := 0;
BEGIN
select passwd
into v_passwd
from customer
where cust_id = p_cust_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
return v_not_logged_in;
END;
if v_passwd = p_passwd then
return v_logged_in;
else
return v_not_logged_in;
end if;
end;

AFTER ALTER ON DATABASE gives the old password value in Oracle

I am writing a trigger to fetch the 'password' value when 'sys.users$' table gets updated.
CREATE OR REPLACE
TRIGGER Sys_User_Nm_Trg AFTER ALTER ON DATABASE
WHEN (ora_dict_obj_type = 'USER')
DECLARE
CURSOR get_pw IS
SELECT password
FROM sys.user$
WHERE name = ora_dict_obj_name;
user_pw_ VARCHAR2(100);
BEGIN
IF (ora_dict_obj_name != 'SYS' AND ora_dict_obj_name != 'SYSTEM') THEN
IF (ora_des_encrypted_password IS NOT NULL) THEN
OPEN get_pw;
FETCH get_pw INTO user_pw_;
CLOSE get_pw;
END IF;
END IF;
END;
/
But this cursor gives the old 'password' not the changed 'password' value. But if I run
SELECT password
FROM sys.user$
WHERE name = ora_dict_obj_name;
in a separate transaction it gives the correct changed value. Is there something that I should change in the trigger to fetch the updated value for the password?

Resources