There is no explanation what the function is supposed to do exactly and the code is ambiguous in multiple places. As I understand your codeit, this is the purpose of the function is:
Reset a given sequence in a given schema to the maximum value of a given column in a given table of the same schema - or to the minimum value of the given sequence if that should be bigger.
You are mixing
format()with plain string concatenation in nonsensical ways. Be sure to read the manual onformat()before you continue.The variable
schema_namewas undefined. I added another function parameter to paspass it. It's odd that you use the schemadboin the twosetval()calls at the end. Also"dbo"is a typical identifier for SQL Server, but not in Postgres. Maybe another error or on purpose?The variable
maxValwas undefined. Probably should be_maxVal. I removed that variable completely in the simplified version.You don't need
+ 1forsetval(), since the next value returned is incremented by default. Example in the manual: