Skip to main content

Patient Information Retrieval πŸš€

Mentor's Note: In a hospital, doctors need patient data instantly. A slow or buggy system can be dangerous. This lab shows you how to build a safe, reliable lookup tool! πŸ’‘


🌟 The Scenario: The Doctor's Search πŸ©Ίβ€‹

Imagine you are a doctor at a clinic in Surat.

  • The Logic: A patient arrives and gives you their Patient ID. πŸ“¦
  • The Search: You enter the ID into the system. It looks in the Patient Master table. πŸ”
  • The Result:
    • Found: The system displays their Name, DOB, and Phone. βœ…
    • Not Found: The system shows a friendly "Record not found" instead of crashing. πŸ•ΈοΈ

πŸ“– Concept Explanation​

1. Using %TYPE πŸ·οΈβ€‹

Instead of guessing if a name is VARCHAR2(50) or (100), we use Table.Column%TYPE. This makes the code "Smart" because it automatically copies the correct size from the table.

2. Error Trapping​

We use the EXCEPTION block to catch NO_DATA_FOUND. This is the "Parachute" that ensures the program exits safely if an ID is wrong. πŸͺ‚


🎨 Visual Logic: The Lookup Flow​


πŸ’» Implementation: The Hospital Lab​

-- πŸ›’ Scenario: Looking up a patient's medical file
-- πŸš€ Action: Using SELECT INTO with exception handling

DECLARE
-- πŸ“¦ Smart variables using %TYPE
v_id Patientmaster.pat_id%TYPE;
v_name Patientmaster.pat_name%TYPE;
v_dob Patientmaster.DOB%TYPE;
v_phone Patientmaster.phoneno%TYPE;
BEGIN
-- πŸ“₯ 1. Accept ID from user
v_id := &Enter_Patient_ID;

-- πŸ” 2. Try to fetch details
SELECT pat_name, DOB, phoneno
INTO v_name, v_dob, v_phone
FROM Patientmaster
WHERE pat_id = v_id;

-- πŸ“€ 3. Show Result
DBMS_OUTPUT.PUT_LINE('--- Patient Profile ---');
DBMS_OUTPUT.PUT_LINE('Name: ' || v_name || ' πŸ‘€');
DBMS_OUTPUT.PUT_LINE('DOB: ' || TO_CHAR(v_dob, 'DD-Mon-YYYY'));
DBMS_OUTPUT.PUT_LINE('Contact: ' || v_phone || ' πŸ“ž');

EXCEPTION
-- πŸ•ΈοΈ 4. Safety Net
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('⚠️ Alert: Patient ID ' || v_id || ' does not exist.');
END;
/

πŸ“Š Sample Dry Run​

Goal: Find ID 501 (Not in table)

StepActionStatusResult
1Input 501Variable setWaiting...
2QuerySearch pat_id=501No rows found! ❌
3JumpGo to EXCEPTIONHandler reached πŸͺ‚
4PrintShow Alert"ID 501 does not exist"

πŸ“‰ Complexity Analysis​

  • Time Complexity: $O(1)$ - This is a Primary Key Search, which is the fastest way to find data in a database. ⚑

🎯 Practice Problems​

Easy Level πŸŸ’β€‹

  • Add a line to print the current system date (SYSDATE) at the top of the report.

Medium Level πŸŸ‘β€‹

  • If the patient is found, also display their Doctor's Name by joining with the Doctormaster table.

πŸ’‘ Interview Tip πŸ‘”β€‹

"Interviewers often ask: 'Can SELECT INTO return 0 rows?' Answer: No, if it returns 0 rows, it immediately stops and triggers the NO_DATA_FOUND exception. Your code must handle it!"


πŸ“ˆ Learning Path​


πŸ“ Visit Us

🏫 VD Computer Tuition Surat

VD Computer Tuition
πŸ“ Address
2/66 Faram Street, Rustompura
Surat – 395002, Gujarat, India
πŸ“ž Phone / WhatsApp
+91 84604 41384
🌐 Website

Computer Classes & Tuition β€” Areas We Serve in Surat

Adajanβ€’Althanβ€’Amroliβ€’Athwaβ€’Athwalinesβ€’Bhagalβ€’Bhatarβ€’Bhestanβ€’Canal Roadβ€’Chowkβ€’Citylightβ€’Dumasβ€’Gaurav Pathβ€’Ghod Dod Roadβ€’Haziraβ€’Jahangirpuraβ€’Kamrejβ€’Kapodraβ€’Katargamβ€’Limbayatβ€’Magdallaβ€’Majura Gateβ€’Mota Varachhaβ€’Nanpuraβ€’New Citylightβ€’Olpadβ€’Palβ€’Pandesaraβ€’Parle Pointβ€’Piplodβ€’Punaβ€’Randerβ€’Ring Roadβ€’Rustampuraβ€’Sachinβ€’Salabatpuraβ€’Sarthanaβ€’Sosyo Circleβ€’Udhnaβ€’Varachhaβ€’Ved Roadβ€’Vesuβ€’VIP Road
πŸ“ž Call SirπŸ’¬ WhatsApp Sir