Search code examples
postgresqlplpgsqlpostgresql-9.3

Using Single quotes for a value inside plpgsql function


I have postgresql function which accepts 4 arguments

CREATE OR REPLACE FUNCTION public.manual_table_parsing_monthly_custom_edits(
schema_name text,
main_permit text,
manual_table text,
permit_num text)
RETURNS void AS
$BODY$
DECLARE
    e record;
    load_move_count double precision;
BEGIN
  EXECUTE format('Insert into %I.%I(permit_date,permit_number,year,month_number,
    report_permit_date,job_category,address,legal_description,neighbourhood,neighbourhood_number,
    job_description,work_type,floor_area,construction_value,zoning,units_added,
    latitude,longitude,location,count,building_type,building_type_number,filter,community,
    groups,plan,block,lot,unit_id,dwelling,condo_fh,developer,builder,unit_address,construction_type,geom)
            select permit_date,permit_number,year,month_number,
    report_permit_date,job_category,address,legal_description,neighbourhood,neighbourhood_number,
    job_description,work_type,floor_area,construction_value,zoning,units_added,
    latitude,longitude,location,count,building_type,building_type_number,filter,community,
    groups,plan,block,lot,unit_id,dwelling,condo_fh,developer,builder,unit_address,construction_type,geom
from %I.%I
where flag like ''move'' or flag like ''load'' and permit_number 
=%I',schema_name,main_permit,schema_name,manual_table,permit_number);

The last argument in a function permit_number which is used in the where statement is raising error no column 'yyyy'. Its because the %I is taking the value within double quotes. How should i edit so that the value will be within single quotes


Solution

  • As per documentiaion %I should be used for identifiers, and %L should be used for literal values.

    Also should use $$ quoting, to ease reading of your function...