[英]Automatically change the Nth value of a 100+ line long SQL script for automatically inserting dummy data
I have a long SQL script that I'm converting from MySQL syntax to Postgres.我有一个很长的 SQL 脚本,我正在将它从 MySQL 语法转换为 Postgres。
There are about 100+ lines of script for inserting dummy data that look like this:大约有 100 多行用于插入虚拟数据的脚本,如下所示:
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
.我想将插入的第 4 列的值更改为
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.在所有 100 多个插入语句中,它当前设置为 1,在 MySQL 中,将强制转换为该列中的 boolean 值,但在 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
?我的问题是:是否有任何工具可以用来仅将每个语句的第 4 列中的
1
的值更改为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. Ctrl + f 并将
1
替换为true
将不起作用,因为我在插入语句中有另一个值,我想将其保留为 1。
There are 2 possibilities.有两种可能性。
set bool_col = true where numb_col = 1
then remove the numb_col and rename bool_col.set bool_col = true where numb_col = 1
然后删除 numb_col 并重命名 bool_col。'
,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.我特意包含了单引号和换行符的先例,以便我们只替换我们需要替换的
1
而不是任何其他的。
You can change the data type to integer
before the INSERT
s and back afterwards:您可以在
INSERT
之前将数据类型更改为integer
,然后再返回:
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.