How to change the format of a query result?

Asked

Viewed 365 times

0

I would like to know if there is any argument in select that changes the format of result to be used as dump. Example:

create table `table_name` (`id` int, `value` text);
insert into `table_name` values (1, "a"), (2, "b"), (3, "c");

select * from `table_name`;
/* Result: "insert into `table_name` values (1, "a"), (2, "b"), (3, "c")" */

2 answers

1

Paulo, I have never seen in any database any function that does what you want, but you can get the similar result by mounting the sql inside select using the function Concat(), with this your example would be this way:

select concat('insert into table_name values (',id,',',value,');') from table_name;

Each row of the result will be an Insert with a table record.

  • I’ve never seen anything like this by cli (other than G in mysql), but due to the fact that many database tools have this option to export the query result as Insert, I thought it might be possible. I believe the most appropriate approach would be to carry out the concat as you commented.

0

I don’t know this function of generating the Insert by SQL.

However Mysql Workbench after script execution has export option for various formats: CSV, HTML, JSON, SQL INSERT and etc.

inserir a descrição da imagem aqui

  • Thank you for the attention and reply friend, but my interest would be to accomplish by the part of the cli same :/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.