Consolidated output from a LOOP [message #642407] |
Thu, 10 September 2015 01:05 |
muktha_22
Messages: 527 Registered: December 2009
|
Senior Member |
|
|
Hi All,
We have a procedure to send out alert message on the password expiry for the apps users.
Where on 15 Days before the expiry date, all the managers, team leads are getting alert messages against every users in the loop, where the same manager getting multiple mails saying the same statement.
Now we want to sent a single consolidated mail to the managers and team leads against their apps users.
Kindly help me out.
USERNAME | Days | EXPIRY_DATE | CC_EMAIL_ID_1 | TL_EMAIL_ID
-------- | ---- | ------------ | ------------ | -----------------
ABC | 15 | 03/07/2015 | TEAM_LEAD_ABC@yahoo.com | MGR_ABC@yahoo.com
CDE | 15 | 03/07/2015 | | MGR_CDE@yahoo.com
FGH | 15 | 03/07/2015 | TEAM_LEAD_ABC@yahoo.com | MGR_ABC@yahoo.com
I just want to stop mailing in a loop.
Regards
Muktha
|
|
|
|
|
|
Re: Consolidated output from a LOOP [message #642413 is a reply to message #642411] |
Thu, 10 September 2015 03:09 |
muktha_22
Messages: 527 Registered: December 2009
|
Senior Member |
|
|
Hi,
Here is the code I am using currently.
CREATE OR replace PROCEDURE alert_password
IS
var1 VARCHAR2(50);
var2 NUMBER;
var3 VARCHAR2(50);
var5 VARCHAR2(50);
var4 DATE;
var6 VARCHAR2(50);
var7 VARCHAR2(50);
var8 VARCHAR2(50);
CURSOR c1 IS
SELECT a.username,
Trunc((((86400*(a.expiry_date-SYSDATE))/60)/60)/24) "Days",
a.expiry_date,
b.cc_email_id_1,
b.tl_email_id
FROM dba_users a,
password_alert b
WHERE a.username=b.schema_name
ORDER BY b.schema_name DESC;
BEGIN
OPEN c1;
LOOP
FETCH c1
INTO var1,
var2,
var4,
var8,
var7;
BEGIN
SELECT server
INTO var6
FROM password_alert
WHERE schema_name=var1;
EXCEPTION
WHEN OTHERS THEN
var6:='XYZ';
END;
IF (var7 IS NOT NULL
AND
var8 IS NULL)
AND
(
var2 =20
OR
var2 = 15
)
THEN
SELECT tl_email_id
INTO var3
FROM password_alert
WHERE Upper(schema_name)=var1;
Password_alert_Proc_2(var3,'abc@yahoo.com','password change','Server of'
||var6
|| '(LIVE server)- s user '
|| var1
||' gets '
||' expired on '
||var4);
ELSIF (var8 IS NOT NULL
AND
var7 IS NOT NULL)
AND
(
var2 =20
OR
var2 = 15
)
THEN
SELECT tl_email_id,
cc_email_id_1
INTO var3,
var5
FROM password_alert
WHERE upper(schema_name)=var1;
Password_alert_Proc_3 (var3,xyz@gmail.com',var5, 'password change ', 'Server of '
||var6
|| '(LIVE server)- s user '
|| var1
||' gets '
||' expired on '
||var4);
ELSE
NULL;
END IF;
EXIT
WHEN c1 %NOTFOUND;
END LOOP;
CLOSE c1;
END;
Here is the table structure "password_alert" used in the procedure.
Regards
Muktha
|
|
|
|
Re: Consolidated output from a LOOP [message #642415 is a reply to message #642414] |
Thu, 10 September 2015 05:45 |
cookiemonster
Messages: 13920 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
Some general points on the code.
1) calling variables var1-var8 is one of the shoddiest/laziest coding practices known. It means you keep having to refer back to the bit of code that sets them to work out what each one actually is. So rename them to something sensible now.
2) Likewise variables should be typed to the column that is used to populate them with %TYPE. What you've done there is again lazy and very bad practice.
3) Exception when others should very rarely be used. It certainly should never be used against a single select. If you're worried about no_data_found then handle that explicitly.
4) Of course in this case worrying about no_data_found, and having the select intos at all is just silly. Each of the three selects is looking for the same row in password_alert that the explict cursor has already found. So the explicit cursor should be getting all the data.
5) This is silly too:
Trunc((((86400*(a.expiry_date-SYSDATE))/60)/60)/24)
I have no idea why you thought you needed all that math. What do you think 86400/60/60/24 equals?
6) And finally if you're going to write a structure that replicates a FOR LOOP you might as well just use a FOR LOOP. It's less code and would have avoided problems 1 and 2.
Some other notes.
You've been here more than long enough to know we want tables as create table and insert statements not a screen shot from excel. Plus judging from the query it's not an example of the table but rather an example of the output from the explicit cursor.
The actual table structure is foolish. It means you're going to have to enter the team leaders email against every user they manage.
Each team leaders email should exist once - you need two tables here - one with the team leaders details and one to link a team leader to a user.
|
|
|
Re: Consolidated output from a LOOP [message #642418 is a reply to message #642415] |
Thu, 10 September 2015 06:00 |
muktha_22
Messages: 527 Registered: December 2009
|
Senior Member |
|
|
Hi All,
Thanks for the help.
1) This procedure is already existing before I Join.
2) I am restricted to use the original table data and etc.
3) I thought, while looking at the table structure, some one can guide me (May be I am wrong)
I will try recoding it.
Thanks and Regards
Muktha
|
|
|
|
Re: Consolidated output from a LOOP [message #642445 is a reply to message #642420] |
Fri, 11 September 2015 00:21 |
muktha_22
Messages: 527 Registered: December 2009
|
Senior Member |
|
|
Hi Cookiemonster,
I have already given you the table structure (Table name is "password_alert").
Where I am combining DBA_USERS view and the TABLE of "password_alert".
The actual query is below:
SELECT a.username,
Trunc((((86400*(a.expiry_date-SYSDATE))/60)/60)/24) "Days",
a.expiry_date,
b.cc_email_id_1,
b.tl_email_id
FROM dba_users a,
password_alert b
WHERE a.username=b.schema_name
ORDER BY b.schema_name DESC;
Regards
Muktha
|
|
|
|
Re: Consolidated output from a LOOP [message #642453 is a reply to message #642445] |
Fri, 11 September 2015 02:49 |
cookiemonster
Messages: 13920 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
muktha_22 wrote on Fri, 11 September 2015 06:21Hi Cookiemonster,
I have already given you the table structure (Table name is "password_alert").
Of course you haven't.
You've given us a screen-shot of an excel table that contains the result of a query that joins password_alert to dba_users.
Can you not understand the difference?
|
|
|