1

I am executing an alter table command and adding a new column with default value it is showing an error.

CREATE OR REPLACE FUNCTION test_function() RETURNS void AS $$
BEGIN
EXECUTE format('ALTER TABLE viminfo ADD COLUMN vimtype character varying(64) NOT NULL DEFAULT (NA), ADD COLUMN vimname character varying(255) NOT NULL DEFAULT (NA)');
END
$$language plpgsql;

Error: ERROR: column "na" does not exist

What is wrong I am doing here.. Thanks

1
  • Why the execute? You can write the DDL statement directly in the function without the use of dynamic SQL. Commented May 11, 2017 at 14:30

1 Answer 1

1

if you don't quote the string it is understood as object, try this:

    CREATE OR REPLACE FUNCTION test_function() RETURNS void AS $$
BEGIN
EXECUTE format('ALTER TABLE viminfo ADD COLUMN vimtype character varying(64) NOT NULL DEFAULT %L, ADD COLUMN vimname character varying(255) NOT NULL DEFAULT %L','NA','NA');
END
$$language plpgsql;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.