i need a pl sql procedure to reset password - plsql

i need a pl sql code to reset old password,i done with password lock 3 attempts,after that i have to write the code to reset the password,here is the code which executes password 3 attempt lock,i need reset the password code
Create or replace
FUNCTION x_pwd(
p_user IN VARCHAR2,
p_password IN VARCHAR2)
RETURN VARCHAR2
IS
l_error NUMBER;
l_count NUMBER;
BEGIN
SELECT error
INTO l_error
FROM credentials
WHERE username = p_user;
IF(l_error >= 3) THEN
RETURN 'Authentication failed. Reached incorrect number of attempts';
ELSE
SELECT COUNT(id)
INTO l_count
FROM credentials
WHERE username = p_user
AND password = p_password;
IF l_count > 0 THEN
UPDATE credentials SET error = 0 WHERE username = p_user;
RETURN 'LOGIN SUCCESSFUL';
ELSE
UPDATE credentials SET error = l_error+1 WHERE username = p_user;
RETURN 'LOGIN FAILED';
END IF;
END IF;
COMMIT;
END;

Check the following PLSQL code to reset the password:
BEGIN
UPDATE credentials
SET password = p_password -- use as parameter in procedure
WHERE username = p_user;
-- or you can include "and id = p_id" in where clause
IF SQL%ROWCOUNT > 0
THEN
COMMIT;
RETURN 'Password Reset Successful.';
ELSE
RETURN 'Password Reset Failed. Invalid User Name';
END IF;
END;

Related

create a pl/sql procedure to change the staus of the video database table

My question
Create a procedure called video_return to change the rental status for that returned copy. When the copy is successfully checked in,
you need to update the corresponding rows (records) in the VIDEO_RENTAL_RECORD and VIDEO_COPY tables.
The following are the special cases:
The value
of p_video_copy_id does not exist in the corresponding column of
the VIDEO_COPY table.
The status of that copy is not “R” (STATUS !=
'R').
The value of p_video_return_date is greater than the current date.
I have written the code but I am not sure if this code is correct or not. Please, can anyone help me out with the code? Thank you so much.
CREATE OR REPLACE PROCEDURE video_return(
p_video_copy_id NUMBER,
p_video_return_date DATE
) AS
video_copy_id number;
video_return_date date;
v_count number;
begin
select count(*) into v_count
from video
WHERE video_copy_id = p_video_copy_id;
if p_video_copy_id != video_copy_id then
Dbms_Output.PUT_LINE('Video number ' || p_video_copy_id || ' not found.');
end if;
if p_video_copy_id = video_copy_id then
update video
set p_status = 'IN'
where p_video_copy_id = video_copy_id;
IF p_video_return_date > SYSDATE THEN
update video_rental_record
set p_video_return_date = SYSDATE
where p_video_copy_id = video_copy_id
AND p_video_return_date IS NULL;
Dbms_Output.PUT_LINE('Video successfully returned and available for rental.');
END IF;
END IF;
END IF ;
END;
CREATE OR REPLACE PROCEDURE video_return
(
p_video_copy_id NUMBER,
p_video_return_date DATE
)
as
video_copy_id number;
video_return_date date;
v_count number;
begin
select count(*) into v_count from video
WHERE video_copy_id = p_video_copy_id;
if p_video_copy_id != video_copy_id then
Dbms_Output.PUT_LINE('Video number ' || p_video_copy_id || ' not found.');
end if;
if p_video_copy_id = video_copy_id then
update video set p_status = 'IN'
where p_video_copy_id = video_copy_id;
end if; -- add end if here
IF p_video_return_date > SYSDATE THEN
update video_rental_record set p_video_return_date = SYSDATE
where p_video_copy_id = video_copy_id AND p_video_return_date IS NULL;
Dbms_Output.PUT_LINE('Video successfully returned and available for rental.');
END IF;
--END IF;
--END IF ;
END;
/
There are lot of things conceptually missing here in your code. You have not initialized "video_copy_id" variable so it will be always NULL and NULL cant be compared with anything. So basically you either need to initialize the variable so change the conditions. Hope below snippet helps.
CREATE OR REPLACE PROCEDURE video_return(
p_video_copy_id NUMBER,
p_video_return_date DATE )
AS
video_copy_id NUMBER; --Not initialized always NULL
video_return_date DATE;
v_count NUMBER;
BEGIN
SELECT COUNT(*) INTO v_count FROM video WHERE video_copy_id = p_video_copy_id;
IF p_video_copy_id != nvl(video_copy_id,'some value') THEN --Cant compare anything with null
Dbms_Output.PUT_LINE('Video number ' || p_video_copy_id || ' not found.');
END IF;
IF p_video_copy_id = video_copy_id THEN
UPDATE video SET p_status = 'IN' WHERE p_video_copy_id = nvl(video_copy_id,'some value'); --Never satisfied
IF p_video_return_date > SYSDATE THEN
UPDATE video_rental_record
SET p_video_return_date = SYSDATE
WHERE p_video_copy_id = nvl(video_copy_id,'some value') --Never satisfied
AND p_video_return_date IS NULL;
Dbms_Output.PUT_LINE('Video successfully returned and available for rental.');
END IF;
END IF;
END;

How do packages work?

I've created a package with two procedures, one of which is to create a user. However when I execute the procedure although successful, I'm not seeing the user created..
create or replace PACKAGE APPLICATIONPACKAGE AS
procedure CreateSingleUser( userName in VARCHAR2, rolename in VARCHAR2);
PROCEDURE DeleteUser(userName in varchar2);
END APPLICATIONPACKAGE;
/
create or replace PACKAGE BODY
APPLICATIONPACKAGE AS
procedure CreateSingleUser( userName in VARCHAR2, rolename in VARCHAR2)
AS
dynamicsql VARCHAR2 (2000);
BEGIN
BEGIN
if rolename='APPADMIN' OR rolename='APPMODIFIER' or rolename='APPVIEWER'
THEN --limit user input to specific roles
----assign dynamic sql to a variable for excecution by excecute immediate
dynamicsql :='CREATE USER ' || userName|| ' IDENTIFIED BY pass1234 DEFAULT
TABLESPACE USERS QUOTA 1M ON USERS PASSWORD EXPIRE';
DBMS_OUTPUT.PUT_LINE (dynamicsql);
EXECUTE IMMEDIATE dynamicsql;
EXECUTE IMMEDIATE 'GRANT ' || rolename || ' TO ' || userName || '';
EXECUTE IMMEDIATE 'GRANT CREATE SESSION TO ' || userName;
else
DBMS_OUTPUT.PUT_LINE('Not a valid role...sorry role specified not given to
a user');
END IF;
END;
END CreateSingleUser;
PROCEDURE DeleteUser(userName in varchar2)
AS
deleteSql VARCHAR2 (500);
BEGIN
deleteSql :='DROP USER ' || userName; -- assign drop user command to
variable delete sql
DBMS_OUTPUT.PUT_LINE (deleteSql);
EXECUTE IMMEDIATE deleteSql; -- excecute the sql dynamically;
END DeleteUser;
----- end procedure delete user-------
END APPLICATIONPACKAGE;

What to do? My user defined input is not allowing me to input a value

I wrote a procedure that is supposed to allow the input of a username and password and if successful it should proc a timestamp of the login. Problem is is that the code seems to skip the &username altogether and moves on to the next user defined entry which is the &pass in the code. This is my code: ECHO IS ON AND SO IS SERVEROUTPUT BTW
create or replace procedure LOGIN_CK_PF
as
fname bb_shopper.firstname%type;
lname bb_shopper.lastname%type;
uname bb_shopper.username%type;
pass bb_shopper.password%type;
begin
select firstname, lastname, username, password
into fname, lname, uname, pass from bbshop
where username=&username;
if pass = &pass then
dbms_output.put_line(Sysdate);
else
dbms_output.put_line('Invalid Login');
end if;
exception
when no_data_found then
dbms_output.put_line('Invalid Login');
end;
/
this is what the code output looks like during the entry of values:
Enter value for username:
old 11: select firstname, lastname, username,
password into fname, lname, uname, pass
from bbshop
new 11: select firstname, lastname, username,
password into fname, lname, uname, pass
from bbshop
it looks like its taking the entire select statement as an entry??? ERRORS FOR THE PARTICULAR LINE IS BELOW:
11/2 PL/SQL: SQL Statement ignored
11/107 PL/SQL: ORA-00936: missing expression
You need to use proper PL/SQL parameters to pass in to your procedure.
CREATE OR REPLACE PROCEDURE LOGIN_CK_PF (P_User IN VARCHAR2,
P_Pass IN VARCHAR2)
AS
fname bb_shopper.firstname%TYPE;
lname bb_shopper.lastname%TYPE;
uname bb_shopper.username%TYPE;
pass bb_shopper.password%TYPE;
BEGIN
SELECT firstname, lastname, username, password
INTO fname, lname, uname, pass
FROM bbshop
WHERE username = P_User;
IF pass = P_Pass THEN
DBMS_OUTPUT.put_line (SYSDATE);
ELSE
DBMS_OUTPUT.put_line ('Invalid Login');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.put_line ('Invalid Login');
END;
/

How to check if username exists

How to return 1 if username exists and 0 in other case? Here is what I've tried.
IF EXISTS (SELECT * FROM [User] WHERE UserName = #UserName and EmailID=#EmailID)
begin
return 1;
end
else
begin
declare #CreatedOn datetime
select #CreatedOn = getdate()
insert into [User](UserName,Password,EmailID,ContactNo,CreatedOn) values(#UserName,#Password,#EmailID,#ContactNo,#CreatedOn)
CREATE PROCEDURE CheckUserExists
AS
#UserName NVARCHAR(MAX),
#Email NVARCHAR(MAX)
BEGIN
IF EXISTS (SELECT 1 FROM [User] WHERE UserName = #UserName OR EmailID=#EmailID)
BEGIN
RETURN 1
END
ELSE BEGIN
RETURN 0
END
END
Maybe you could try this query:
Select count(*) from [user] where UserName = #UserName and EmailID=#EmailID
and if user name exists count(*) will be larger then 0
I see a bigger problem here. you stated you want to know if the user exists or not. but the query shows a side affect, that if the user doesn't exist you will create the user in the same call. these should be separate sqlcommands. one for the query/reading and another for the command/insert.
to determine if the user exists a simple select 1 from [users] where [username] = #user should suffice. if any (1?) rows are returned the user exists, otherwise the user does not exist.
you can then issue the command to create the user
insert into [User](UserName,Password,EmailID,ContactNo,CreatedOn) values(#UserName,#Password,#EmailID,#ContactNo,getdate())
Try this:
IF EXISTS (SELECT 1 FROM [User] WHERE UserName = #UserName and EmailID=#EmailID)
begin
return 1;
end
else
return 0;
/* Execute the SP As*/
DECLARE #return_value int
EXEC #return_value = [dbo].[CheckUserExists]
Select #return_value
/*STORED PROCEDURE*/
CREATE PROCEDURE CheckUserExists
AS
#UserName NVARCHAR(MAX),
#Password NVARCHAR(MAX)
#EmailID NVARCHAR(MAX),
#ContactNo NVARCHAR(MAX)
BEGIN
IF EXISTS (SELECT 1 FROM [User] WHERE UserName = #UserName and EmailID=#EmailID)
BEGIN
RETURN 1;
END
ELSE
BEGIN
DECLARE #CreatedOn DATETIME
SELECT #CreatedOn = GETDATE()
INSERT INTO [User](UserName,Password,EmailID,ContactNo,CreatedOn)
VALUES(#UserName,#Password,#EmailID,#ContactNo,#CreatedOn)
RETURN 0;
END
CREATE PROCEDURE CheckUserExists
AS
#UserName NVARCHAR(MAX),
#Email NVARCHAR(MAX)
BEGIN
declare #usercount int
Select #usercount = count(UserName) from [user] where UserName = #UserName and EmailID=#EmailID
IF (#usercount = 1)
BEGIN
RETURN 1
END
ELSE BEGIN
RETURN 0
END
END

PL/SQL DDL Execute Immediate

ACCEPT p_username PROMPT 'Enter Username : '
ACCEPT p_password PROMPT 'Enter New Password for Username : '
VARIABLE g_output VARCHAR2(4000)
DECLARE
CURSOR NAME IS SELECT TABLE_NAME FROM DBA_TABLES
WHERE OWNER LIKE '%&p_username%';
DDL_DROP VARCHAR2(200);
BEGIN
FOR TNAME IN NAME
LOOP
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE' || ' ' || TNAME.TABLE_NAME;
:g_output := :g_output || ' ' || TNAME.TABLE_NAME;
END;
END LOOP;
END;
/
PRINT g_output
Hello, I'm new to PL/SQL and trying to make a script to drop the user's table and ultimately change their password later after dropping their tables. I am having difficulty with the EXECUTE IMMEDIATE command. The script works if I remove the EXECUTE IMMEDIATE line. I tested it by printing the table names inside the loop and I get the right # of tables and their corresponding names.
Any help is appreciated, thanks.
Edited the code to reflect the suggestion but still didn't work. Getting the same error.
ACCEPT p_username PROMPT 'Enter Username : '
ACCEPT p_password PROMPT 'Enter New Password for Username : '
VARIABLE g_output VARCHAR2(4000)
DECLARE
NAME SYS_REFCURSOR;
DDL_WORD VARCHAR2(200);
BEGIN
OPEN NAME FOR SELECT TABLE_NAME FROM DBA_TABLES
WHERE OWNER LIKE '%&p_username%';
LOOP
FETCH NAME INTO DDL_WORD;
EXIT WHEN NAME%NOTFOUND;
EXECUTE IMMEDIATE 'DROP TABLE "' || DDL_WORD || '" CASCADE CONSTRAINTS';
:g_output := :g_output || ' ' || DDL_WORD;
END LOOP;
CLOSE NAME;
END;
/
PRINT g_output
You probably need to specify the owner for the table in the DROP statement:
ACCEPT p_username PROMPT 'Enter Username : '
ACCEPT p_password PROMPT 'Enter New Password for Username : '
VARIABLE g_output VARCHAR2(4000)
DECLARE
CURSOR NAME IS SELECT OWNER, TABLE_NAME FROM DBA_TABLES
WHERE OWNER LIKE '%&p_username%';
DDL_DROP VARCHAR2(200);
BEGIN
FOR TNAME IN NAME
LOOP
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE' || ' ' || TNAME.OWNER || '.' || TNAME.TABLE_NAME;
:g_output := :g_output || ' ' || TNAME.OWNER || '.' || TNAME.TABLE_NAME;
END;
END LOOP;
END;
/
PRINT g_output
The code looks fine.
You could try with () like this
BEGIN
EXECUTE IMMEDIATE (code_text);
END;
You could try
c SYS_REFCURSOR;
BEGIN
OPEN c FOR 'SELECT * FROM table';
CLOSE c;
END;

Resources