There is a filled table. Can I get an INSERT code containing all the values ​​in the table? Those. there is a table

+-----+-----------------+ | id | name | +-----+-----------------+ | 1 | Ivan Ivanov | | 2 | Sergey Petrov | +-----+-----------------+ 

You need to get a code from it:

 INSERT INTO table VALUES(1, Ivan Ivanov) INSERT INTO table VALUES(2, Sergey Petrov) 

Is it possible?

2 answers 2

 SELECT GROUP_CONCAT(CONCAT('INSERT INTO table VALUES(', id, ',\'', name, '\')') SEPARATOR ';\n') INTO OUTFILE 'c://folder/filename.sql' FROM `table`; 
  • There is a sql-server tag in the question. And the function group_concat is only in MySQL, as well as into outfile - Mike
  • Yes, group_concat is not necessary here, nothing prevents you from merging lines into a file one by one. And MS SQL has an upload to the file, even if syntactically the request will look different. Yes, I didn’t pay attention to the tag, right. Well, it turned out a demonstration of the idea, and not ready-made code. It happens. - Akina

Well, you can see the commands through the database import or programmatically conclude

  • But can you learn more about how to do this? - t16bz
  • In Management Studio using the Generate Script command. - msi
  • @msi where is this command? - t16bz
  • t16bz, right-click on the base name. - msi
  • @msi there is no such thing - t16bz