Home > database >  Automatically change the Nth value of a 100 line long SQL script for automatically inserting dummy
Automatically change the Nth value of a 100 line long SQL script for automatically inserting dummy

Time:04-14

I have a long SQL script that I'm converting from MySQL syntax to Postgres.

There are about 100 lines of script for inserting dummy data that look like this:

INSERT INTO product (sku, name, description, image_url, active, units_in_stock,
unit_price, category_id, date_created)
VALUES ('BOOK-TECH-1001', 'Spring Framework Tutorial', 'Learn Spring',
'assets/images/products/placeholder.png'
,1,100,29.99,1, NOW());

INSERT INTO product (sku, name, description, image_url, active, units_in_stock,
unit_price, category_id, date_created)
VALUES ('BOOK-TECH-1002', 'Kubernetes - Deploying Containers', 'Learn Kubernetes',
'assets/images/products/placeholder.png'
,1,100,24.99,1, NOW());

INSERT INTO product (sku, name, description, image_url, active, units_in_stock,
unit_price, category_id, date_created)
VALUES ('BOOK-TECH-1003', 'Internet of Things (IoT) - Getting Started', 'Learn IoT',
'assets/images/products/placeholder.png'
,1,100,29.99,1, NOW());

-- etc...

I would like to change the value for the 4th column that's being inserted to true. In all 100 insert statements it's currently set to 1 which, in MySQL, would coerce to a boolean value in that column, but does not work in Postgres.

My question is: are there any tools out there that I could use to change only the value of 1 in every statement's 4th column to true?

For example, I want to change the following:

INSERT INTO product (sku, name, description, image_url, active, units_in_stock,
unit_price, category_id, date_created)
VALUES ('BOOK-TECH-1001', 'Spring Framework Tutorial', 'Learn Spring',
'assets/images/products/placeholder.png'
,1,100,29.99,1, NOW());

to this:

INSERT INTO product (sku, name, description, image_url, active, units_in_stock,
unit_price, category_id, date_created)
VALUES ('BOOK-TECH-1001', 'Spring Framework Tutorial', 'Learn Spring',
'assets/images/products/placeholder.png'
,true,100,29.99,1, NOW());

Any suggestions or online tools I could use? Ctrl f and replacing 1 with true won't work because I have another value in the insert statement that I want to keep at 1.

CodePudding user response:

There are 2 possibilities.

  • You import the value as a number, then do and update set bool_col = true where numb_col = 1 then remove the numb_col and rename bool_col.
  • You use a replace statement and replace
'
,1,

with

'
,true,

I've intentionally included the single quote and newline precedent so that we only replace the 1 that we need to replace and not any others.

CodePudding user response:

You can change the data type to integer before the INSERTs and back afterwards:

CREATE TABLE product (
   sku text NOT NULL,
   name text NOT NULL,
   description text,
   image_url text NOT NULL,
   active boolean NOT NULL,
   units_in_stock integer NOT NULL,
   unit_price numeric(15,2) NOT NULL,
   category_id integer NOT NULL,
   date_created timestamp with time zone NOT NULL
);

ALTER TABLE product ALTER active TYPE integer USING active::integer;

INSERT INTO product
   (sku, name, description,
    image_url, active, units_in_stock,
    unit_price, category_id, date_created)
VALUES ('BOOK-TECH-1001', 'Spring Framework Tutorial', 'Learn Spring',
        'assets/images/products/placeholder.png', 1, 100,
        29.99, 1, NOW());

ALTER TABLE product ALTER active TYPE boolean USING active::boolean;
  • Related