In PL/SQL can I set a variable dynamically?












0















In Oracle 12c R2, I have a function which receives a row type as a variable. In the function I want to read a table which contains a column name and a value, I want to then populate the row type variable passed in using the column name and the data from the table I read.



Here is a simplistic idea of what I want to do;



CREATE TABLE table_to_be_updated
(
key_value number,
cola varchar2(2),
colb varchar2(2),
colc varchar2(2),
cold varchar2(2),
cole varchar2(2),
colf varchar2(2)
);

CREATE TABLE table_default_value
(
default_stuff number,
column_name varchar(30),
column_default_value varchar2(2)
);

function do_defaults(in_table table_to_be_updated%rowtype, in_value number) return table_to_be_updated%rowtype
is
out_table table_to_be_updated%rowtype := in_table;
cursor my_curs
is
select * from table_default_value where default_stuff = in_value;
begin
for default_rec in my_curs
loop
out_table.[default_rec.column_name] := default_rec.column_default_value
end loop;
return out_table;
end;
insert into table_default_value (default_stuff,column_name,column_default_value) values (1,'cola','xx'));
insert into table_default_value (default_stuff,column_name,column_default_value) values (1,'colc','aa'));
insert into table_default_value (default_stuff,column_name,column_default_value) values (1,'cole','bb'));


In the line;



out_table.[default_rec.column_name] := [default_rec.column_default_value]


[default_rec.column_name] would be the column name, from the cursor, in out_table name I want to move data to.



and



[default_rec.column_default_value] is the value from the cursor I want to move into that column.



I suspect that what I want to do is impossible in PL/SQL, but I thought I'd ask.



There are other ways to accomplish updating the table directly, specifically using dynamic SQL with execute immediate, but I have a number of similar tables which all need to have the same things done to them, and I would prefer a single function to work on a record and then pass it back to have the calling routine update the proper table.










share|improve this question























  • Hmm, I would try to do this with an associative array (index by varchar2) as the parameter instead of your %rowtype record which I don't think will work. And then just use default_rec.column_name as the key, e.g. out_table(default_rec.column_name) := default_rec.column_default_value;

    – kfinity
    Nov 28 '18 at 22:15











  • In some languages (usually weakly typed languages) you can treat record data types as associative arrays, but PL/SQL (a mostly strongly typed language) is not one of those languages.

    – Sentinel
    Nov 28 '18 at 23:41











  • I could create an associative array from the DB, but then the question is how to get the data from the associative array into the %rowtype variable.

    – Paul Stearns
    Dec 3 '18 at 17:01
















0















In Oracle 12c R2, I have a function which receives a row type as a variable. In the function I want to read a table which contains a column name and a value, I want to then populate the row type variable passed in using the column name and the data from the table I read.



Here is a simplistic idea of what I want to do;



CREATE TABLE table_to_be_updated
(
key_value number,
cola varchar2(2),
colb varchar2(2),
colc varchar2(2),
cold varchar2(2),
cole varchar2(2),
colf varchar2(2)
);

CREATE TABLE table_default_value
(
default_stuff number,
column_name varchar(30),
column_default_value varchar2(2)
);

function do_defaults(in_table table_to_be_updated%rowtype, in_value number) return table_to_be_updated%rowtype
is
out_table table_to_be_updated%rowtype := in_table;
cursor my_curs
is
select * from table_default_value where default_stuff = in_value;
begin
for default_rec in my_curs
loop
out_table.[default_rec.column_name] := default_rec.column_default_value
end loop;
return out_table;
end;
insert into table_default_value (default_stuff,column_name,column_default_value) values (1,'cola','xx'));
insert into table_default_value (default_stuff,column_name,column_default_value) values (1,'colc','aa'));
insert into table_default_value (default_stuff,column_name,column_default_value) values (1,'cole','bb'));


In the line;



out_table.[default_rec.column_name] := [default_rec.column_default_value]


[default_rec.column_name] would be the column name, from the cursor, in out_table name I want to move data to.



and



[default_rec.column_default_value] is the value from the cursor I want to move into that column.



I suspect that what I want to do is impossible in PL/SQL, but I thought I'd ask.



There are other ways to accomplish updating the table directly, specifically using dynamic SQL with execute immediate, but I have a number of similar tables which all need to have the same things done to them, and I would prefer a single function to work on a record and then pass it back to have the calling routine update the proper table.










share|improve this question























  • Hmm, I would try to do this with an associative array (index by varchar2) as the parameter instead of your %rowtype record which I don't think will work. And then just use default_rec.column_name as the key, e.g. out_table(default_rec.column_name) := default_rec.column_default_value;

    – kfinity
    Nov 28 '18 at 22:15











  • In some languages (usually weakly typed languages) you can treat record data types as associative arrays, but PL/SQL (a mostly strongly typed language) is not one of those languages.

    – Sentinel
    Nov 28 '18 at 23:41











  • I could create an associative array from the DB, but then the question is how to get the data from the associative array into the %rowtype variable.

    – Paul Stearns
    Dec 3 '18 at 17:01














0












0








0








In Oracle 12c R2, I have a function which receives a row type as a variable. In the function I want to read a table which contains a column name and a value, I want to then populate the row type variable passed in using the column name and the data from the table I read.



Here is a simplistic idea of what I want to do;



CREATE TABLE table_to_be_updated
(
key_value number,
cola varchar2(2),
colb varchar2(2),
colc varchar2(2),
cold varchar2(2),
cole varchar2(2),
colf varchar2(2)
);

CREATE TABLE table_default_value
(
default_stuff number,
column_name varchar(30),
column_default_value varchar2(2)
);

function do_defaults(in_table table_to_be_updated%rowtype, in_value number) return table_to_be_updated%rowtype
is
out_table table_to_be_updated%rowtype := in_table;
cursor my_curs
is
select * from table_default_value where default_stuff = in_value;
begin
for default_rec in my_curs
loop
out_table.[default_rec.column_name] := default_rec.column_default_value
end loop;
return out_table;
end;
insert into table_default_value (default_stuff,column_name,column_default_value) values (1,'cola','xx'));
insert into table_default_value (default_stuff,column_name,column_default_value) values (1,'colc','aa'));
insert into table_default_value (default_stuff,column_name,column_default_value) values (1,'cole','bb'));


In the line;



out_table.[default_rec.column_name] := [default_rec.column_default_value]


[default_rec.column_name] would be the column name, from the cursor, in out_table name I want to move data to.



and



[default_rec.column_default_value] is the value from the cursor I want to move into that column.



I suspect that what I want to do is impossible in PL/SQL, but I thought I'd ask.



There are other ways to accomplish updating the table directly, specifically using dynamic SQL with execute immediate, but I have a number of similar tables which all need to have the same things done to them, and I would prefer a single function to work on a record and then pass it back to have the calling routine update the proper table.










share|improve this question














In Oracle 12c R2, I have a function which receives a row type as a variable. In the function I want to read a table which contains a column name and a value, I want to then populate the row type variable passed in using the column name and the data from the table I read.



Here is a simplistic idea of what I want to do;



CREATE TABLE table_to_be_updated
(
key_value number,
cola varchar2(2),
colb varchar2(2),
colc varchar2(2),
cold varchar2(2),
cole varchar2(2),
colf varchar2(2)
);

CREATE TABLE table_default_value
(
default_stuff number,
column_name varchar(30),
column_default_value varchar2(2)
);

function do_defaults(in_table table_to_be_updated%rowtype, in_value number) return table_to_be_updated%rowtype
is
out_table table_to_be_updated%rowtype := in_table;
cursor my_curs
is
select * from table_default_value where default_stuff = in_value;
begin
for default_rec in my_curs
loop
out_table.[default_rec.column_name] := default_rec.column_default_value
end loop;
return out_table;
end;
insert into table_default_value (default_stuff,column_name,column_default_value) values (1,'cola','xx'));
insert into table_default_value (default_stuff,column_name,column_default_value) values (1,'colc','aa'));
insert into table_default_value (default_stuff,column_name,column_default_value) values (1,'cole','bb'));


In the line;



out_table.[default_rec.column_name] := [default_rec.column_default_value]


[default_rec.column_name] would be the column name, from the cursor, in out_table name I want to move data to.



and



[default_rec.column_default_value] is the value from the cursor I want to move into that column.



I suspect that what I want to do is impossible in PL/SQL, but I thought I'd ask.



There are other ways to accomplish updating the table directly, specifically using dynamic SQL with execute immediate, but I have a number of similar tables which all need to have the same things done to them, and I would prefer a single function to work on a record and then pass it back to have the calling routine update the proper table.







oracle plsql oracle12c






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 28 '18 at 21:23









Paul StearnsPaul Stearns

392220




392220













  • Hmm, I would try to do this with an associative array (index by varchar2) as the parameter instead of your %rowtype record which I don't think will work. And then just use default_rec.column_name as the key, e.g. out_table(default_rec.column_name) := default_rec.column_default_value;

    – kfinity
    Nov 28 '18 at 22:15











  • In some languages (usually weakly typed languages) you can treat record data types as associative arrays, but PL/SQL (a mostly strongly typed language) is not one of those languages.

    – Sentinel
    Nov 28 '18 at 23:41











  • I could create an associative array from the DB, but then the question is how to get the data from the associative array into the %rowtype variable.

    – Paul Stearns
    Dec 3 '18 at 17:01



















  • Hmm, I would try to do this with an associative array (index by varchar2) as the parameter instead of your %rowtype record which I don't think will work. And then just use default_rec.column_name as the key, e.g. out_table(default_rec.column_name) := default_rec.column_default_value;

    – kfinity
    Nov 28 '18 at 22:15











  • In some languages (usually weakly typed languages) you can treat record data types as associative arrays, but PL/SQL (a mostly strongly typed language) is not one of those languages.

    – Sentinel
    Nov 28 '18 at 23:41











  • I could create an associative array from the DB, but then the question is how to get the data from the associative array into the %rowtype variable.

    – Paul Stearns
    Dec 3 '18 at 17:01

















Hmm, I would try to do this with an associative array (index by varchar2) as the parameter instead of your %rowtype record which I don't think will work. And then just use default_rec.column_name as the key, e.g. out_table(default_rec.column_name) := default_rec.column_default_value;

– kfinity
Nov 28 '18 at 22:15





Hmm, I would try to do this with an associative array (index by varchar2) as the parameter instead of your %rowtype record which I don't think will work. And then just use default_rec.column_name as the key, e.g. out_table(default_rec.column_name) := default_rec.column_default_value;

– kfinity
Nov 28 '18 at 22:15













In some languages (usually weakly typed languages) you can treat record data types as associative arrays, but PL/SQL (a mostly strongly typed language) is not one of those languages.

– Sentinel
Nov 28 '18 at 23:41





In some languages (usually weakly typed languages) you can treat record data types as associative arrays, but PL/SQL (a mostly strongly typed language) is not one of those languages.

– Sentinel
Nov 28 '18 at 23:41













I could create an associative array from the DB, but then the question is how to get the data from the associative array into the %rowtype variable.

– Paul Stearns
Dec 3 '18 at 17:01





I could create an associative array from the DB, but then the question is how to get the data from the associative array into the %rowtype variable.

– Paul Stearns
Dec 3 '18 at 17:01












1 Answer
1






active

oldest

votes


















0














Here is the best I can come up with;



function do_defaults(in_table table_to_be_updated%rowtype, in_value number) return table_to_be_updated%rowtype
is
TYPE DEFAULT_TYPE IS TABLE OF VARCHAR2(2)
INDEX BY VARCHAR2(30);
DEFAULT_ARRAY DEFAULT_TYPE;

out_table table_to_be_updated%rowtype := in_table;
cursor my_curs
is
select * from table_default_value where default_stuff = in_value;
begin
DEFAULT_ARRAY('cola') := null;
DEFAULT_ARRAY('colb') := null;
DEFAULT_ARRAY('colc') := null;
DEFAULT_ARRAY('cold') := null;
DEFAULT_ARRAY('cole') := null;
DEFAULT_ARRAY('colf') := null;
for default_rec in my_curs
loop
DEFAULT_ARRAY(default_rec.column_name) := default_rec.column_default_value
end loop;
out_table.cola := DEFAULT_ARRAY('cola');
out_table.colb := DEFAULT_ARRAY('colb');
out_table.colc := DEFAULT_ARRAY('colc');
out_table.cold := DEFAULT_ARRAY('cold');
out_table.cole := DEFAULT_ARRAY('cole');
out_table.colf := DEFAULT_ARRAY('colf');
return out_table;
end;





share|improve this answer
























    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%2f53528330%2fin-pl-sql-can-i-set-a-variable-dynamically%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Here is the best I can come up with;



    function do_defaults(in_table table_to_be_updated%rowtype, in_value number) return table_to_be_updated%rowtype
    is
    TYPE DEFAULT_TYPE IS TABLE OF VARCHAR2(2)
    INDEX BY VARCHAR2(30);
    DEFAULT_ARRAY DEFAULT_TYPE;

    out_table table_to_be_updated%rowtype := in_table;
    cursor my_curs
    is
    select * from table_default_value where default_stuff = in_value;
    begin
    DEFAULT_ARRAY('cola') := null;
    DEFAULT_ARRAY('colb') := null;
    DEFAULT_ARRAY('colc') := null;
    DEFAULT_ARRAY('cold') := null;
    DEFAULT_ARRAY('cole') := null;
    DEFAULT_ARRAY('colf') := null;
    for default_rec in my_curs
    loop
    DEFAULT_ARRAY(default_rec.column_name) := default_rec.column_default_value
    end loop;
    out_table.cola := DEFAULT_ARRAY('cola');
    out_table.colb := DEFAULT_ARRAY('colb');
    out_table.colc := DEFAULT_ARRAY('colc');
    out_table.cold := DEFAULT_ARRAY('cold');
    out_table.cole := DEFAULT_ARRAY('cole');
    out_table.colf := DEFAULT_ARRAY('colf');
    return out_table;
    end;





    share|improve this answer




























      0














      Here is the best I can come up with;



      function do_defaults(in_table table_to_be_updated%rowtype, in_value number) return table_to_be_updated%rowtype
      is
      TYPE DEFAULT_TYPE IS TABLE OF VARCHAR2(2)
      INDEX BY VARCHAR2(30);
      DEFAULT_ARRAY DEFAULT_TYPE;

      out_table table_to_be_updated%rowtype := in_table;
      cursor my_curs
      is
      select * from table_default_value where default_stuff = in_value;
      begin
      DEFAULT_ARRAY('cola') := null;
      DEFAULT_ARRAY('colb') := null;
      DEFAULT_ARRAY('colc') := null;
      DEFAULT_ARRAY('cold') := null;
      DEFAULT_ARRAY('cole') := null;
      DEFAULT_ARRAY('colf') := null;
      for default_rec in my_curs
      loop
      DEFAULT_ARRAY(default_rec.column_name) := default_rec.column_default_value
      end loop;
      out_table.cola := DEFAULT_ARRAY('cola');
      out_table.colb := DEFAULT_ARRAY('colb');
      out_table.colc := DEFAULT_ARRAY('colc');
      out_table.cold := DEFAULT_ARRAY('cold');
      out_table.cole := DEFAULT_ARRAY('cole');
      out_table.colf := DEFAULT_ARRAY('colf');
      return out_table;
      end;





      share|improve this answer


























        0












        0








        0







        Here is the best I can come up with;



        function do_defaults(in_table table_to_be_updated%rowtype, in_value number) return table_to_be_updated%rowtype
        is
        TYPE DEFAULT_TYPE IS TABLE OF VARCHAR2(2)
        INDEX BY VARCHAR2(30);
        DEFAULT_ARRAY DEFAULT_TYPE;

        out_table table_to_be_updated%rowtype := in_table;
        cursor my_curs
        is
        select * from table_default_value where default_stuff = in_value;
        begin
        DEFAULT_ARRAY('cola') := null;
        DEFAULT_ARRAY('colb') := null;
        DEFAULT_ARRAY('colc') := null;
        DEFAULT_ARRAY('cold') := null;
        DEFAULT_ARRAY('cole') := null;
        DEFAULT_ARRAY('colf') := null;
        for default_rec in my_curs
        loop
        DEFAULT_ARRAY(default_rec.column_name) := default_rec.column_default_value
        end loop;
        out_table.cola := DEFAULT_ARRAY('cola');
        out_table.colb := DEFAULT_ARRAY('colb');
        out_table.colc := DEFAULT_ARRAY('colc');
        out_table.cold := DEFAULT_ARRAY('cold');
        out_table.cole := DEFAULT_ARRAY('cole');
        out_table.colf := DEFAULT_ARRAY('colf');
        return out_table;
        end;





        share|improve this answer













        Here is the best I can come up with;



        function do_defaults(in_table table_to_be_updated%rowtype, in_value number) return table_to_be_updated%rowtype
        is
        TYPE DEFAULT_TYPE IS TABLE OF VARCHAR2(2)
        INDEX BY VARCHAR2(30);
        DEFAULT_ARRAY DEFAULT_TYPE;

        out_table table_to_be_updated%rowtype := in_table;
        cursor my_curs
        is
        select * from table_default_value where default_stuff = in_value;
        begin
        DEFAULT_ARRAY('cola') := null;
        DEFAULT_ARRAY('colb') := null;
        DEFAULT_ARRAY('colc') := null;
        DEFAULT_ARRAY('cold') := null;
        DEFAULT_ARRAY('cole') := null;
        DEFAULT_ARRAY('colf') := null;
        for default_rec in my_curs
        loop
        DEFAULT_ARRAY(default_rec.column_name) := default_rec.column_default_value
        end loop;
        out_table.cola := DEFAULT_ARRAY('cola');
        out_table.colb := DEFAULT_ARRAY('colb');
        out_table.colc := DEFAULT_ARRAY('colc');
        out_table.cold := DEFAULT_ARRAY('cold');
        out_table.cole := DEFAULT_ARRAY('cole');
        out_table.colf := DEFAULT_ARRAY('colf');
        return out_table;
        end;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 4 '18 at 20:37









        Paul StearnsPaul Stearns

        392220




        392220
































            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%2f53528330%2fin-pl-sql-can-i-set-a-variable-dynamically%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







            Popular posts from this blog

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)