Using Nested IF ELSE statements in sql -
when 1 of following conditions met, want code go next execution step:
- first name, last name , dob : 3 not blank
- id , dob not blank
- ssn , dob not blank
- id , group number not blank
below code have. when run providing first name, last name , dob (condition 1 satisfied), still fails saying condition 4 not met. can tell me doing wrong?
if ( ( @firstname null or len(ltrim(@firstname)) = 0 ) , ( @lastname null or len(ltrim(@lastname)) = 0 ) , ( @dob null ) ) begin insert @validationerror (errormessage) values ( 'first name, last name , date of birth must specified.' ) end else begin if ( @dob null , @id null ) begin insert @validationerror (errormessage) values ( 'date of birth , id must specified.' ) end else begin if ( @dob null , @ssn null ) begin insert @validationerror (errormessage) values ( 'date of birth , ssn must specified.' ) end else begin if ( @id null , @groupnumber null ) begin insert @validationerror (errormessage) values ( 'id , group number must specified.' ) end end end end
a case
statement simpler:
insert @validationerror (errormessage) select case when criteria1 'first name, last name , date of birth must specified.' when criteria2 'date of birth , id must specified.' when criteria3 'date of birth , ssn must specified.' when criteria4 'id , group number must specified.' end
as far error in syntax, you've got extraneous begin
, end
, believe following work:
if ( ( @firstname null or len(ltrim(@firstname)) = 0 ) , ( @lastname null or len(ltrim(@lastname)) = 0 ) , ( @dob null ) ) begin insert @validationerror (errormessage) values ( 'first name, last name , date of birth must specified.') end else if ( @dob null , @id null ) begin insert @validationerror (errormessage) values ( 'date of birth , id must specified.' ) end else if ( @dob null , @ssn null ) begin insert @validationerror (errormessage) values ( 'date of birth , ssn must specified.' ) end else if ( @id null , @groupnumber null ) begin insert @validationerror (errormessage) values ( 'id , group number must specified.' ) end
Comments
Post a Comment