9.21 Change SQL filtering condition

 

Modify the filtering condition in a SQL statement.
The following SQL statement aims to select employees in the sales department whose salary is above 10,000. Now we are trying to re-specify department in the filtering condition as R&D.

select 
	EID,NAME,SURNAME,DEPT,SALARY
from
	Employee
where
	DEPT='Sales' and SALARY>10000

In SPL, we use s.sqlparse(part) to split a SQL statement and generate a sequence of parts of the SQL. When parameter part is present, use part value to replace the corresponding part of the SQL statement and return a new SQL statement. @w option represents where clause and @s represents select statement.

SPL script:

A
1 select EID,NAME,SURNAME,DEPT,SALARY from Employee where DEPT=‘Sales’ and SALARY>10000
2 =A1.sqlparse@w()
3 =A2.split@t(“and”)
4 =A3.pselect(like(~,“DEPT*”))
5 =A3(A4)=“DEPT=‘R&D’”
6 =A3.concat("and")
7 =A1.sqlparse@w(A6)

A1 Define a SQL statement constant.
A2 s.sqlparse() function works with @w option to get the where conditional clause.
A3 Use s.split() function to split where clause. @t enables trimming each split part.
A4 Get the current department condition.
A5 Change the department condition to R&D.
A6 Use and to concatenate members of the sequence of conditions.
A7 s.sqlparse(part) uses @w option to replace the old where conditional clause.

Execution result:

Value
select EID,NAME,SURNAME,DEPT,SALARY from Employee where DEPT=‘R&D’ and SALARY>10000