简体   繁体   中英

inserting values into table using sql

I'm trying to insert the date into a table which contain 4 columns first i inserted data in 2 columns and after i inserted data in remaining 2 columns but data is insert in 2nd row instead of 1st row and i want this data in 1st row only. we can insert it by using "update table set...." but i want to insert the data into table in a such a manner .. when we inserting a value in a particular column it has to check its previous row if it is null then the data should add in previous row (filling of rows with all columns at any time)

my table=words(vulgar,voilance,offence,hate) as columns

insert into words(vulgar,voilance) values('xxx','kill');

select * from words;


vulgar-voilance-offence-hate

 xxx--------kill----------------------

insert into words(offence,hate) values('idiot','snake');

select * from words;

vulga---voilance----offence----hate

xxx---------kill----------------------------   
                ------------------------   idiot----    snake

In my knowledge, this is not possible in SQL, without "advanced" logic to retrieve row with null and update in case it finds a row, or insert in case it doesent.

However, your question leaves alot to the imagination; what if there are several rows with nulls, what if you insert values in two columns, but only one is null, etc.

I think you need to rethink your datamodel.

As others pointed out, this is not the best data model you can have, so you might consider redesign. Referring strictly to the technical question itself, since you are on Oracle you can use PL/SQL to achieve that, like (in your example):

create or replace procedure p_insert_2_words(p_offence in varchar2, p_hate in varchar2)
is
begin
    update words set offence = p_offence and hate = p_hate 
        where offence is null and hate is null;
    if sql%rowcount = 0
    then
        insert into words(offence,hate) values(p_offence, p_hate);
    end if;
end;

starting from this point you can create a more generic procedure that will insert / update all the words:

create or replace procedure p_insert_or_update(p_offence in varchar2 default null, p_hate in varchar2 default null, p_vulgar in varchar2 default null, p_voilance in varchar2 default null)
is
begin
    if p_offence is not null
    then
        update words set offence = p_offence
            where offence is null;
        if sql%rowcount = 0
        then
            insert into words(offence) values(p_offence);
        end if;
    end if;
    if p_hate is not null
    then
        update words set hate = p_hate 
            where hate  is null;
        if sql%rowcount = 0
        then
            insert into words(hate ) values(p_hate);
        end if;
    end if;
    if p_offence is not null
    then
        update words set offence = p_offence
            where offence is null;
        if sql%rowcount = 0
        then
            insert into words(offence) values(p_offence);
        end if;
    end if;
    if p_vulgar is not null
    then
        update words set voilance = p_voilance 
            where voilance is null;
        if sql%rowcount = 0
        then
            insert into words(voilance) values(p_voilance);
        end if;
    end if;
end;

You can copy-paste the above code and then call this like (call must be made as StoredProcedure ):

p_insert_or_update(p_offence => 'idiot', p_hate => 'snake');
p_insert_or_update(p_offence => 'idiot');
p_insert_or_update(p_offence => 'idiot', p_voilance => 'kill');

or any other combination you need. The result will be the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM