1

I have a bigint column named mycolumn. I execute SQL scripts using the PSQL command.

Using COPY command:

COPY public.mytable (myothercol, mycolumn) FROM stdin;
1   \N
\.

This works. But the following does not work:

EXECUTE 'insert into public.mytable (myothercol, mycolumn) values ($1,$2);' USING 
1,NULL;

This gives me error:

column "mycolumn" is of type bigint but expression is of type text

Why does insert not work for null value, whereas COPY works?

7
  • Why the dynamic SQL to begin with? You could simply use insert into public.mytable (myothercol, mycolumn) values ($1,$2); Commented Jan 16, 2019 at 15:56
  • @a_horse_with_no_name how do I give values of $1 $2 ? Commented Jan 16, 2019 at 15:57
  • Those are apparently parameters to your function, aren't they? Commented Jan 16, 2019 at 15:58
  • @a_horse_with_no_name I'm not using a function. Its a simple plpgsql script between DO END Commented Jan 16, 2019 at 16:32
  • Then use insert into public.mytable (myothercol, mycolumn) values (1, null); Commented Jan 16, 2019 at 16:49

1 Answer 1

2

You best tell PostgreSQL to convert the parameter to bigint explicitly:

EXECUTE 'insert into public.mytable (myothercol, mycolumn) values ($1,$2::bigint);'
   USING 1,NULL;

The problem is that PostgreSQL does not automatically know what data type a NULL is, so it guesses text. COPY does not have to guess a data type.

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.