插入到 table 中,并在值后附加数字序列

Insert into a table with sequence of numbers appended to a value

我正在使用 postgres Sql,我打算将一百万条记录插入 table。但是,对于其中一列,我想插入一个值,该值附加了一个数字并按顺序增加。例如:

在上面的示例中,pxinsname 和 key 的递增值附加了常量文本 (S-, ABC )

我不确定如何逐步增加这些列中的值。尝试使用以下查询 -

insert into testable (
    pxcommitdatetime,pxsavedatetime,pxcreatedatetime,pxcreateoperator,pxcreateopname,pxinsname,key,tripname, source, destination, passengername,passengerage
)
select
CURRENT_TIMESTAMP,
CURRENT_TIMESTAMP,
CURRENT_TIMESTAMP,
test,
test,
// S- "Incremental value",
// ABC S "Incremental value",
    md5(random()::text),
    md5(random()::text),
    md5(random()::text),
    md5(random()::text),
    left(md5(random()::text), 4)
from generate_series(1, 10) s(i)

为此,您可以使用序列: https://www.postgresql.org/docs/current/sql-createsequence.html

首先您需要创建它:

CREATE SEQUENCE key_seq START 1;

然后在您的查询中使用如下:

insert into testable (
    pxcommitdatetime,pxsavedatetime,pxcreatedatetime,pxcreateoperator,pxcreateopname,pxinsname,key,tripname, source, destination, passengername,passengerage
)
select
CURRENT_TIMESTAMP,
CURRENT_TIMESTAMP,
CURRENT_TIMESTAMP,
test,
test,
// S- "Incremental value",
 "ABC S" || nextval('key_seq')::text,
    md5(random()::text),
    md5(random()::text),
    md5(random()::text),
    md5(random()::text),
    left(md5(random()::text), 4)
from generate_series(1, 10) s(i)