Como faço para passar o valor como inteiro?
Realmente, o next_value devia ser passado como inteiro, não literal.
Para resolver isso, basta usar o %s ao invés de %L. Este irá alocar
o valor diretamente, por isso recomendo um CAST no parâmetro (ele já
é inteiro, mas é só para garantir 100% que não haverá erros):
format('CREATE SEQUENCE %I.%I START %s OWNED BY %I.%I.%I;',
...., row.next_value::int, ...);
OK! Mudando o %L por %s já passa o valor como inteiro e funciona (mesmo
sem o cast ::int). A função ficou assim.
CREATE OR REPLACE FUNCTION wosis.fun_create_sequence(sch text)
RETURNS text
LANGUAGE plpgsql VOLATILE
AS
$BODY$
DECLARE
row record;
BEGIN
FOR row IN
SELECT table_name, table_name||'_id' AS id_name, 0 AS
next_value
FROM information_schema.tables
WHERE table_schema = sch
AND table_type = 'BASE TABLE'
ORDER BY table_name
LOOP
EXECUTE
format(
'SELECT max(%I)+1 FROM %I.%I;',
row.id_name,
sch,
row.table_name)
INTO row.next_value;
EXECUTE
format(
'CREATE SEQUENCE %I.%I START %s OWNED BY
%I.%I.%I;',
sch,
row.table_name||'_'||row.id_name||'_seq',
row.next_value,
sch,
row.table_name,
row.id_name
);
EXECUTE
format(
'ALTER TABLE %I.%I ALTER COLUMN %I SET DEFAULT
nextval(%L);',
sch,
row.table_name,
row.id_name,
sch || '.'
||row.table_name||'_'||row.id_name||'_seq'
);
END LOOP;
RETURN 'Sequences were created';
END;
$BODY$;
ALTER FUNCTION wosis.fun_create_sequence(text) OWNER TO postgres;
COMMENT ON FUNCTION wosis.fun_create_sequence(text) IS 'Create sequences
in all tables in a given schema';
Muito obrigado pela ajuda!
[]'s
Eloi
_______________________________________________
pgbr-geral mailing list
[email protected]
https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral