Как я могу преобразовать несколько операторов SQL INSERT в один оператор - PullRequest
0 голосов
/ 28 февраля 2019

У меня есть файл SQL со 100 операторами INSERT (см. Пример ниже), который я использовал для создания базы данных SQLITE.Сейчас я создаю базу данных MySQL и хочу преобразовать этот код в один оператор INSERT (пример ниже.)

Моя главная причина сделать это - просто сделать его немного лучше.Я надеялся, что будет хороший онлайн-конвертер liitle, но я не смог найти его, поэтому я здесь для предложений.

Текущий файл SQL состоит из тысяч таких файлов:

insert into customer (customer_id, customer_title, customer_forename, customer_surname, customer_address_1, customer_address_2, customer_town_city, customer_county, customer_postcode, customer_telephone_number, customer_email_address, customer_notes) values ('955773', 'Mrs', 'Valle', 'Keave', '95380', 'Gale', 'Helena', 'Montana', '59623', '406-989-2003', 'vkeave0@shareasale.com', 'grow world-class deliverables');
insert into customer (customer_id, customer_title, customer_forename, customer_surname, customer_address_1, customer_address_2, customer_town_city, customer_county, customer_postcode, customer_telephone_number, customer_email_address, customer_notes) values ('818817', 'Mrs', 'Melessa', 'Parmby', '914', 'Fairview', 'Montgomery', 'Alabama', '36114', '334-255-3054', 'mparmby1@addtoany.com', 'iterate seamless schemas');
insert into customer (customer_id, customer_title, customer_forename, customer_surname, customer_address_1, customer_address_2, customer_town_city, customer_county, customer_postcode, customer_telephone_number, customer_email_address, customer_notes) values ('896697', 'Mrs', 'Cecil', 'Capsey', '05', 'Bay', 'Mesa', 'Arizona', '85215', '602-646-6128', 'ccapsey2@cnet.com', 'implement revolutionary functionalities');
insert into customer (customer_id, customer_title, customer_forename, customer_surname, customer_address_1, customer_address_2, customer_town_city, customer_county, customer_postcode, customer_telephone_number, customer_email_address, customer_notes) values ('871100', 'Rev', 'Morgun', 'Coppeard', '049', 'High Crossing', 'Spokane', 'Washington', '99220', '509-838-3291', 'mcoppeard3@facebook.com', 'empower 24/365 users');
insert into customer (customer_id, customer_title, customer_forename, customer_surname, customer_address_1, customer_address_2, customer_town_city, customer_county, customer_postcode, customer_telephone_number, customer_email_address, customer_notes) values ('589912', 'Mr', 'Ginger', 'Annandale', '7', 'Clove', 'Dayton', 'Ohio', '45403', '937-265-6244', 'gannandale4@nasa.gov', 'integrate clicks-and-mortar content');
insert into customer (customer_id, customer_title, customer_forename, customer_surname, customer_address_1, customer_address_2, customer_town_city, customer_county, customer_postcode, customer_telephone_number, customer_email_address, customer_notes) values ('074451', 'Honorable', 'Nomi', 'Messitt', '402', 'Pond', 'Salt Lake City', 'Utah', '84115', '801-761-3351', 'nmessitt5@statcounter.com', 'empower dot-com channels');
insert into customer (customer_id, customer_title, customer_forename, customer_surname, customer_address_1, customer_address_2, customer_town_city, customer_county, customer_postcode, customer_telephone_number, customer_email_address, customer_notes) values ('321380', 'Mr', 'Cris', 'De Giovanni', '7', 'Northland', 'Kansas City', 'Missouri', '64199', '816-646-5785', 'cdegiovanni6@uol.com.br', 'envisioneer intuitive paradigms');
insert into customer (customer_id, customer_title, customer_forename, customer_surname, customer_address_1, customer_address_2, customer_town_city, customer_county, customer_postcode, customer_telephone_number, customer_email_address, customer_notes) values ('006367', 'Ms', 'Sherye', 'Commin', '72099', 'Crescent Oaks', 'Toledo', 'Ohio', '43666', '419-663-9711', 'scommin7@livejournal.com', 'visualize collaborative e-tailers');
insert into customer (customer_id, customer_title, customer_forename, customer_surname, customer_address_1, customer_address_2, customer_town_city, customer_county, customer_postcode, customer_telephone_number, customer_email_address, customer_notes) values ('587714', 'Mr', 'Phedra', 'Scone', '478', 'Monterey', 'San Francisco', 'California', '94105', '310-116-1148', 'pscone8@myspace.com', 'matrix seamless bandwidth');
insert into customer (customer_id, customer_title, customer_forename, customer_surname, customer_address_1, customer_address_2, customer_town_city, customer_county, customer_postcode, customer_telephone_number, customer_email_address, customer_notes) values ('801027', 'Mrs', 'Bayard', 'Brindley', '76049', 'Duke', 'El Paso', 'Texas', '79968', '915-207-0519', 'bbrindley9@mysql.com', 'visualize enterprise networks');

ЧтоЯ хочу достичь:

INSERT INTO
  customer (
    customer_id,
    customer_title,
    customer_forename,
    customer_surname,
    customer_address_1,
    customer_address_2,
    customer_town_city,
    customer_county,
    customer_postcode,
    customer_telephone_number,
    customer_email_address,
    customer_notes
  )
VALUES
  (
    1001,
    1002,
    1003,
    1004,
    1005,
    1006,
    1007,
    1008,
    1009,
    1010
  ),
  (
    'MR',
    'MR',
    'MR',
    'MR',
    'MR',
    'MR',
    'MR',
    'MR',
    'MR',
    'MR'
  ),
  (
    'Thomas',
    'Ben',
    'Rob',
    'Morgan',
    'Joe',
    'Samee',
    'Kitty',
    'Cameron',
    'Frank',
    'Eric'
  ),
  (
    'Masters',
    'Willy',
    'Norris',
    'Porch',
    'Mkoji',
    'Chouds',
    'Lewis',
    'Brooks',
    'Brown',
    'Cartman'
  ),
  (
    '6 Garthland Rd',
    '38 Downham Chase',
    '32 Bramside',
    '10 Rich Hill',
    '8 StockyP',
    '11 Hyde Rd',
    '18 BuzzinLa',
    '1 The Hills',
    '55 Pengting Rd',
    '10 Southpark'
  ),
  (
    'All Lies',
    'All Lies',
    'All Lies',
    'All Lies',
    'All Lies',
    'All Lies',
    'All Lies',
    'All Lies',
    'All Lies',
    'All Lies'
  ),
  (
    'Hazel Grove',
    'Timperley',
    'Bramhall',
    'Cheadle Hulme',
    'Stockport',
    'Hyde',
    'Liverpool',
    'New Mills',
    'Bolton',
    'Colorado'
  ),
  (
    'Some County',
    'Some County',
    'Some County',
    'Some County',
    'Some County',
    'Some County',
    'Some County',
    'Some County',
    'Some County',
    'Some County'
  ),
  (
    'SK7 4NA',
    'SK12 1EF',
    'M4 1HJ',
    'NK7 6HG',
    'PK9 8II',
    'SW1A 1AA',
    'HG8 8IK',
    'HJ87 JKK',
    'SK12 1QJ',
    'N8 7HJ'
  ),
  (
    '07704768408',
    '07704768408',
    '07704768408',
    '07704768408',
    '07704768408',
    '07704768408',
    '07704768408',
    '07704768408',
    '07704768408',
    '07704768408'
  ),
  (
    'LIE@MADEUP.COM',
    'LIE@MADEUP.COM',
    'LIE@MADEUP.COM',
    'LIE@MADEUP.COM',
    'LIE@MADEUP.COM',
    'LIE@MADEUP.COM',
    'LIE@MADEUP.COM',
    'LIE@MADEUP.COM',
    'LIE@MADEUP.COM',
    'LIE@MADEUP.COM'
  ),
  (
    'Here are some notes.',
    'Here are some notes.',
    'Here are some notes.',
    'Here are some notes.',
    'Here are some notes.',
    'Here are some notes.',
    'Here are some notes.',
    'Here are some notes.',
    'Here are some notes.',
    'Here are some notes.'
  );

Если есть быстрое решение проблемы, о которой вы знаете, пожалуйста, дайте мне знать.Вопрос, который был помечен как дубликат этого, в настоящее время остается тем же.Этот вопрос о скорости запроса, о преобразовании одного формата в другой.

Ответы [ 3 ]

0 голосов
/ 28 февраля 2019

Вы можете вставить несколько записей, выполнив следующие действия:

INSERT INTO customer (customer_id, customer_title, customer_forename, customer_surname, customer_address_1, customer_address_2, customer_town_city, customer_county, customer_postcode, customer_telephone_number, customer_email_address, customer_notes)
VALUES ('955773', 'Mrs', 'Valle', 'Keave', '95380', 'Gale', 'Helena', 'Montana', '59623', '406-989-2003', 'vkeave0@shareasale.com', 'grow world-class deliverables'),
('818817', 'Mrs', 'Melessa', 'Parmby', '914', 'Fairview', 'Montgomery', 'Alabama', '36114', '334-255-3054', 'mparmby1@addtoany.com', 'iterate seamless schemas'),
.
.
.
.
.
('801027', 'Mrs', 'Bayard', 'Brindley', '76049', 'Duke', 'El Paso', 'Texas', '79968', '915-207-0519', 'bbrindley9@mysql.com', 'visualize enterprise networks');

Вы должны разбивать каждую новую запись запятой: VALUES(.....), (........), последняя запись будет заканчиваться точкой с запятой: (......);

0 голосов
/ 28 февраля 2019

вы можете вставить, но в последнем разделе значения вы должны поставить точку с запятой, а перед последним разделом значения в каждом разделе вы должны поставить запятую EX:

   INSERT INTO [dbo].[tbl_Page]
       ([PageTitle]
       ,[Link]
       ,[Status]
       ,[CreatedDate]
       ,[Description])
 VALUES
       ('demo','demo',1,'12/11/2019','demo'),
       ('demo','demo',1,'12/11/2019','demo'),
       ('demo','demo',1,'12/11/2019','demo'),
       ('demo','demo',1,'12/11/2019','demo'),
       ('demo','demo',1,'12/11/2019','demo');

Проверено

See Image

0 голосов
/ 28 февраля 2019

insert поддерживает несколько значений для values.Разве это не работает для вас?

insert into customer (customer_id, customer_title, customer_forename, customer_surname, customer_address_1, customer_address_2, customer_town_city, customer_county, customer_postcode, customer_telephone_number, customer_email_address, customer_notes)
    values ('955773', 'Mrs', 'Valle', 'Keave', '95380', 'Gale', 'Helena', 'Montana', '59623', '406-989-2003', 'vkeave0@shareasale.com', 'grow world-class deliverables'),
           ('818817', 'Mrs', 'Melessa', 'Parmby', '914', 'Fairview', 'Montgomery', 'Alabama', '36114', '334-255-3054', 'mparmby1@addtoany.com', 'iterate seamless schemas')
           . . .

Итак, вы можете вставить несколько строк в один insert.Однако вы не можете вставить данные столбец за столбцом.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...