function - Using variable for fieldname in postgresql -
I have a function where I can be able to use text variables as a field name in a post variable function Want to
Clear _currentEmployee text; _ Mercant integer; Reserve to choose ... Loop _currentEmployee = _record.name _myCount = (TblTraining from SELECT count (*) WHERE tblTraining._currentEmployee = 4); If _mycount = 0 ... end loop; In fact, it is a routine to check the training records of each employee. The table schema has obvious issues because the employees are given the form of columns in the training table instead of the normalized id in each line. Is entered in, but I have to work with whatever I have found here. Whenever I use the function, this field names and processing, instead of changing it correctly, its _currentEmployee
behaves. I get an error:
_currentlEmployee tbl is not a training area
Tips?
Generally, parametric identifiers are not allowed in SQL.
Performed by a dynamic SQL string in the PL / PGSUL function. Beware of SQL injection Column names must be treated as user input.
DECLARE _rec records; _ct Integer; To select RAP ... for loop executed format ('TLL traineancing from selection number (*) Where TBTering.% I = 4', _rec.name); - Saves the SQL injection in% I; IF _ct = 0 then ... end loop;
-
>> clears the wire to be a valid identifier with
% i
prevents syntax errors and SQL injection. Keep in mind that unquoted identifiers are always placed at the lower level in postgres. If your correct column name is short-lived, you have to make the minimum version available here. Also do not use CaMeL-case identifiers in Post-Grace to avoid all related issues that begin with -
The plpgsql assignment operator is
: =
(but not needed in my reply) -
For more and more examples, try searching for +. :
Comments
Post a Comment