sql - Insert multiple rows into a table with only one value changing -


let's have table following columns:

field 1 | field 2 | field3 | field4

i want insert multiple rows in table, values of field1, field2 , field3 identical each row. value of field4 change.

obviously insert each row separately resulting query bit ugly, , i'm wondering if there more efficient / elegant way it.

i thought of example:

insert my_table (field1, field2, field3, field4) values (foo, bar, baz, ('value one','value two','value three','value four')) 

and result be:

field1 | field2 | field3 | field4 foo    | bar    | baz    | value 1 foo    | bar    | baz    | value 2 foo    | bar    | baz    | value 4 foo    | bar    | baz    | value 5 

in practice, 'field4' column string type, , different values known when write query. there's no need them table or (although if it's possible, i'm interested in solution can it) posible or have write each insert separately ?

edit: i've changed question more clear data type of changing column (general textual data) , data come from. sorry have answered without these information.

thanks.

you use variation on nicholas krasnov's answer case statement set string values:

insert my_table(field1, field2, field3, field4) select 'foo', 'bar', 'baz',   case level     when 1 'value one'     when 2 'value two'     when 3 'value three'     when 4 'value four'   end dual connect level <= 4;  select * my_table;  field1 field2 field3 field4              ------ ------ ------ -------------------- foo    bar    baz    value 1             foo    bar    baz    value 2             foo    bar    baz    value 3           foo    bar    baz    value 4            

sql fiddle.

adding more rows/values require change level limit , when clauses match. (like this). have else warning in case mismatch in numbers. there's no special significance string value goes level value, incidentally.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -