Question

So i created a table, called "exam" with three columns "Name", "Rollno" and "Result P/F". I want it to do the following:

When the name is input (via '&name' syntax), it automatically assigns the first name with the roll number (Rollno) 1501 AND then asks you for the Result, for which you can only enter either "P" or "F", for pass or fail, respectively. When you input the next name, it increments the Rollno to 1502.. and so on.

About the Rollno part, i have a sequence called EXAMSQN. It looks like: The first one

About the "Result P/F" part, I have a check constraint. And this is how i insert values to the table:

insert into exam (name, rollno, "Result P/F")
values ('&name', examsqn.nextval, '&Result P/F')

And by the way, this is how my check constraint goes:

1  alter table exam
2* add constraint exam_result_ck check ("Result P/F" in ('P','F'))

So what I expect from this is it take only a P or and an F, when it asks for Result

I am asked for the name, and the result but right after that i get an error. This is how the whole this goes:

Enter value for name: nikh
Enter value for result: P
old   2: values ('&name', examsqn.nextval, '&Result P/F')
new   2: values ('nikh', examsqn.nextval, 'P P/F')
insert into exam (name, rollno, "Result P/F")
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.EXAM_RESULT_CK) violated

Your help is greatly appreciated.

Thank you :)

Was it helpful?

Solution

To get it to work all you need to do is remove the 'P/F' from '&Result P/F'

You can see this from the old, new output that it is trying to insert 'P P/F' which is why your constraint fails.

Also you would be better off not calling your column "Result P/F" but using an alias.

Cheers

OTHER TIPS

The output clearly shows the substituted value:

new   2: values ('nikh', examsqn.nextval, 'P P/F')

Obviously 'P P/F' != 'P' and 'P P/F' != 'F'. So the check constraint hurls.

This happens because of the space in your variable name. I suggest you use an underscore instead: &Result_PF. Or just call it result.

If you want to tell the users what the valid values are you need to use a SQL*Plus prompt:

accept result_pf prompt "Please enter a value for Result (P/F): "
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top