I am getting an issue where when I try to reference the parameters directly by name I am getting back the literal values after the function gets called. Can anyone help me out with how I can use the parameter values here?
CREATE OR REPLACE FUNCTION dbo.reset_sequence( tablename text, columnname text, sequence_name text) RETURNS void AS $BODY$ DECLARE BEGIN IF( (SELECT MAX( columnname ) ) < (SELECT min_value FROM dbo.tablename) ) THEN -- EXECUTE 'SELECT setval( ' || sequence_name || ', (SELECT min_value FROM dbo.' || sequence_name ||')'; ELSE -- EXECUTE 'SELECT setval( ' || sequence_name || ', ' || '(SELECT MAX("' || columnname || '") FROM dbo."' || tablename || '")' || '+1)'; END IF; END; $BODY$ LANGUAGE plpgsql VOLATILE; EDIT: The issue I am having is more specifically related to the syntax outside the EXECUTE commands. The other solution doesn't really help me there.
After researching another topic I am trying another solution but am still getting issues.
CREATE OR REPLACE FUNCTION dbo.reset_sequence( tablename text, columnname text, sequence_name text) RETURNS void AS $BODY$ DECLARE _maxVal int; _minVal int; BEGIN EXECUTE format('SELECT MAX( ''' || columnname || ''' ) FROM ' || schema_name || '."' || tablename || '"') INTO _maxVal; EXECUTE format('SELECT min_value FROM ' || schema_name || '."' || sequence_name || ''' ') INTO _minVal; IF( maxVal < _minVal) THEN -- EXECUTE 'SELECT setval( ' || sequence_name || ', (SELECT min_value FROM dbo.' || sequence_name ||')'; ELSE -- EXECUTE 'SELECT setval( ' || sequence_name || ', ' || '(SELECT MAX("' || columnname || '") FROM dbo."' || tablename || '")' || '+1)'; END IF; END; $BODY$ LANGUAGE plpgsql VOLATILE; The syntax on this works, however when I call the function I get an error where it can't store the EXECUTE statements as integers, it seems to be returning the name of the column, not the max value in that column.