Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8752759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:17:52+00:00 2026-06-13T13:17:52+00:00

I’m playing around with MySQL stored procedures and I need a little help wrapping

  • 0

I’m playing around with MySQL stored procedures and I need a little help wrapping my head around some things. Below I’m attempting to;

1) Check if the student_id exist in the database and if it does then display “alumni already exist”
2) Check to see if the department and degree parameter entered don’t exist and if it doesn’t, then display “_ does not exist” (side note : these two columns are foreign keys)

Right now, my IF statement doesn’t work and throws arbitrary errors. (ex. student_id doesn’t exist in table but the error “Alumni Exist Already” is thrown, this is one of many)

I’d like to know what I’m doing wrong. Also, If the way I’m approaching this makes sense and if it doesn’t, what’s a more pragmatic way of going about this?

Thanks

DELIMITER //
DROP PROCEDURE IF EXISTS sp_add_alumni//

CREATE PROCEDURE sp_add_alumni (
IN  student_id     INT(20),
IN     first_name     VARCHAR(255),
IN     last_name      VARCHAR(255),
IN     street         VARCHAR(255),
IN     city           VARCHAR(255),
IN     state          VARCHAR(2),
IN     zip_code       VARCHAR(15),
IN     email          VARCHAR(255),
IN     telephone      VARCHAR(22),
IN     degree         VARCHAR(255),
IN     department     VARCHAR(255)
)
BEGIN
     DECLARE studentID INT(20);
     DECLARE departmentVAL VARCHAR(255);
     DECLARE degreeVal VARCHAR(255);

     DECLARE EXIT HANDLER FOR SQLWARNING
          BEGIN
               ROLLBACK;
               SELECT 'ALUMNI INSERT HAS FAILED';
          END;


     SET studentID = student_id;
     SET departmentVal = department;
     SET degreeVal = degree;


     IF EXISTS (SELECT 1 FROM alumni WHERE student_id = studentID ) THEN
               SELECT 'ALUMNI ALREADY EXISTS';
     ELSEIF NOT EXISTS (SELECT 1 FROM valid_departments WHERE UCASE(department) = UCASE(departmentVal)) THEN
               SELECT 'DEPARTMENT DOES NOT EXISTS';
     ELSEIF NOT EXISTS (SELECT 1 FROM valid_degrees WHERE UCASE(degree) = UCASE(degreevVal)) THEN
               SELECT 'DEGREE DOES NOT EXISTS';
     ELSE  
              SELECT 'ALUMNI ADDED';
     END IF;

     START TRANSACTION;
        INSERT INTO alumni (student_id, pwd ,first_name, last_name, street, city, state, zip_code, email, telephone, degree, department, role_id, donation_total) VALUES (student_id, NULL ,first_name, last_name, street, city, state, zip_code, email, telephone, degree, department, 1, 0.00);
     COMMIT;
END//
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:17:53+00:00Added an answer on June 13, 2026 at 1:17 pm
    1. I’d like to know what I’m doing wrong.

      As documented under Restrictions on Stored Programs:

      Name Conflicts within Stored Routines

      The same identifier might be used for a routine parameter, a local variable, and a table column. Also, the same local variable name can be used in nested blocks. For example:

      CREATE PROCEDURE p (i INT)
      BEGIN
        DECLARE i INT DEFAULT 0;
        SELECT i FROM t;
        BEGIN
          DECLARE i INT DEFAULT 1;
          SELECT i FROM t;
        END;
      END;
      

      In such cases, the identifier is ambiguous and the following precedence rules apply:

      • A local variable takes precedence over a routine parameter or table column.

      • A routine parameter takes precedence over a table column.

      • A local variable in an inner block takes precedence over a local variable in an outer block.

      The behavior that variables take precedence over table columns is nonstandard.

      In your case student_id is a routine parameter and studentID is a local variable; therefore (given the precedence rules above) the filter criterion WHERE student_id = studentID is comparing those two things with eachother and at no time is inspecting a table column.

      Since the local variable was set to the value of the routine parameter, this filter always evaluates to true.

      You could avoid this either by using different names for your parameters/variables, or else by qualifying your column reference with a table prefix:

      WHERE alumni.student_id = studentID
      
    2. Also, If the way I’m approaching this makes sense and if it doesn’t, what’s a more pragmatic way of going about this?

      Define suitable UNIQUE and foreign key constraints, then attempts to insert invalid data will fail without you explicitly having to check anything:

      ALTER TABLE alumni
        ADD UNIQUE  KEY (student_id),  -- is this not already a PRIMARY KEY ?
        ADD FOREIGN KEY (department) REFERENCES valid_departments (department),
        ADD FOREIGN KEY (degree    ) REFERENCES valid_degrees     (degree    )
      ;
      

      To make the foreign keys use a case-insensitive lookup, ensure that the respective columns use a case insensitive collation.

      Note the restrictions to which foreign keys are subject in the article linked above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I've tracked down a weird MySQL problem to the two different ways I was
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.