/* Den här funktionen skriver ut innehållet i tabellen EMPLOYEE, * och sen får man välja vilken anställd man vill höja lönen för. * Till skillnad från funktionen "fix_employee" i "esql-test-4", * som använder ESQL-satsen UPDATE, * använder vi här en cursor, som öppnas "for update". */ void fix_employee(void) { EXEC SQL begin declare section; char this_name[20 + 1]; int this_salary; char employee_to_change[100]; EXEC SQL end declare section; int found_the_employee; print_employee(); printf("\n"); printf("Vilken anställd vill du höja lönen med 10%% för? "); smart_fgets(employee_to_change, 100, stdin); skip_trailing_space(employee_to_change); EXEC SQL declare emp_cursor2 cursor for select name, salary from employee for update of salary; EXEC SQL open emp_cursor2; found_the_employee = 0; while (sqlca.sqlcode == 0) { EXEC SQL fetch emp_cursor2 into :this_name, :this_salary; /* Tänk på att INGRES tycker att "Bullock, J.D. " och "Bullock, J.D." * är lika, men strcmp vill att blanktecknen ska stämma också! */ skip_trailing_space(this_name); if (strcmp(this_name, employee_to_change) == 0) { EXEC SQL update employee set salary = salary * 1.10 where current of emp_cursor2; found_the_employee = 1; } } /* while */ EXEC SQL close emp_cursor2; if (! found_the_employee) { printf("Det finns ingen anställd med namnet %s!\n", employee_to_change); } else { printf("Ok! Ändringen utförd!\n"); print_employee(); } printf("found_the_employee = %d\n", found_the_employee); } /* fix_employee */