Insert a row with NULL values from one table












0















I have a custom type:



CREATE TYPE public.relacion AS
(id integer,
codpadre character varying,
codhijo character varying,
canpres numeric(7,3),
cancert numeric(7,3),--this is usually NULL
posicion smallint);


After that, I need another type from relacion type:



CREATE TYPE public.borrarrelacion AS
(idborrar integer,
paso integer,
rel relacion);


Well, now into a function I need to copy some rows from a relacion type table to borrarrelacion type table:



This is a snippet of my code:



    DECLARE:
r relacion%ROWTYPE;
------------

BEGIN

EXECUTE FORMAT ('CREATE TABLE IF NOT EXISTS "mytable" OF borrarrelacion (PRIMARY KEY (idborrar))');
EXECUTE FORMAT ('SELECT * FROM %I WHERE codpadre = %s AND codhijo = %s',
tablarelacion,quote_literal(codigopadre),quote_literal(codigohijo)) INTO r;
EXECUTE FORMAT ('INSERT INTO "mytable" VALUES(0,0,%s)',r);


But I get an error because the field r.cancert is NULL and it's trying to insert something like that:



INSERT INTO "mytable" VALUES(0,0,(0,'cod1','cod2',10,,0));



I can solve this problem reading every field of r and putting it values in the INSERT statement, like that: (and changing the NULL value by 0)



EXECUTE FORMAT ('INSERT INTO "mytable" VALUES(0,0,(%s,%s,%s,%s,%s,%s))',
r.id,quote_literal(r.codpadre),quote_literal(r.codhijo),r.canpres,COALESCE(r.cancert,0),r.posicion);


But I would like to know if I can avoid this way and I can insert exactly the same row from one table to other.










share|improve this question























  • "But I get an error because the field r.cancert is NULL" - what error exactly do you get?

    – 404
    Nov 27 '18 at 22:06











  • @eurotrash, I get something like that: INSERT INTO "algo" VALUES(0,0,(1,"CAP 1",P01,10.000,,0)) and ERROR: syntax error at or near "," SQL state: 42601

    – user3733164
    Nov 27 '18 at 22:10













  • Why do you use dynamic SQL at all? None of the statements shown would require that.

    – a_horse_with_no_name
    Nov 27 '18 at 22:10











  • @a_horse_with_no_name, sorry. It's only a simplify example. The real problem create the tables dinamically and I need to use dynamic SQL

    – user3733164
    Nov 27 '18 at 22:12
















0















I have a custom type:



CREATE TYPE public.relacion AS
(id integer,
codpadre character varying,
codhijo character varying,
canpres numeric(7,3),
cancert numeric(7,3),--this is usually NULL
posicion smallint);


After that, I need another type from relacion type:



CREATE TYPE public.borrarrelacion AS
(idborrar integer,
paso integer,
rel relacion);


Well, now into a function I need to copy some rows from a relacion type table to borrarrelacion type table:



This is a snippet of my code:



    DECLARE:
r relacion%ROWTYPE;
------------

BEGIN

EXECUTE FORMAT ('CREATE TABLE IF NOT EXISTS "mytable" OF borrarrelacion (PRIMARY KEY (idborrar))');
EXECUTE FORMAT ('SELECT * FROM %I WHERE codpadre = %s AND codhijo = %s',
tablarelacion,quote_literal(codigopadre),quote_literal(codigohijo)) INTO r;
EXECUTE FORMAT ('INSERT INTO "mytable" VALUES(0,0,%s)',r);


But I get an error because the field r.cancert is NULL and it's trying to insert something like that:



INSERT INTO "mytable" VALUES(0,0,(0,'cod1','cod2',10,,0));



I can solve this problem reading every field of r and putting it values in the INSERT statement, like that: (and changing the NULL value by 0)



EXECUTE FORMAT ('INSERT INTO "mytable" VALUES(0,0,(%s,%s,%s,%s,%s,%s))',
r.id,quote_literal(r.codpadre),quote_literal(r.codhijo),r.canpres,COALESCE(r.cancert,0),r.posicion);


But I would like to know if I can avoid this way and I can insert exactly the same row from one table to other.










share|improve this question























  • "But I get an error because the field r.cancert is NULL" - what error exactly do you get?

    – 404
    Nov 27 '18 at 22:06











  • @eurotrash, I get something like that: INSERT INTO "algo" VALUES(0,0,(1,"CAP 1",P01,10.000,,0)) and ERROR: syntax error at or near "," SQL state: 42601

    – user3733164
    Nov 27 '18 at 22:10













  • Why do you use dynamic SQL at all? None of the statements shown would require that.

    – a_horse_with_no_name
    Nov 27 '18 at 22:10











  • @a_horse_with_no_name, sorry. It's only a simplify example. The real problem create the tables dinamically and I need to use dynamic SQL

    – user3733164
    Nov 27 '18 at 22:12














0












0








0








I have a custom type:



CREATE TYPE public.relacion AS
(id integer,
codpadre character varying,
codhijo character varying,
canpres numeric(7,3),
cancert numeric(7,3),--this is usually NULL
posicion smallint);


After that, I need another type from relacion type:



CREATE TYPE public.borrarrelacion AS
(idborrar integer,
paso integer,
rel relacion);


Well, now into a function I need to copy some rows from a relacion type table to borrarrelacion type table:



This is a snippet of my code:



    DECLARE:
r relacion%ROWTYPE;
------------

BEGIN

EXECUTE FORMAT ('CREATE TABLE IF NOT EXISTS "mytable" OF borrarrelacion (PRIMARY KEY (idborrar))');
EXECUTE FORMAT ('SELECT * FROM %I WHERE codpadre = %s AND codhijo = %s',
tablarelacion,quote_literal(codigopadre),quote_literal(codigohijo)) INTO r;
EXECUTE FORMAT ('INSERT INTO "mytable" VALUES(0,0,%s)',r);


But I get an error because the field r.cancert is NULL and it's trying to insert something like that:



INSERT INTO "mytable" VALUES(0,0,(0,'cod1','cod2',10,,0));



I can solve this problem reading every field of r and putting it values in the INSERT statement, like that: (and changing the NULL value by 0)



EXECUTE FORMAT ('INSERT INTO "mytable" VALUES(0,0,(%s,%s,%s,%s,%s,%s))',
r.id,quote_literal(r.codpadre),quote_literal(r.codhijo),r.canpres,COALESCE(r.cancert,0),r.posicion);


But I would like to know if I can avoid this way and I can insert exactly the same row from one table to other.










share|improve this question














I have a custom type:



CREATE TYPE public.relacion AS
(id integer,
codpadre character varying,
codhijo character varying,
canpres numeric(7,3),
cancert numeric(7,3),--this is usually NULL
posicion smallint);


After that, I need another type from relacion type:



CREATE TYPE public.borrarrelacion AS
(idborrar integer,
paso integer,
rel relacion);


Well, now into a function I need to copy some rows from a relacion type table to borrarrelacion type table:



This is a snippet of my code:



    DECLARE:
r relacion%ROWTYPE;
------------

BEGIN

EXECUTE FORMAT ('CREATE TABLE IF NOT EXISTS "mytable" OF borrarrelacion (PRIMARY KEY (idborrar))');
EXECUTE FORMAT ('SELECT * FROM %I WHERE codpadre = %s AND codhijo = %s',
tablarelacion,quote_literal(codigopadre),quote_literal(codigohijo)) INTO r;
EXECUTE FORMAT ('INSERT INTO "mytable" VALUES(0,0,%s)',r);


But I get an error because the field r.cancert is NULL and it's trying to insert something like that:



INSERT INTO "mytable" VALUES(0,0,(0,'cod1','cod2',10,,0));



I can solve this problem reading every field of r and putting it values in the INSERT statement, like that: (and changing the NULL value by 0)



EXECUTE FORMAT ('INSERT INTO "mytable" VALUES(0,0,(%s,%s,%s,%s,%s,%s))',
r.id,quote_literal(r.codpadre),quote_literal(r.codhijo),r.canpres,COALESCE(r.cancert,0),r.posicion);


But I would like to know if I can avoid this way and I can insert exactly the same row from one table to other.







postgresql






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 27 '18 at 22:00









user3733164user3733164

1729




1729













  • "But I get an error because the field r.cancert is NULL" - what error exactly do you get?

    – 404
    Nov 27 '18 at 22:06











  • @eurotrash, I get something like that: INSERT INTO "algo" VALUES(0,0,(1,"CAP 1",P01,10.000,,0)) and ERROR: syntax error at or near "," SQL state: 42601

    – user3733164
    Nov 27 '18 at 22:10













  • Why do you use dynamic SQL at all? None of the statements shown would require that.

    – a_horse_with_no_name
    Nov 27 '18 at 22:10











  • @a_horse_with_no_name, sorry. It's only a simplify example. The real problem create the tables dinamically and I need to use dynamic SQL

    – user3733164
    Nov 27 '18 at 22:12



















  • "But I get an error because the field r.cancert is NULL" - what error exactly do you get?

    – 404
    Nov 27 '18 at 22:06











  • @eurotrash, I get something like that: INSERT INTO "algo" VALUES(0,0,(1,"CAP 1",P01,10.000,,0)) and ERROR: syntax error at or near "," SQL state: 42601

    – user3733164
    Nov 27 '18 at 22:10













  • Why do you use dynamic SQL at all? None of the statements shown would require that.

    – a_horse_with_no_name
    Nov 27 '18 at 22:10











  • @a_horse_with_no_name, sorry. It's only a simplify example. The real problem create the tables dinamically and I need to use dynamic SQL

    – user3733164
    Nov 27 '18 at 22:12

















"But I get an error because the field r.cancert is NULL" - what error exactly do you get?

– 404
Nov 27 '18 at 22:06





"But I get an error because the field r.cancert is NULL" - what error exactly do you get?

– 404
Nov 27 '18 at 22:06













@eurotrash, I get something like that: INSERT INTO "algo" VALUES(0,0,(1,"CAP 1",P01,10.000,,0)) and ERROR: syntax error at or near "," SQL state: 42601

– user3733164
Nov 27 '18 at 22:10







@eurotrash, I get something like that: INSERT INTO "algo" VALUES(0,0,(1,"CAP 1",P01,10.000,,0)) and ERROR: syntax error at or near "," SQL state: 42601

– user3733164
Nov 27 '18 at 22:10















Why do you use dynamic SQL at all? None of the statements shown would require that.

– a_horse_with_no_name
Nov 27 '18 at 22:10





Why do you use dynamic SQL at all? None of the statements shown would require that.

– a_horse_with_no_name
Nov 27 '18 at 22:10













@a_horse_with_no_name, sorry. It's only a simplify example. The real problem create the tables dinamically and I need to use dynamic SQL

– user3733164
Nov 27 '18 at 22:12





@a_horse_with_no_name, sorry. It's only a simplify example. The real problem create the tables dinamically and I need to use dynamic SQL

– user3733164
Nov 27 '18 at 22:12












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53508871%2finsert-a-row-with-null-values-from-one-table%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53508871%2finsert-a-row-with-null-values-from-one-table%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown